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

Source Code for Module PyFoam.Applications.WriteDictionary

  1  #  ICE Revision: $Id$ 
  2  """ 
  3  Application class that implements pyFoamWriteDictionary 
  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_,exec_ 
 13   
14 -class WriteDictionary(PyFoamApplication):
15 - def __init__(self, 16 args=None, 17 **kwargs):
18 description="""\ 19 Write a value to a Foam-Dictionary. The description of the value is 20 word. If the value is non-atomic (a list or a dictionary) it has to be 21 in in Python-notation. Parts of the expression can be accessed by 22 using the Python-notation for accessing sub-expressions. 23 24 Example of usage: 25 > pyFoamWriteDictionary.py --test pitzDaily/0/U "boundaryField['inlet']['type']" zeroGradient < 26 """ 27 28 PyFoamApplication.__init__(self, 29 args=args, 30 description=description, 31 usage="%prog [options] <dictfile> <key> <val>", 32 changeVersion=False, 33 nr=3, 34 interspersed=False, 35 **kwargs)
36
37 - def addOptions(self):
38 self.parser.add_option("--test", 39 action="store_true", 40 dest="test", 41 default=False, 42 help="Doesn't write to the file, but outputs the result on stdout") 43 44 self.parser.add_option("--strip-quotes-from-value", 45 action="store_true", 46 dest="stripQuotes", 47 default=False, 48 help="Strip the quotes from the value if they had to be defined") 49 50 self.parser.add_option("--evaluate", 51 action="store_false", 52 dest="verbatim", 53 default=True, 54 help="Interpret the string as a python expression before assigning it")
55 56
57 - def run(self):
58 fName=self.parser.getArgs()[0] 59 all=self.parser.getArgs()[1] 60 if all[0]=='"': 61 all=all[1:] 62 if all[-1]=='"': 63 all=all[:-1] 64 65 val=self.parser.getArgs()[2] 66 if self.opts.stripQuotes: 67 if val[0]=='"': 68 val=val[1:] 69 if val[-1]=='"': 70 val=val[:-1] 71 72 73 match=re.compile("([a-zA-Z_][a-zA-Z0-9_]*)(.*)").match(all) 74 if match==None: 75 self.error("Expression",all,"not usable as an expression") 76 77 key=match.group(1) 78 sub=None 79 if len(match.groups())>1: 80 if match.group(2)!="": 81 sub=match.group(2) 82 83 if self.opts.verbatim: 84 newValue=val 85 else: 86 newValue=eval(val) 87 88 try: 89 dictFile=ParsedParameterFile(fName,backup=True) 90 val=dictFile[key] 91 except KeyError: 92 self.error("Key: ",key,"not existing in File",fName) 93 except IOError: 94 e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' 95 self.error("Problem with file",fName,":",e) 96 97 if sub==None: 98 dictFile[key]=newValue 99 else: 100 try: 101 exec_("dictFile[key]"+sub+"=newValue") 102 except Exception: 103 e = sys.exc_info()[1] # Needed because python 2.5 does not support 'as e' 104 self.error("Problem with subexpression:",sys.exc_info()[0],":",e) 105 106 if self.opts.test: 107 print_(str(dictFile)) 108 else: 109 dictFile.writeFile()
110