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

Source Code for Module PyFoam.Applications.ReadDictionary

 1  """ 
 2  Application class that implements pyFoamReadDictionary 
 3  """ 
 4   
 5  import sys 
 6   
 7  from PyFoamApplication import PyFoamApplication 
 8   
 9  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
10   
11 -class ReadDictionary(PyFoamApplication):
12 - def __init__(self):
13 description=""" 14 Reads a value from a Foam-Dictionary and prints it to the screen. 15 The description of the value is word. If the value is 16 non-atomic (a list or a dictionary) it is output in Python-notation. 17 Parts of the expression can be accessed with an option 18 """ 19 20 PyFoamApplication.__init__(self,description=description,usage="%prog [options] <dictfile> <key>",nr=2,interspersed=True)
21
22 - def addOptions(self):
23 self.parser.add_option("--subexpression",action="store",default=None,dest="subexpression",help="A subexpression (in python notation to access parts of the value)") 24 self.parser.add_option("--debug",action="store_true",default=None,dest="debug",help="Debugs the parser")
25 26
27 - def run(self):
28 fName=self.parser.getArgs()[0] 29 key=self.parser.getArgs()[1] 30 31 try: 32 dictFile=ParsedParameterFile(fName,backup=False,debug=self.opts.debug) 33 val=dictFile[key] 34 except KeyError: 35 self.error("Key: ",key,"not existing in File",fName) 36 except IOError,e: 37 self.error("Problem with file",fName,":",e) 38 39 if self.opts.subexpression==None: 40 erg=val 41 else: 42 try: 43 erg=eval(str(val)+self.opts.subexpression) 44 except Exception,e: 45 self.error("Problem with subexpression:",sys.exc_info()[0],":",e) 46 47 print erg
48