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

Source Code for Module PyFoam.Applications.ReadDictionary

 1  #  ICE Revision: $Id$ 
 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  from PyFoam.ThirdParty.six import print_ 
13   
14 -class ReadDictionary(PyFoamApplication):
15 - def __init__(self, 16 args=None, 17 **kwargs):
18 description="""\ 19 Reads a value from a Foam-Dictionary and prints it to the screen. The 20 description of the value is word. If the value is non-atomic (a list 21 or a dictionary) it is output in Python-notation. Parts of the 22 expression can be accessed by using the Python-notation for accessing 23 sub-expressions. 24 25 Example of usage: 26 pyFoamReadDictionary.py pitzDaily/0/U "boundaryField['inlet']['type']" 27 """ 28 29 PyFoamApplication.__init__(self, 30 args=args, 31 description=description, 32 usage="%prog [options] <dictfile> <key>", 33 nr=2, 34 changeVersion=False, 35 interspersed=True, 36 **kwargs)
37
38 - def addOptions(self):
39 self.parser.add_option("--debug", 40 action="store_true", 41 default=None, 42 dest="debug", 43 help="Debugs the parser")
44 45
46 - def run(self):
47 fName=self.parser.getArgs()[0] 48 all=self.parser.getArgs()[1] 49 if all[0]=='"': 50 all=all[1:] 51 if all[-1]=='"': 52 all=all[:-1] 53 54 match=re.compile("([a-zA-Z_][a-zA-Z0-9_]*)(.*)").match(all) 55 if match==None: 56 self.error("Expression",all,"not usable as an expression") 57 58 key=match.group(1) 59 sub=None 60 if len(match.groups())>1: 61 if match.group(2)!="": 62 sub=match.group(2) 63 64 try: 65 dictFile=ParsedParameterFile(fName,backup=False,debug=self.opts.debug) 66 val=dictFile[key] 67 except KeyError: 68 self.error("Key: ",key,"not existing in File",fName) 69 except IOError: 70 e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' 71 self.error("Problem with file",fName,":",e) 72 73 if sub==None: 74 erg=val 75 else: 76 try: 77 erg=eval(str(val)+sub) 78 except Exception: 79 e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' 80 self.error("Problem with subexpression:",sys.exc_info()[0],":",e) 81 82 print_(erg)
83 84 # Should work with Python3 and Python2 85