diff options
author | Clark Williams <williams@redhat.com> | 2009-03-16 12:42:44 -0500 |
---|---|---|
committer | Clark Williams <williams@redhat.com> | 2009-03-16 12:42:44 -0500 |
commit | 7f44cc522eb6f1f84bcceb2e7ae3fb8f246a9b83 (patch) | |
tree | 7fd3f55c7c97367e0679428b5938de8e0127fd17 /rteval/xmlout.py | |
parent | e01928472913ec145739b37e013490fc4142af74 (diff) | |
parent | 09528548abe8911ec2931d579517d923736cb00a (diff) | |
download | rteval-7f44cc522eb6f1f84bcceb2e7ae3fb8f246a9b83.tar.gz rteval-7f44cc522eb6f1f84bcceb2e7ae3fb8f246a9b83.tar.xz rteval-7f44cc522eb6f1f84bcceb2e7ae3fb8f246a9b83.zip |
added fixtag() to remove whitespace; added section descriptions for dmi
Diffstat (limited to 'rteval/xmlout.py')
-rw-r--r-- | rteval/xmlout.py | 77 |
1 files changed, 75 insertions, 2 deletions
diff --git a/rteval/xmlout.py b/rteval/xmlout.py index 877974e..4884511 100644 --- a/rteval/xmlout.py +++ b/rteval/xmlout.py @@ -5,7 +5,7 @@ import sys import libxml2 import libxslt import codecs - +import re class XMLOut(object): '''Class to create XML output''' @@ -23,7 +23,7 @@ class XMLOut(object): self.xmldoc.freeDoc() - def __encode(self, value): + def __encode(self, value, tagmode = False): if type(value) is unicode: val = value elif type(value) is str: @@ -31,6 +31,10 @@ class XMLOut(object): else: val = unicode(str(value)) + if tagmode is True: + rx = re.compile(" ") + val = rx.sub("_", val) + # libxml2 uses UTF-8 internally and must have # all input as UTF-8. return val.encode('utf-8') @@ -46,6 +50,36 @@ class XMLOut(object): tmp = tagname.replace(' ', '_') return tmp.replace('\t', '_') + + def __parseToXML(self, node, data): + # All supported variable types needs to be set up + # here. TypeError exception will be raised on + # unknown types. + + t = type(data) + if t is unicode or t is str or t is int or t is float: + n = libxml2.newText(self.__encode(data)) + node.addChild(n) + elif t is bool: + v = data and "1" or "0" + n = libxml2.newText(self.__encode(v)) + node.addChild(n) + elif t is dict: + for (key, val) in data.iteritems(): + node2 = libxml2.newNode(self.__encode(self.parsedata_prefix + key, True)) + self.__parseToXML(node2, val) + node.addChild(node2) + elif t is tuple: + for v in data: + if type(v) is dict: + self.__parseToXML(node, v) + else: + n = libxml2.newNode(self.tuple_tagname) + self.__parseToXML(n, v) + node.addChild(n) + else: + raise TypeError, "unhandled type (%s) for value '%s'" % (type(data), unicode(data)) + def close(self): if self.status == 0: raise RuntimeError, "XMLOut: No XML document is created nor loaded" @@ -163,6 +197,19 @@ class XMLOut(object): self.__add_attributes(ntag, attributes) + def ParseData(self, tagname, data, attributes=None, tuple_tagname="tuples", prefix = ""): + if self.status != 1: + raise RuntimeError, "XMLOut: taggedvalue() cannot be called before NewReport() is called" + + self.tuple_tagname = self.__fixtag(tuple_tagname) + self.parsedata_prefix = prefix + + ntag = libxml2.newNode(self.__fixtag(tagname)) + self.__add_attributes(ntag, attributes) + self.__parseToXML(ntag, data) + self.currtag.addChild(ntag) + + if __name__ == '__main__': x = XMLOut('rteval', '0.6', None, 'UTF-8') x.NewReport() @@ -192,3 +239,29 @@ if __name__ == '__main__': x.LoadReport("latency.xml", True) x.Write("-") x.Write("-", "rteval_text.xsl") + x.close() + + ## Test new data parser ... it eats most data types + x.NewReport() + x.ParseData("ParseTest", "test string", {"type": "simple_string"}) + x.ParseData("ParseTest", 1234, {"type": "integer"}) + x.ParseData("ParseTest", 39.3904, {"type": "float"}) + x.ParseData("ParseTest", (11,22,33,44,55), {"type": "tuples"}) + x.ParseData("ParseTest", (99,88,77), {"type": "tuples", "comment": "Changed default tuple tag name"}, + "int_values") + test = {"var1": "value 1", + "var2": { "varA1": 1, + "pi": 3.1415926, + "varA3": (1, + 2, + {"test1": "val1"}, + (4.1,4.2,4.3), + 5), + "varA4": {'another_level': True, + 'another_value': "blabla"} + }, + "utf8 data": u'æøå', + u"løpe": True} + x.ParseData("ParseTest", test, {"type": "dict"}, prefix="test ") + x.close() + x.Write("-") |