00001 """Shell script plots make images via command line programs.
00002
00003 These are usually shell scripts to command line programs for data
00004 analysis that use the filesystem for io. This can be rather slow
00005 processing, but it makes for easy development.
00006
00007 """
00008
00009 from dataplot import GenericPlot
00010 import os
00011
00012 class Plot(GenericPlot):
00013 """Make plots through the filesystem and command line programs.
00014
00015 Subclasses need to define:
00016
00017 1. A method get_plot_args, which returns a dictionary with at
00018 least one key 'data' which maps to a text string that will be
00019 written out to the input file for the command line program.
00020
00021 2. The attribute cmdtmp, which is a template string that will be
00022 filled in with the dictionary returned by get_plot_args. This
00023 string will be the command line that is executed to make the
00024 plot. You should use the 'infile' key in your string, i.e.
00025
00026 cmdtmp='/usr/local/bin/hairpinplot.bash %(infile)s'
00027
00028 """
00029
00030 def get_cline_template_dict(self):
00031 """Dict used to fill in self.cmdtmp string.
00032
00033 """
00034 di=getattr(self,'TEMPLATE_DICT_CACHE',None)
00035 if di:
00036 return di
00037 di={
00038 'infile':self.get_full_base(),
00039 'outfile':self.from_filename(),
00040 }
00041
00042
00043 plotargs=self.get_plot_args()
00044 di.update(plotargs)
00045 self.TEMPLATE_DICT_CACHE=di
00046 return di
00047
00048 def get_cline(self):
00049 """Construct command line based on template and dict.
00050
00051 """
00052 di=self.get_cline_template_dict()
00053 cmd=self.cmdtmp%di
00054 return cmd
00055
00056 def write_data_to_infile(self):
00057 """Write data from get_plot_args into infile specified.
00058
00059 Presumably this data is the input for a command-line program.
00060
00061 """
00062 di=self.get_cline_template_dict()
00063 f=di['infile']
00064 fh=open(f,'w')
00065 fh.write(di['data'])
00066 fh.close()
00067 self.do_chgrp_on(f)
00068
00069 def makefile(self):
00070 """Write data to infile and then execute the commandline.
00071
00072 This presumably makes the convert_from file.
00073
00074 """
00075 self.write_data_to_infile()
00076 cmd=self.get_cline()
00077 os.system(cmd)
00078