1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2012 OpenStack LLC
5
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6
# not use this file except in compliance with the License. You may obtain
7
# a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
# License for the specific language governing permissions and limitations
18
Dict <--> XML de/serializer.
20
The identity API prefers attributes over elements, so we serialize that way
21
by convention, with a few hardcoded exceptions.
25
from lxml import etree
29
DOCTYPE = '<?xml version="1.0" encoding="UTF-8"?>'
30
XMLNS = 'http://docs.openstack.org/identity/api/v2.0'
34
"""Deserialize XML to a dictionary."""
38
deserializer = XmlDeserializer()
39
return deserializer(xml)
42
def to_xml(d, xmlns=None):
43
"""Serialize a dictionary to XML."""
47
serialize = XmlSerializer()
48
return serialize(d, xmlns)
51
class XmlDeserializer(object):
52
def __call__(self, xml_str):
53
"""Returns a dictionary populated by decoding the given xml string."""
54
dom = etree.fromstring(xml_str.strip())
55
return self.walk_element(dom)
59
"""Remove the namespace from the tagname.
61
TODO(dolph): We might care about the namespace at some point.
63
>>> XmlDeserializer._tag_name('{xmlNamespace}tagName')
67
m = re.search('[^}]+$', tag)
68
return m.string[m.start():]
70
def walk_element(self, element):
71
"""Populates a dictionary by walking an etree element."""
73
for k, v in element.attrib.iteritems():
74
# boolean-looking attributes become booleans in JSON
84
if element.text is not None:
85
text = element.text.strip()
87
# current spec does not have attributes on an element with text
88
values = values or text or {}
90
for child in [self.walk_element(x) for x in element]:
91
values = dict(values.items() + child.items())
93
return {XmlDeserializer._tag_name(element.tag): values}
96
class XmlSerializer(object):
97
def __call__(self, d, xmlns=None):
98
"""Returns an xml etree populated by the given dictionary.
100
Optionally, namespace the etree by specifying an ``xmlns``.
103
# FIXME(dolph): skipping links for now
108
assert len(d.keys()) == 1, ('Cannot encode more than one root '
109
'element: %s' % d.keys())
111
# name the root dom element
114
# only the root dom element gets an xlmns
115
root = etree.Element(name, xmlns=(xmlns or XMLNS))
117
self.populate_element(root, d[name])
119
# TODO(dolph): you can get a doctype from lxml, using ElementTrees
120
return '%s\n%s' % (DOCTYPE, etree.tostring(root, pretty_print=True))
122
def _populate_list(self, element, k, v):
123
"""Populates an element with a key & list value."""
124
# spec has a lot of inconsistency here!
127
if k == 'media-types':
128
# xsd compliance: <media-types> contains <media-type>s
129
# find an existing <media-types> element or make one
130
container = element.find('media-types')
131
if container is None:
132
container = etree.Element(k)
133
element.append(container)
135
elif k == 'serviceCatalog':
136
# xsd compliance: <serviceCatalog> contains <service>s
137
container = etree.Element(k)
138
element.append(container)
140
elif k == 'values' and element.tag[-1] == 's':
141
# OS convention is to contain lists in a 'values' element,
142
# so the list itself can have attributes, which is
144
name = element.tag[:-1]
151
child = etree.Element(name)
152
self.populate_element(child, item)
153
container.append(child)
155
def _populate_dict(self, element, k, v):
156
"""Populates an element with a key & dictionary value."""
157
child = etree.Element(k)
158
self.populate_element(child, v)
159
element.append(child)
161
def _populate_bool(self, element, k, v):
162
"""Populates an element with a key & boolean value."""
163
# booleans are 'true' and 'false'
164
element.set(k, unicode(v).lower())
166
def _populate_str(self, element, k, v):
167
"""Populates an element with a key & string value."""
168
if k in ['description']:
169
# always becomes an element
170
child = etree.Element(k)
171
child.text = unicode(v)
172
element.append(child)
174
# add attributes to the current element
175
element.set(k, unicode(v))
177
def _populate_number(self, element, k, v):
178
"""Populates an element with a key & numeric value."""
179
# numbers can be handled as strings
180
self._populate_str(element, k, v)
182
def populate_element(self, element, value):
183
"""Populates an etree with the given value."""
184
if isinstance(value, list):
185
self._populate_sequence(element, value)
186
elif isinstance(value, dict):
187
self._populate_tree(element, value)
189
def _populate_sequence(self, element, l):
190
"""Populates an etree with a sequence of elements, given a list."""
191
# xsd compliance: child elements are singular: <users> has <user>s
193
if element.tag[-1] == 's':
194
name = element.tag[:-1]
197
child = etree.Element(name)
198
self.populate_element(child, item)
199
element.append(child)
201
def _populate_tree(self, element, d):
202
"""Populates an etree with attributes & elements, given a dict."""
203
for k, v in d.iteritems():
204
if isinstance(v, dict):
205
self._populate_dict(element, k, v)
206
elif isinstance(v, list):
207
self._populate_list(element, k, v)
208
elif isinstance(v, bool):
209
self._populate_bool(element, k, v)
210
elif isinstance(v, basestring):
211
self._populate_str(element, k, v)
212
elif type(v) in [int, float, long, complex]:
213
self._populate_number(element, k, v)