"""Shell script plots make images via command line programs.
These are usually shell scripts to command line programs for data
analysis that use the filesystem for io. This can be rather slow
processing, but it makes for easy development.
"""
from dataplot import GenericPlot
import os
class Plot(GenericPlot):
"""Make plots through the filesystem and command line programs.
Subclasses need to define:
1. A method get_plot_args, which returns a dictionary with at
least one key 'data' which maps to a text string that will be
written out to the input file for the command line program.
2. The attribute cmdtmp, which is a template string that will be
filled in with the dictionary returned by get_plot_args. This
string will be the command line that is executed to make the
plot. You should use the 'infile' key in your string, i.e.
cmdtmp='/usr/local/bin/hairpinplot.bash %(infile)s'
"""
def get_cline_template_dict(self):
"""Dict used to fill in self.cmdtmp string.
"""
di=getattr(self,'TEMPLATE_DICT_CACHE',None)
if di:
return di
di={
'infile':self.get_full_base(),
'outfile':self.from_filename(),
}
# Defaults may be overridden by arguments
# additional arguments may also be specified by get_plot_args dict
plotargs=self.get_plot_args()
di.update(plotargs)
self.TEMPLATE_DICT_CACHE=di
return di
def get_cline(self):
"""Construct command line based on template and dict.
"""
di=self.get_cline_template_dict()
cmd=self.cmdtmp%di
return cmd
def write_data_to_infile(self):
"""Write data from get_plot_args into infile specified.
Presumably this data is the input for a command-line program.
"""
di=self.get_cline_template_dict()
f=di['infile']
fh=open(f,'w')
fh.write(di['data'])
fh.close()
self.do_chgrp_on(f)
def makefile(self):
"""Write data to infile and then execute the commandline.
This presumably makes the convert_from file.
"""
self.write_data_to_infile()
cmd=self.get_cline()
os.system(cmd)