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