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

Source Code for Module PyFoam.Applications.ReadDictionary

 1  #  ICE Revision: $Id: ReadDictionary.py 9161 2008-08-04 08:01:05Z bgschaid $  
 2  """ 
 3  Application class that implements pyFoamReadDictionary 
 4  """ 
 5   
 6  import sys,re 
 7   
 8  from PyFoamApplication import PyFoamApplication 
 9   
10  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
11   
12 -class ReadDictionary(PyFoamApplication):
13 - def __init__(self,args=None):
14 description=""" 15 Reads a value from a Foam-Dictionary and prints it to the screen. 16 The description of the value is word. If the value is 17 non-atomic (a list or a dictionary) it is output in Python-notation. 18 Parts of the expression can be accessed by using the Python-notation for accessing 19 sub-expressions. 20 21 Example of usage: 22 pyFoamReadDictionary.py pitzDaily/0/U "boundaryField['inlet']['type']" 23 """ 24 25 PyFoamApplication.__init__(self, 26 args=args, 27 description=description, 28 usage="%prog [options] <dictfile> <key>", 29 nr=2, 30 changeVersion=False, 31 interspersed=True)
32
33 - def addOptions(self):
34 self.parser.add_option("--debug", 35 action="store_true", 36 default=None, 37 dest="debug", 38 help="Debugs the parser")
39 40
41 - def run(self):
42 fName=self.parser.getArgs()[0] 43 all=self.parser.getArgs()[1] 44 45 match=re.compile("([a-zA-Z_][a-zA-Z0-9_]*)(.*)").match(all) 46 if match==None: 47 self.error("Expression",all,"not usable as an expression") 48 49 key=match.group(1) 50 sub=None 51 if len(match.groups())>1: 52 if match.group(2)!="": 53 sub=match.group(2) 54 55 try: 56 dictFile=ParsedParameterFile(fName,backup=False,debug=self.opts.debug) 57 val=dictFile[key] 58 except KeyError: 59 self.error("Key: ",key,"not existing in File",fName) 60 except IOError,e: 61 self.error("Problem with file",fName,":",e) 62 63 if sub==None: 64 erg=val 65 else: 66 try: 67 erg=eval(str(val)+sub) 68 except Exception,e: 69 self.error("Problem with subexpression:",sys.exc_info()[0],":",e) 70 71 print erg
72