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