Skip to content

Commit

Permalink
Merge pull request #34 from gisce/ns_support
Browse files Browse the repository at this point in the history
Support for namespaces
  • Loading branch information
ecarreras committed Feb 10, 2015
2 parents 1743900 + da421a8 commit 5f53275
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
12 changes: 8 additions & 4 deletions libcomxml/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
64 changes: 64 additions & 0 deletions tests/test_libcomxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<?xml version='1.0' encoding='UTF-8'?>\n"
self.xml += "<rss "
self.xml += "xmlns:opensearch=\"http://a9.com/-/spec/opensearch/1.1/\" "
self.xml += "xmlns:atom=\"http://www.w3.org/2005/Atom\" "
self.xml += "version=\"2.0\">"
self.xml += "<channel><link>http://example.com/New+York+history</link>"
self.xml += "<atom:link "
self.xml += "href=\"http://example.com/opensearchdescription.xml\" "
self.xml += "rel=\"search\" "
self.xml += "type=\"application/opensearchdescription+xml\"/>"
self.xml += "<opensearch:totalResults>4230000</opensearch:totalResults>"
self.xml += "</channel>"
self.xml += "</rss>"

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))

0 comments on commit 5f53275

Please sign in to comment.