diff --git a/libcomxml/core/__init__.py b/libcomxml/core/__init__.py index d50e665..79214a2 100644 --- a/libcomxml/core/__init__.py +++ b/libcomxml/core/__init__.py @@ -76,7 +76,7 @@ class XmlField(Field): """Field with XML capabilities """ def __init__(self, name, value=None, parent=None, attributes=None, - rep=None): + rep=None, namespace=None): """ :param name: the name of the field super(Cabecera, self).__init__(name, root) :param value: the value of the field @@ -86,6 +86,7 @@ def __init__(self, name, value=None, parent=None, attributes=None, """ self.parent = parent self.xml_enc = get_xml_default_encoding() + self.namespace = namespace super(XmlField, self).__init__(name, value=value, attributes=attributes, rep=rep) @@ -125,13 +126,16 @@ def element(self, parent=None): :param parent: an etree Element to be used as parent for this one """ if parent is not None: - ele = etree.SubElement(parent, self.name, **self.attributes) + if self.namespace: + name = '{%s}%s' % (self.namespace, self.name) + else: + name = self.name + ele = etree.SubElement(parent, name, **self.attributes) else: ele = etree.Element(self.name, **self.attributes) ele = self._parse_value(ele) return ele - def __str__(self): """Returns the XML repr of the field @@ -230,7 +234,7 @@ def build_tree(self): return self.doc_root = self.root.element() for key in self.sorted_fields(): - if not key in self._fields: + if key not in self._fields: continue field = self._fields[key] if field != self.root: diff --git a/tests/test_libcomxml.py b/tests/test_libcomxml.py index 5c912bf..90b31ba 100644 --- a/tests/test_libcomxml.py +++ b/tests/test_libcomxml.py @@ -213,3 +213,67 @@ def __init__(self): l.build_tree() self.assertEqual(self.xml, str(l)) + + +class Namespaces(unittest.TestCase): + def setUp(self): + self.xml = "\n" + self.xml += "" + self.xml += "http://example.com/New+York+history" + self.xml += "" + self.xml += "4230000" + self.xml += "" + self.xml += "" + + def test_namesapces_root(self): + + + NAMESPACES = { + 'opensearch': 'http://a9.com/-/spec/opensearch/1.1/', + 'atom': 'http://www.w3.org/2005/Atom' + } + + class Channel(XmlModel): + + _sort_order = ('link', 'atom_link', 'os_total_results') + + def __init__(self): + self.channel = XmlField('channel') + self.link = XmlField('link') + self.atom_link = XmlField( + 'link', namespace=NAMESPACES['atom'] + ) + self.os_total_results = XmlField( + 'totalResults', namespace=NAMESPACES['opensearch'] + ) + super(Channel, self).__init__('Channel', 'channel', drop_empty=False) + + class Rss(XmlModel): + + _sort_order = ('channel', ) + + def __init__(self): + self.tag = XmlField('rss', attributes={ + 'version': '2.0', 'nsmap': NAMESPACES + }) + self.channel = Channel() + super(Rss, self).__init__('RSS', 'tag', drop_empty=False) + + rss = Rss() + rss.channel.atom_link.attributes.update({ + 'rel': 'search', + 'type': 'application/opensearchdescription+xml', + 'href': 'http://example.com/opensearchdescription.xml' + }) + rss.channel.feed({ + 'link': 'http://example.com/New+York+history', + 'os_total_results': 4230000, + }) + rss.build_tree() + self.assertEqual(self.xml, str(rss))