Skip to content

Commit 5f53275

Browse files
committed
Merge pull request #34 from gisce/ns_support
Support for namespaces
2 parents 1743900 + da421a8 commit 5f53275

2 files changed

Lines changed: 72 additions & 4 deletions

File tree

libcomxml/core/__init__.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class XmlField(Field):
7676
"""Field with XML capabilities
7777
"""
7878
def __init__(self, name, value=None, parent=None, attributes=None,
79-
rep=None):
79+
rep=None, namespace=None):
8080
"""
8181
:param name: the name of the field super(Cabecera, self).__init__(name, root)
8282
:param value: the value of the field
@@ -86,6 +86,7 @@ def __init__(self, name, value=None, parent=None, attributes=None,
8686
"""
8787
self.parent = parent
8888
self.xml_enc = get_xml_default_encoding()
89+
self.namespace = namespace
8990

9091
super(XmlField, self).__init__(name, value=value,
9192
attributes=attributes, rep=rep)
@@ -125,13 +126,16 @@ def element(self, parent=None):
125126
:param parent: an etree Element to be used as parent for this one
126127
"""
127128
if parent is not None:
128-
ele = etree.SubElement(parent, self.name, **self.attributes)
129+
if self.namespace:
130+
name = '{%s}%s' % (self.namespace, self.name)
131+
else:
132+
name = self.name
133+
ele = etree.SubElement(parent, name, **self.attributes)
129134
else:
130135
ele = etree.Element(self.name, **self.attributes)
131136
ele = self._parse_value(ele)
132137
return ele
133138

134-
135139
def __str__(self):
136140
"""Returns the XML repr of the field
137141
@@ -230,7 +234,7 @@ def build_tree(self):
230234
return
231235
self.doc_root = self.root.element()
232236
for key in self.sorted_fields():
233-
if not key in self._fields:
237+
if key not in self._fields:
234238
continue
235239
field = self._fields[key]
236240
if field != self.root:

tests/test_libcomxml.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,67 @@ def __init__(self):
213213
l.build_tree()
214214

215215
self.assertEqual(self.xml, str(l))
216+
217+
218+
class Namespaces(unittest.TestCase):
219+
def setUp(self):
220+
self.xml = "<?xml version='1.0' encoding='UTF-8'?>\n"
221+
self.xml += "<rss "
222+
self.xml += "xmlns:opensearch=\"http://a9.com/-/spec/opensearch/1.1/\" "
223+
self.xml += "xmlns:atom=\"http://www.w3.org/2005/Atom\" "
224+
self.xml += "version=\"2.0\">"
225+
self.xml += "<channel><link>http://example.com/New+York+history</link>"
226+
self.xml += "<atom:link "
227+
self.xml += "href=\"http://example.com/opensearchdescription.xml\" "
228+
self.xml += "rel=\"search\" "
229+
self.xml += "type=\"application/opensearchdescription+xml\"/>"
230+
self.xml += "<opensearch:totalResults>4230000</opensearch:totalResults>"
231+
self.xml += "</channel>"
232+
self.xml += "</rss>"
233+
234+
def test_namesapces_root(self):
235+
236+
237+
NAMESPACES = {
238+
'opensearch': 'http://a9.com/-/spec/opensearch/1.1/',
239+
'atom': 'http://www.w3.org/2005/Atom'
240+
}
241+
242+
class Channel(XmlModel):
243+
244+
_sort_order = ('link', 'atom_link', 'os_total_results')
245+
246+
def __init__(self):
247+
self.channel = XmlField('channel')
248+
self.link = XmlField('link')
249+
self.atom_link = XmlField(
250+
'link', namespace=NAMESPACES['atom']
251+
)
252+
self.os_total_results = XmlField(
253+
'totalResults', namespace=NAMESPACES['opensearch']
254+
)
255+
super(Channel, self).__init__('Channel', 'channel', drop_empty=False)
256+
257+
class Rss(XmlModel):
258+
259+
_sort_order = ('channel', )
260+
261+
def __init__(self):
262+
self.tag = XmlField('rss', attributes={
263+
'version': '2.0', 'nsmap': NAMESPACES
264+
})
265+
self.channel = Channel()
266+
super(Rss, self).__init__('RSS', 'tag', drop_empty=False)
267+
268+
rss = Rss()
269+
rss.channel.atom_link.attributes.update({
270+
'rel': 'search',
271+
'type': 'application/opensearchdescription+xml',
272+
'href': 'http://example.com/opensearchdescription.xml'
273+
})
274+
rss.channel.feed({
275+
'link': 'http://example.com/New+York+history',
276+
'os_total_results': 4230000,
277+
})
278+
rss.build_tree()
279+
self.assertEqual(self.xml, str(rss))

0 commit comments

Comments
 (0)