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

Source Code for Module PyFoam.Applications.CommonLibFunctionTrigger

 1  """Implements a trigger that removes the libs and/or function 
 2  entry from the controlDict""" 
 3   
 4  import re 
 5  from os import path 
 6  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
 7  from PyFoam.Error import warning 
 8   
9 -class CommonLibFunctionTrigger(object):
10 """ The class that does the actual triggering 11 """ 12
13 - def addOptions(self):
14 self.parser.add_option("--remove-libs", 15 action="store_true", 16 dest="removeLibs", 17 default=False, 18 help="Remove the libs entry from the controlDict for the duration of the application run") 19 self.parser.add_option("--remove-functions", 20 action="store_true", 21 dest="removeFunctions", 22 default=False, 23 help="Remove the functions entry from the controlDict for the duration of the application run")
24
25 - def addLibFunctionTrigger(self,run,sol):
26 if self.opts.removeLibs or self.opts.removeFunctions: 27 warning("Adding Trigger to reset lib/function at end") 28 trig=LibFunctionTrigger(sol,self.opts.removeLibs,self.opts.removeFunctions) 29 run.addEndTrigger(trig.resetIt)
30 31
32 -class LibFunctionTrigger:
33 - def __init__(self,sol,libs,funs):
34 self.control=ParsedParameterFile(path.join(sol.systemDir(),"controlDict"),backup=True) 35 36 self.fresh=False 37 38 try: 39 if libs and ("libs" in self.control): 40 warning("Temporarily removing the libs-entry from the controlDict") 41 del self.control["libs"] 42 self.fresh=True 43 if funs and ("functions" in self.control): 44 warning("Temporarily removing the functions-entry from the controlDict") 45 del self.control["functions"] 46 self.fresh=True 47 48 if self.fresh: 49 self.control.writeFile() 50 except Exception,e: 51 warning("Restoring defaults") 52 self.control.restore() 53 raise e
54
55 - def resetIt(self):
56 if self.fresh: 57 warning("Trigger called: Resetting controlDict") 58 self.control.restore() 59 self.fresh=False
60