With the changes in recipe parser, the slave machine parser has been
changed to use etree as well. Again, due to validation using the schema,
the code is now much simpler.
Signed-off-by: Radek Pazdera <rpazdera(a)redhat.com>
---
lnst/Controller/SlaveMachineParse.py | 72 -----------------------------------
lnst/Controller/SlaveMachineParser.py | 69 +++++++++++++++++++++++++++++++++
lnst/Controller/SlavePool.py | 18 ++-------
3 files changed, 73 insertions(+), 86 deletions(-)
delete mode 100644 lnst/Controller/SlaveMachineParse.py
create mode 100644 lnst/Controller/SlaveMachineParser.py
diff --git a/lnst/Controller/SlaveMachineParse.py b/lnst/Controller/SlaveMachineParse.py
deleted file mode 100644
index 709c478..0000000
--- a/lnst/Controller/SlaveMachineParse.py
+++ /dev/null
@@ -1,72 +0,0 @@
-"""
-This module defines SlaveMachineParse class useful to parse XML machine
-descriptions for the slave pool
-
-Copyright 2013 Red Hat, Inc.
-Licensed under the GNU General Public License, version 2 as
-published by the Free Software Foundation; see COPYING for details.
-"""
-
-__author__ = """
-rpazdera(a)redhat.com (Radek Pazdera)
-"""
-
-import logging
-import os
-import re
-from lnst.Controller.RecipeParse import ParamsParse
-from lnst.Common.XmlParser import LnstParser
-from lnst.Common.XmlProcessing import XmlDomTreeInit, XmlProcessingError
-from lnst.Common.XmlProcessing import XmlData, XmlCollection
-
-class SlaveMachineError(XmlProcessingError):
- pass
-
-class SlaveMachineParse(LnstParser):
- def parse(self, node):
- self._data = XmlData(node)
- scheme = {"params": self._params,
- "interfaces": self._interfaces}
- self._process_child_nodes(node, scheme)
- return self._data
-
- def _params(self, node, params):
- if "params" in self._data:
- msg = "Only a single <params> child allowed under
<slavemachine>."
- raise SlaveMachineError(msg, node)
-
- subparser = ParamsParse(self)
- self._data["params"] = subparser.parse(node)
-
- def _interfaces(self, node, params):
- if not "interfaces" in self._data:
- self._data["interfaces"] = XmlCollection(node)
- else:
- msg = "Only a single <interfaces> child allowed under
<slavemachine>."
- raise SlaveMachineError(msg, node)
-
- scheme = {"eth": self._eth}
- self._process_child_nodes(node, scheme)
-
- def _eth(self, node, params):
- machine = self._data
-
- iface = XmlData(node)
- iface["id"] = self._get_attribute(node, "id")
- iface["network"] = self._get_attribute(node, "network")
- iface["type"] = "eth"
-
- # parse interface parameters
- scheme = {"params": self._iface_params}
- params = {"iface": iface}
- self._process_child_nodes(node, scheme, params)
-
- machine["interfaces"].append(iface)
-
- def _iface_params(self, node, params):
- if "params" in params["iface"]:
- msg = "Only a single <params> child allowed under
<interface>."
- raise SlaveMachineError(msg, node)
-
- subparser = ParamsParse(self)
- params["iface"]["params"] = subparser.parse(node)
diff --git a/lnst/Controller/SlaveMachineParser.py
b/lnst/Controller/SlaveMachineParser.py
new file mode 100644
index 0000000..02374cb
--- /dev/null
+++ b/lnst/Controller/SlaveMachineParser.py
@@ -0,0 +1,69 @@
+"""
+This module defines SlaveMachineParser class useful to parse XML machine
+descriptions for the slave pool
+
+Copyright 2013 Red Hat, Inc.
+Licensed under the GNU General Public License, version 2 as
+published by the Free Software Foundation; see COPYING for details.
+"""
+
+__author__ = """
+rpazdera(a)redhat.com (Radek Pazdera)
+"""
+
+import logging
+import os
+import re
+from lxml import etree
+from lnst.Common.XmlParser import XmlParser
+from lnst.Common.XmlProcessing import XmlProcessingError, XmlData, XmlCollection
+
+class SlaveMachineError(XmlProcessingError):
+ pass
+
+class SlaveMachineParser(XmlParser):
+ def __init__(self, sm_path):
+ super(SlaveMachineParser, self).__init__("schema-sm.rng", sm_path)
+
+ def _process(self, sm_tag):
+ sm = XmlData(sm_tag)
+
+ # params
+ params_tag = sm_tag.find("params")
+ params = self._process_params(params_tag)
+ if len(params) > 0:
+ sm["params"] = params
+
+ # interfaces
+ interfaces_tag = sm_tag.find("interfaces")
+ if interfaces_tag is not None and len(interfaces_tag) > 0:
+ sm["interfaces"] = XmlCollection(interfaces_tag)
+ for eth_tag in interfaces_tag:
+ interface = self._process_interface(eth_tag)
+ sm["interfaces"].append(interface)
+
+ return sm
+
+ def _process_params(self, params_tag):
+ params = XmlCollection(params_tag)
+ if params_tag is not None:
+ for param_tag in params_tag:
+ param = XmlData(param_tag)
+ param["name"] = self._get_attribute(param_tag,
"name")
+ param["value"] = self._get_attribute(param_tag,
"value")
+ params.append(param)
+ return params
+
+ def _process_interface(self, iface_tag):
+ iface = XmlData(iface_tag)
+ iface["id"] = self._get_attribute(iface_tag, "id")
+ iface["network"] = self._get_attribute(iface_tag, "network")
+ iface["type"] = "eth"
+
+ # interface parameters
+ params_tag = iface_tag.find("params")
+ params = self._process_params(params_tag)
+ if len(params) > 0:
+ iface["params"] = params
+
+ return iface
diff --git a/lnst/Controller/SlavePool.py b/lnst/Controller/SlavePool.py
index e8baf83..5872b0c 100644
--- a/lnst/Controller/SlavePool.py
+++ b/lnst/Controller/SlavePool.py
@@ -18,13 +18,12 @@ import os
import re
import copy
from xml.dom import minidom
+from lnst.Common.Config import lnst_config
from lnst.Common.NetUtils import normalize_hwaddr
from lnst.Common.NetUtils import test_tcp_connection
-from lnst.Common.XmlProcessing import XmlDomTreeInit
from lnst.Common.XmlProcessing import XmlProcessingError, XmlData
from lnst.Controller.Machine import Machine
-from lnst.Common.Config import lnst_config
-from lnst.Controller.SlaveMachineParse import SlaveMachineParse
+from lnst.Controller.SlaveMachineParse import SlaveMachineParser
from lnst.Controller.SlaveMachineParse import SlaveMachineError
class SlavePool:
@@ -56,20 +55,11 @@ class SlavePool:
def add_file(self, filepath):
if os.path.isfile(filepath) and re.search("\.xml$", filepath, re.I):
- dom_init = XmlDomTreeInit()
- dom = dom_init.parse_file(filepath)
-
dirname, basename = os.path.split(filepath)
-
- parser = SlaveMachineParse()
- parser.set_include_root(dirname)
- parser.disable_events()
-
m_id = re.sub("\.[xX][mM][lL]$", "", basename)
- slavemachine = dom.getElementsByTagName("slavemachine")[0]
- xml_data = parser.parse(slavemachine)
-
+ parser = SlaveMachineParser(filepath)
+ xml_data = parser.parse()
machine_spec = self._process_machine_xml_data(m_id, xml_data)
if self._pool_checks:
--
1.8.3.1