1
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
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,args=args,description=description,usage="%prog [options] <dictfile> <key>",nr=2,interspersed=True)
26
28 self.parser.add_option("--debug",action="store_true",default=None,dest="debug",help="Debugs the parser")
29
30
32 fName=self.parser.getArgs()[0]
33 all=self.parser.getArgs()[1]
34
35 match=re.compile("([a-zA-Z_][a-zA-Z0-9_]*)(.*)").match(all)
36 if match==None:
37 self.error("Expression",all,"not usable as an expression")
38
39 key=match.group(1)
40 sub=None
41 if len(match.groups())>1:
42 if match.group(2)!="":
43 sub=match.group(2)
44
45 try:
46 dictFile=ParsedParameterFile(fName,backup=False,debug=self.opts.debug)
47 val=dictFile[key]
48 except KeyError:
49 self.error("Key: ",key,"not existing in File",fName)
50 except IOError,e:
51 self.error("Problem with file",fName,":",e)
52
53 if sub==None:
54 erg=val
55 else:
56 try:
57 erg=eval(str(val)+sub)
58 except Exception,e:
59 self.error("Problem with subexpression:",sys.exc_info()[0],":",e)
60
61 print erg
62