Package PyFoam :: Package Applications :: Module CommonPickledDataInput
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.CommonPickledDataInput

 1  """ 
 2  Class that implements reading vom a pickled file. Every utility that 
 3  gets input from a pipe should use it 
 4  """ 
 5  from optparse import OptionGroup 
 6   
 7  import cPickle as pickle  
 8  import sys 
 9   
10 -class CommonPickledDataInput(object):
11 """ The class that defines the options for reading from a pickled plot 12 """ 13
14 - def addOptions(self):
15 pickled=OptionGroup(self.parser, 16 "Pickled file reading", 17 "Options for reading from a pickled file") 18 self.parser.add_option_group(pickled) 19 pickled.add_option("--pickled-file", 20 action="store", 21 default=None, 22 dest="pickledFileRead", 23 help=""" 24 File from which the pickled data should be read. If this is set to 25 'stdin' then the data is read from the standard-input to allow using 26 the pipe into it. If unset and stdin is not a terminal, then it is 27 automatically chosen""") 28 29 pickled.add_option("--print-data", 30 action="store_true", 31 default=False, 32 dest="printPickledData", 33 help="print the pickled data after is has been read") 34 35 pickled.add_option("--print-stdout", 36 action="store_true", 37 default=False, 38 dest="printStdout", 39 help="Print the standard-output (if it has been safed into the pickled file)")
40
41 - def readPickledData(self):
42 if "inputData" in self: 43 if self.opts.pickledFileRead: 44 self.error("--pickled-file specified, when input data was provided via the Python-API") 45 data=self["inputData"] 46 else: 47 if not self.opts.pickledFileRead: 48 if sys.stdin.isatty(): 49 self.error("The option --pickled-file has to be set") 50 else: 51 self.opts.pickledFileRead="stdin" 52 53 if self.opts.pickledFileRead=="stdin": 54 pick=pickle.Unpickler(sys.stdin) 55 else: 56 pick=pickle.Unpickler(open(self.opts.pickledFileRead)) 57 data=pick.load() 58 del pick 59 60 if self.opts.printStdout: 61 try: 62 print data["stdout"] 63 except KeyError: 64 print "<No stdout in data>" 65 if self.opts.printPickledData: 66 import pprint 67 printer=pprint.PrettyPrinter() 68 printer.pprint(data) 69 70 return data
71