X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=sfa%2Futil%2Fxml.py;h=9c4a80f01866b0bc676368cd1af3a6d648c5ac43;hb=30d9951e075d93127c3909dcb41be09b420b3525;hp=d6734e630d0484a7ae57144f3415a9c80cc0ef3a;hpb=3d9943d1e5f993408b618cde29ff31b3585e5cef;p=sfa.git diff --git a/sfa/util/xml.py b/sfa/util/xml.py index d6734e63..9c4a80f0 100755 --- a/sfa/util/xml.py +++ b/sfa/util/xml.py @@ -1,10 +1,11 @@ #!/usr/bin/python -from types import StringTypes from lxml import etree from StringIO import StringIO from sfa.util.faults import InvalidXML from sfa.rspecs.elements.element import Element +from sfa.util.py23 import StringType + # helper functions to help build xpaths class XpathFilter: @staticmethod @@ -20,7 +21,8 @@ class XpathFilter: return xpath @staticmethod - def xpath(filter={}): + def xpath(filter=None): + if filter is None: filter={} xpath = "" if filter: filter_list = [] @@ -78,11 +80,12 @@ class XmlElement: def getparent(self): return XmlElement(self.element.getparent(), self.namespaces) - def get_instance(self, instance_class=None, fields=[]): + def get_instance(self, instance_class=None, fields=None): """ Returns an instance (dict) of this xml element. The instance holds a reference to this xml element. """ + if fields is None: fields=[] if not instance_class: instance_class = Element if not fields and hasattr(instance_class, 'fields'): @@ -97,11 +100,12 @@ class XmlElement: instance[field] = self.attrib[field] return instance - def add_instance(self, name, instance, fields=[]): + def add_instance(self, name, instance, fields=None): """ Adds the specifed instance(s) as a child element of this xml element. """ + if fields is None: fields=[] if not fields and hasattr(instance, 'keys'): fields = instance.keys() elem = self.add_element(name) @@ -150,7 +154,7 @@ class XmlElement: # are redirected on self.element def __getattr__ (self, name): if not hasattr(self.element, name): - raise AttributeError, name + raise AttributeError(name) return getattr(self.element, name) class XML: @@ -179,7 +183,7 @@ class XML: # 'rspec' file doesnt exist. 'rspec' is proably an xml string try: tree = etree.parse(StringIO(xml), parser) - except Exception, e: + except Exception as e: raise InvalidXML(str(e)) root = tree.getroot() self.namespaces = dict(root.nsmap) @@ -236,7 +240,7 @@ class XML: d=d.copy() # looks like iteritems won't stand side-effects for k in d.keys(): - if not isinstance(d[k],StringTypes): + if not isinstance(d[k], StringType): del d[k] element.attrib.update(d)