1
2 """
3 Application class that implements pyFoamReadDictionary
4 """
5
6 import sys
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 with an option
19 """
20
21 PyFoamApplication.__init__(self,args=args,description=description,usage="%prog [options] <dictfile> <key>",nr=2,interspersed=True)
22
24 self.parser.add_option("--subexpression",action="store",default=None,dest="subexpression",help="A subexpression (in python notation to access parts of the value)")
25 self.parser.add_option("--debug",action="store_true",default=None,dest="debug",help="Debugs the parser")
26
27
29 fName=self.parser.getArgs()[0]
30 key=self.parser.getArgs()[1]
31
32 try:
33 dictFile=ParsedParameterFile(fName,backup=False,debug=self.opts.debug)
34 val=dictFile[key]
35 except KeyError:
36 self.error("Key: ",key,"not existing in File",fName)
37 except IOError,e:
38 self.error("Problem with file",fName,":",e)
39
40 if self.opts.subexpression==None:
41 erg=val
42 else:
43 try:
44 erg=eval(str(val)+self.opts.subexpression)
45 except Exception,e:
46 self.error("Problem with subexpression:",sys.exc_info()[0],":",e)
47
48 print erg
49