commit b45cd4d7aa819ae4562375d963f44030c59c8183
Author: Radek Pazdera <rpazdera(a)redhat.com>
Date: Wed Aug 28 16:41:28 2013 +0200
XmlTemplates: Changing to support etree
The XML parser changed from DOM to etree. This patch changes the
XmlTemplates class to support etree. Some older code was also removed.
Alias and template functions processing is now handled separately.
Aliases are preprocessed before the document is parsed, while template
functions are expanded during the parsing.
Signed-off-by: Radek Pazdera <rpazdera(a)redhat.com>
Signed-off-by: Jiri Pirko <jiri(a)resnulli.us>
lnst/Common/XmlTemplates.py | 104 +++++++++++++++++++++++--------------------
1 files changed, 55 insertions(+), 49 deletions(-)
---
diff --git a/lnst/Common/XmlTemplates.py b/lnst/Common/XmlTemplates.py
index e1f0f86..daa1254 100644
--- a/lnst/Common/XmlTemplates.py
+++ b/lnst/Common/XmlTemplates.py
@@ -239,37 +239,63 @@ class XmlTemplates:
err = "'%s' is not defined here" % name
raise XmlTemplateError(err)
- def expand_dom(self, node):
- """
- Traverse DOM tree from `node' down and expand any
- templates along the way.
+ def process_aliases(self, element):
+ """ Expand aliases within an element and its children
+
+ This method will iterate through the element tree that is
+ passed and expand aliases in all the text content and
+ attributes.
"""
+ if element.text != None:
+ element.text = self.expand_aliases(element.text)
- if node.nodeType == node.ELEMENT_NODE:
- i = 0
- num_attributes = node.attributes.length
- while(i < num_attributes):
- attr = node.attributes.item(i)
- attr.value = self.expand_string(str(attr.value))
- i += 1
- elif node.nodeType == node.TEXT_NODE:
- node.data = self.expand_string(str(node.data))
+ if element.tail != None:
+ element.tail = self.expand_aliases(element.tail)
- for child in node.childNodes:
- self.expand_dom(child)
+ for name, value in element.attrib.iteritems():
+ element.set(name, self.expand_aliases(value))
- def expand_group(self, group):
- """
- Behaves exactly the same as the `expand' method, but it
- operates on a group of DOM nodes stored within a list,
- rather than a single node.
- """
+ if element.tag == "define":
+ for alias in element.getchildren():
+ name = alias.attrib["name"]
+ value = alias.attrib["value"]
+ self.define_alias(name, value)
+ parent = element.getparent()
+ parent.remove(element)
+ return
+
+ self.add_namespace_level()
+
+ for child in element.getchildren():
+ self.process_aliases(child)
+
+ self.drop_namespace_level()
+
+ def expand_aliases(self, string):
+ while True:
+ alias_match = re.search(self._alias_re, string)
+
+ if alias_match:
+ template = alias_match.group(0)
+ result = self._process_alias_template(template)
+ string = string.replace(template, result)
+ else:
+ break
+
+ return string
+
+ def _process_alias_template(self, string):
+ result = None
+
+ alias_match = re.match(self._alias_re, string)
+ if alias_match:
+ alias_name = alias_match.group(1)
+ result = self._find_definition(alias_name)
- for node in group:
- self.expand_dom(node)
+ return result
- def expand_string(self, string, node=None):
- """ Process a string and expand it into a XmlTemplateString.
"""
+ def expand_functions(self, string, node=None):
+ """ Process a string and expand it into a XmlTemplateString
"""
parts = self._partition_string(string)
value = XmlTemplateString(node=node)
@@ -282,9 +308,8 @@ class XmlTemplates:
def _partition_string(self, string):
""" Process templates in a string
- This method will process and expand all templates contained
- within a string. It handles both aliases and template
- function.
+ This method will process and expand all template functions
+ in a string.
The function returns an array of string partitions and
unresolved template functions for further processing.
@@ -292,16 +317,6 @@ class XmlTemplates:
result = None
- while True:
- alias_match = re.search(self._alias_re, string)
-
- if alias_match:
- template = alias_match.group(0)
- result = self._process_alias_template(template)
- string = string.replace(template, result)
- else:
- break
-
func_match = re.search(self._func_re, string)
if func_match:
prefix = string[0:func_match.start(0)]
@@ -315,16 +330,6 @@ class XmlTemplates:
return [string]
- def _process_alias_template(self, string):
- result = None
-
- alias_match = re.match(self._alias_re, string)
- if alias_match:
- alias_name = alias_match.group(1)
- result = self._find_definition(alias_name)
-
- return result
-
def _process_func_template(self, string):
func_match = re.match(self._func_re, string)
if func_match:
@@ -350,4 +355,5 @@ class XmlTemplates:
func = self._func_map[func_name](param_values, self._machines)
return func
else:
- raise RuntimeError("The passed string is not a template
function.")
+ msg = "The passed string is not a template function."
+ raise XmlTemplateError(msg)