diff options
author | 2010-12-29 17:27:33 -0500 | |
---|---|---|
committer | 2010-12-29 17:27:33 -0500 | |
commit | 93dfb8e3003b483c1041c6f7b4ff293935aeb7c0 (patch) | |
tree | 6b0e336f664fa519cad164756b68840e56b50b16 | |
parent | c8209d01f9ddf4c6670caee08073924cb33e447f (diff) |
Inheritence now happens at all levels on reading of config files. No additional resolution of
inheritances should be necessary. RCOH
-rw-r--r-- | LightInstallation.py | 5 | ||||
-rw-r--r-- | layouts/50PixelStrip.xml | 8 | ||||
-rw-r--r-- | layouts/BasicSixStrip.xml | 42 | ||||
-rw-r--r-- | tests/testdata/inheritanceTEST.xml | 21 | ||||
-rw-r--r-- | tests/testdata/inheritanceTESTout.xml | 14 | ||||
-rw-r--r-- | tests/testdata/inheritanceTRUTH.xml | 14 | ||||
-rw-r--r-- | util/Config.py | 18 | ||||
-rw-r--r-- | util/Search.py | 14 |
8 files changed, 93 insertions, 43 deletions
diff --git a/LightInstallation.py b/LightInstallation.py index 8b2d059..9f9f6da 100644 --- a/LightInstallation.py +++ b/LightInstallation.py @@ -26,6 +26,7 @@ class LightInstallation: compReg.initRegistry() compReg.registerComponent(self.screen, 'Screen') #TODO: move to constants file config = configGetter.loadConfigFile(configFileName) + pdb.set_trace() #read configs from xml rendererConfig = config.find('RendererConfiguration') pixelConfig = config.find('PixelConfiguration') @@ -88,11 +89,7 @@ class LightInstallation: def initializeComponent(self, config): components = [] if config != None: - config = configGetter.resolveConfigInheritance(config) for configItem in config.getchildren(): - configItem = configGetter.resolveConfigInheritance(configItem) #resolve - #inheritences. TODO: migrate to a recursive inheritence resolver that gets run on - #file-parse try: [module,className] = configItem.find('Class').text.split('.') except: diff --git a/layouts/50PixelStrip.xml b/layouts/50PixelStrip.xml new file mode 100644 index 0000000..beabb97 --- /dev/null +++ b/layouts/50PixelStrip.xml @@ -0,0 +1,8 @@ +<PixelStrip> + <Class>layouts.LineLayout</Class> + <Args> + <pixelToPixelSpacing>4</pixelToPixelSpacing> + <spacing>4</spacing> + <numPixels>50</numPixels> + </Args> +</PixelStrip> diff --git a/layouts/BasicSixStrip.xml b/layouts/BasicSixStrip.xml index 38c50d4..0d2d277 100644 --- a/layouts/BasicSixStrip.xml +++ b/layouts/BasicSixStrip.xml @@ -1,61 +1,43 @@ <PixelConfiguration> <PixelStrip> - <Class>layouts.LineLayout</Class> - <Args> + <InheritsFrom>layouts/50PixelStrip.xml</InheritsFrom> + <Args OverrideBehavior="Merge"> <Id>strip1</Id> - <pixelToPixelSpacing>4</pixelToPixelSpacing> - <spacing>4</spacing> - <numPixels>50</numPixels> <originLocation>(10,20)</originLocation> </Args> </PixelStrip> <PixelStrip> - <Class>layouts.LineLayout</Class> - <Args> + <InheritsFrom>layouts/50PixelStrip.xml</InheritsFrom> + <Args OverrideBehavior="Merge"> <Id>strip2</Id> - <pixelToPixelSpacing>4</pixelToPixelSpacing> - <spacing>4</spacing> - <numPixels>50</numPixels> <originLocation>(10,24)</originLocation> </Args> </PixelStrip> <PixelStrip> - <Class>layouts.LineLayout</Class> - <Args> + <InheritsFrom>layouts/50PixelStrip.xml</InheritsFrom> + <Args OverrideBehavior="Merge"> <Id>strip3</Id> - <pixelToPixelSpacing>4</pixelToPixelSpacing> - <spacing>4</spacing> - <numPixels>50</numPixels> <originLocation>(10,28)</originLocation> </Args> </PixelStrip> <PixelStrip> - <Class>layouts.LineLayout</Class> - <Args> + <InheritsFrom>layouts/50PixelStrip.xml</InheritsFrom> + <Args OverrideBehavior="Merge"> <Id>strip4</Id> - <pixelToPixelSpacing>4</pixelToPixelSpacing> - <spacing>4</spacing> - <numPixels>50</numPixels> <originLocation>(10,32)</originLocation> </Args> </PixelStrip> <PixelStrip> - <Class>layouts.LineLayout</Class> - <Args> + <InheritsFrom>layouts/50PixelStrip.xml</InheritsFrom> + <Args OverrideBehavior="Merge"> <Id>strip5</Id> - <pixelToPixelSpacing>4</pixelToPixelSpacing> - <spacing>4</spacing> - <numPixels>50</numPixels> <originLocation>(10,36)</originLocation> </Args> </PixelStrip> <PixelStrip> - <Class>layouts.LineLayout</Class> - <Args> + <InheritsFrom>layouts/50PixelStrip.xml</InheritsFrom> + <Args OverrideBehavior="Merge"> <Id>strip6</Id> - <pixelToPixelSpacing>4</pixelToPixelSpacing> - <spacing>4</spacing> - <numPixels>50</numPixels> <originLocation>(10,40)</originLocation> </Args> </PixelStrip> diff --git a/tests/testdata/inheritanceTEST.xml b/tests/testdata/inheritanceTEST.xml index c2efd71..d90e9af 100644 --- a/tests/testdata/inheritanceTEST.xml +++ b/tests/testdata/inheritanceTEST.xml @@ -1,3 +1,22 @@ <A> - <InheritsFrom>tests/testdata/aParent.xml</InheritsFrom> + <B> + <SubB> + <SubSubB> + <A> + <InheritsFrom>tests/testdata/aParent.xml</InheritsFrom> + </A> + </SubSubB> + </SubB> + <A> + <InheritsFrom>tests/testdata/aParent.xml</InheritsFrom> + </A> + </B> </A> + +<!-- +<Parent> + <A> + <InheritsFrom>tests/testdata/aParent.xml</InheritsFrom> + </A> +</Parent> +--> diff --git a/tests/testdata/inheritanceTESTout.xml b/tests/testdata/inheritanceTESTout.xml index ffa6667..e8fb228 100644 --- a/tests/testdata/inheritanceTESTout.xml +++ b/tests/testdata/inheritanceTESTout.xml @@ -1,4 +1,16 @@ <A> - <param1>1</param1> + <B> + <SubB> + <SubSubB> + <A> + <param1>1</param1> <param2>2</param2> +</A> + </SubSubB> + </SubB> + <A> + <param1>1</param1> + <param2>2</param2> +</A> + </B> </A>
\ No newline at end of file diff --git a/tests/testdata/inheritanceTRUTH.xml b/tests/testdata/inheritanceTRUTH.xml index ffa6667..e8fb228 100644 --- a/tests/testdata/inheritanceTRUTH.xml +++ b/tests/testdata/inheritanceTRUTH.xml @@ -1,4 +1,16 @@ <A> - <param1>1</param1> + <B> + <SubB> + <SubSubB> + <A> + <param1>1</param1> <param2>2</param2> +</A> + </SubSubB> + </SubB> + <A> + <param1>1</param1> + <param2>2</param2> +</A> + </B> </A>
\ No newline at end of file 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 |