summaryrefslogtreecommitdiffstats
path: root/rteval
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2013-03-13 19:56:22 +0100
committerDavid Sommerseth <davids@redhat.com>2013-03-13 19:56:22 +0100
commit99253169b347346a96cc08d767f04dea615db79d (patch)
treebbf4ecc2bf54f234f27013e121f61ff48dc909d7 /rteval
parent27783aa01e41bce0e2b7b6e3db5d4b453f438ffe (diff)
downloadrteval-99253169b347346a96cc08d767f04dea615db79d.tar.gz
rteval-99253169b347346a96cc08d767f04dea615db79d.tar.xz
rteval-99253169b347346a96cc08d767f04dea615db79d.zip
Migrated from libxslt to lxml
To avoid depending on libxslt-python, use the more standard Python lxml module for XSLT processing. Signed-off-by: David Sommerseth <davids@redhat.com>
Diffstat (limited to 'rteval')
-rw-r--r--rteval/sysinfo/dmi.py33
-rw-r--r--rteval/xmlout.py49
2 files changed, 60 insertions, 22 deletions
diff --git a/rteval/sysinfo/dmi.py b/rteval/sysinfo/dmi.py
index b3cea0d..84ca97f 100644
--- a/rteval/sysinfo/dmi.py
+++ b/rteval/sysinfo/dmi.py
@@ -26,8 +26,8 @@
#
import sys, os
-import libxml2, libxslt
-from rteval import rtevalConfig
+import libxml2, lxml.etree
+from rteval import rtevalConfig, xmlout
from rteval.Log import Log
try:
@@ -64,7 +64,7 @@ class DMIinfo(object):
'''class used to obtain DMI info via python-dmidecode'''
def __init__(self, config, logger):
- self.__version = '0.4'
+ self.__version = '0.5'
if not dmidecode_loaded:
logger.log(Log.DEBUG|Log.WARN, "No dmidecode module found, ignoring DMI tables")
@@ -74,17 +74,23 @@ class DMIinfo(object):
self.__fake = False
self.__dmixml = dmidecode.dmidecodeXML()
- xsltdoc = self.__load_xslt('rteval_dmi.xsl')
- self.xsltparser = libxslt.parseStylesheetDoc(xsltdoc)
+ self.__xsltparser = self.__load_xslt('rteval_dmi.xsl')
def __load_xslt(self, fname):
- if os.path.isfile(fname):
- return libxml2.parseFile(fname)
+ xsltfile = None
+ if os.path.exists(fname):
+ xsltfile = open(fname, "r")
elif rtevalConfig.default_config_search([fname], os.path.isfile):
- return libxml2.parseFile(rtevalConfig.default_config_search([fname], os.path.isfile))
- else:
- raise RuntimeError('Could not locate XSLT template for DMI data (%s/%s)' % (fname))
+ xsltfile = open(rtevalConfig.default_config_search([fname], os.path.isfile), "r")
+
+ if xsltfile:
+ xsltdoc = lxml.etree.parse(xsltfile)
+ ret = lxml.etree.XSLT(xsltdoc)
+ xsltfile.close()
+ return ret
+
+ raise RuntimeError, 'Could not locate XSLT template for DMI data (%s)' % (self.sharedir + '/' + fname)
def MakeReport(self):
@@ -95,8 +101,9 @@ class DMIinfo(object):
rep_n.newProp("not_available", "1")
else:
self.__dmixml.SetResultType(dmidecode.DMIXML_DOC)
- resdoc = self.xsltparser.applyStylesheet(self.__dmixml.QuerySection('all'), None)
- dmi_n = resdoc.getRootElement().copyNode(1)
+ dmiqry = xmlout.convert_libxml2_to_lxml_doc(self.__dmixml.QuerySection('all'))
+ resdoc = self.__xsltparser(dmiqry)
+ dmi_n = xmlout.convert_lxml_to_libxml2_nodes(resdoc.getroot())
rep_n.addChild(dmi_n)
return rep_n
@@ -107,7 +114,7 @@ def unit_test(rootdir):
class unittest_ConfigDummy(object):
def __init__(self, rootdir):
- self.config = {'installdir': '%s/rteval'}
+ self.config = {'installdir': '/usr/share/rteval'}
self.__update_vars()
def __update_vars(self):
diff --git a/rteval/xmlout.py b/rteval/xmlout.py
index 454a452..ddc2964 100644
--- a/rteval/xmlout.py
+++ b/rteval/xmlout.py
@@ -26,11 +26,35 @@
import os
import sys
import libxml2
-import libxslt
+import lxml.etree
import codecs
import re
from string import maketrans
+
+def convert_libxml2_to_lxml_doc(inxml):
+ "Converts a libxml2.xmlDoc into a lxml.etree document object"
+
+ if not isinstance(inxml, libxml2.xmlDoc):
+ raise TypeError('Function requires an libxml2.xmlDoc as input')
+
+ root = inxml.getRootElement()
+ ret = lxml.etree.XML(root.serialize('UTF-8'))
+ del root
+ return ret
+
+
+
+def convert_lxml_to_libxml2_nodes(inlxml):
+ "Converts a lxml.etree elements tree into a libxml2.xmlNode object"
+
+ if not isinstance(inlxml,lxml.etree._Element) and not isinstance(inlxml, lxml.etree._XSLTResultTree):
+ raise TypeError('Function requires an lxml.etree object as input')
+
+ return libxml2.parseDoc(lxml.etree.tostring(inlxml)).getRootElement()
+
+
+
class XMLOut(object):
'''Class to create XML output'''
def __init__(self, roottag, version, attr = None, encoding='UTF-8'):
@@ -171,6 +195,7 @@ class XMLOut(object):
self.status = 2 # Confirm that we have loaded a report from file
+
def Write(self, filename, xslt = None):
if self.status != 2 and self.status != 3:
raise RuntimeError, "XMLOut: XML document is not closed"
@@ -181,8 +206,10 @@ class XMLOut(object):
return
else:
# Load XSLT file and prepare the XSLT parser
- xsltdoc = libxml2.parseFile(xslt)
- parser = libxslt.parseStylesheetDoc(xsltdoc)
+ xsltfile = open(xslt, 'r')
+ xsltdoc = lxml.etree.parse(xsltfile)
+ parser = lxml.etree.XSLT(xsltdoc)
+ xsltfile.close()
# imitate libxml2's filename interpretation
if filename != "-":
@@ -192,18 +219,22 @@ class XMLOut(object):
#
# Parse XML+XSLT and write the result to file
#
- resdoc = parser.applyStylesheet(self.xmldoc, None)
- # Decode the result string according to the charset declared in the XSLT file
- xsltres = parser.saveResultToString(resdoc).decode(parser.encoding())
+ xmldoc = convert_libxml2_to_lxml_doc(self.xmldoc)
+ resdoc = parser(xmldoc)
+
# Write the file with the requested output encoding
- dstfile.write(xsltres.encode(self.encoding))
+ dstfile.write(unicode(resdoc).encode(self.encoding))
if dstfile != sys.stdout:
dstfile.close()
# Clean up
- resdoc.freeDoc()
- xsltdoc.freeDoc()
+ del resdoc
+ del xsltdoc
+ del parser
+ del xsltfile
+ del xmldoc
+
def GetXMLdocument(self):
if self.status != 2 and self.status != 3: