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