diff options
Diffstat (limited to 'util/Config.py')
-rw-r--r-- | util/Config.py | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/util/Config.py b/util/Config.py index 040e9b0..4c1eb1e 100644 --- a/util/Config.py +++ b/util/Config.py @@ -3,9 +3,11 @@ import sys import xml import pdb import util.Strings as Strings +import util.Search as Search from logger import main_log, exception_log classArgsMem = {} CONFIG_PATH = 'config/' +DEFAULT_OVERRIDE_MODE = 'Merge' def loadParamRequirementDict(className): if not className in classArgsMem: #WOO CACHING classArgsMem[className] = fileToDict(CONFIG_PATH + className) @@ -16,9 +18,9 @@ def loadConfigFile(fileName): #TODO: error handling etc. if '.params' in fileName: return fileToDict(fileName) if '.xml' in fileName: - config = ElementTree() #use .fromstring, and resolve xincludes + config = ElementTree() config.parse(fileName) - config = ElementTree(resolveConfigInheritance(config.getroot())) + resolveDocumentInheritances(config.getroot()) return config except Exception as inst: main_log.error('Error loading config file ' + fileName)#, inst) TODO: log exception too @@ -30,13 +32,15 @@ def getElement(el): return el elif el.__class__ == ElementTree: return el.getroot() +#XML tree composition. Returns the resulting tree, but happens in-place in the overriding tree. def compositeXMLTrees(parentTree, overridingTree): #TODO: break up into sub-methods, change it to #use .find() #type checking -- convert ElementTrees to their root elements if parentTree == None: return overridingTree if overridingTree == None: - return parentTree + return parentTree #TODO: this will probably cause a bug since it isn't in-place on + #overridingTree parentTree = getElement(parentTree) overridingTree = getElement(overridingTree) parentItems = parentTree.getchildren() @@ -53,7 +57,7 @@ def compositeXMLTrees(parentTree, overridingTree): #TODO: break up into sub-meth main_log.warn('ABUSE! Override of multiple items isn\'t well defined. Don\'t do\ it!') interEl = intersectingElements[0] - mode = 'Replace' + mode = DEFAULT_OVERRIDE_MODE if Strings.OVERRIDE_BEHAVIOR in interEl.attrib: mode = interEl.attrib[Strings.OVERRIDE_BEHAVIOR] if mode != 'Replace' and mode != 'Merge': @@ -91,6 +95,15 @@ def fileToDict(fileName): exception_log.info(fileName + ' is not a well formed python dict. Parsing failed') return eval(fileText) #parses arguments into python objects if possible, otherwise leaves as strings +def pullArgsFromItem(parentNode): + attribArgs = {} + for arg in parentNode.attrib: #automatically pull attributes into the argdict + attribArgs[arg] = parentNode.attrib[arg] + argNode = parentNode.find('Args') + args = generateArgDict(argNode) + for key in attribArgs: + args[key] = attribArgs[key] + return args def generateArgDict(parentNode, recurse=False): args = {} for arg in parentNode.getchildren(): @@ -113,8 +126,13 @@ def generateArgDict(parentNode, recurse=False): if len(args.keys()) == 1 and recurse: return args[args.keys()[0]] return args - -def resolveConfigInheritance(el): +#In place resolution of document inheritances. Doesn't return anything. +def resolveDocumentInheritances(el): + abstractMembers = Search.parental_tree_search(el, '.getchildren()', '.tag==\'InheritsFrom\'') + for subel in abstractMembers: + subel = resolveInheritance(subel) +#In place resolution of inheritence. Doesn't return anything. +def resolveInheritance(el): parentClass = el.find('InheritsFrom') if parentClass != None: parentTree = loadConfigFile(parentClass.text) @@ -122,6 +140,5 @@ def resolveConfigInheritance(el): main_log.warn('Inheritance Failed. ' + parentClass.text + 'does not exist') main_log.error('Inheritance Failed. ' + parentClass.text + 'does not exist') return el - el = compositeXMLTrees(el, parentTree) + el = compositeXMLTrees(parentTree, el) el.remove(parentClass) #get rid of the inheritance flag - return el |