aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorGravatar rcoh <rcoh@mit.edu>2010-12-29 17:27:33 -0500
committerGravatar rcoh <rcoh@mit.edu>2010-12-29 17:27:33 -0500
commit93dfb8e3003b483c1041c6f7b4ff293935aeb7c0 (patch)
tree6b0e336f664fa519cad164756b68840e56b50b16 /util
parentc8209d01f9ddf4c6670caee08073924cb33e447f (diff)
Inheritence now happens at all levels on reading of config files. No additional resolution of
inheritances should be necessary. RCOH
Diffstat (limited to 'util')
-rw-r--r--util/Config.py18
-rw-r--r--util/Search.py14
2 files changed, 26 insertions, 6 deletions
diff --git a/util/Config.py b/util/Config.py
index 040e9b0..dd0b790 100644
--- a/util/Config.py
+++ b/util/Config.py
@@ -3,6 +3,7 @@ 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/'
@@ -16,9 +17,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,6 +31,7 @@ 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
@@ -113,8 +115,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 +129,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
diff --git a/util/Search.py b/util/Search.py
index 25882da..f7e4b81 100644
--- a/util/Search.py
+++ b/util/Search.py
@@ -6,3 +6,17 @@ def find_le(a, x):
def find_ge(a, x):
'Find leftmost value greater than x'
return bisect_left(a, x)
+#returns parents of nodes that meed a given condition
+def parental_tree_search(root, childrenstr, conditionstr):
+ ret = []
+ queue = [root]
+ while queue:
+ current = queue.pop()
+ children = eval('current'+childrenstr)
+ for child in children:
+ if eval('child'+conditionstr):
+ ret.append(current)
+ #we know have a tree, so there are no back-edges etc, so no checking of that kind is
+ #necessary
+ queue += children
+ return ret