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