blob: cb24917c9d7b7a4aa036e0b3e383e0d096bb8b35 [file] [log] [blame]
Dan Smithba6cb162012-08-14 07:22:42 -07001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2#
Kurt Taylor6a6f5be2013-04-02 18:53:47 -04003# Copyright 2012 IBM Corp.
Dan Smithba6cb162012-08-14 07:22:42 -07004# All Rights Reserved.
5#
6# Licensed under the Apache License, Version 2.0 (the "License"); you may
7# not use this file except in compliance with the License. You may obtain
8# a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15# License for the specific language governing permissions and limitations
16# under the License.
17
Dan Smithba6cb162012-08-14 07:22:42 -070018XMLNS_11 = "http://docs.openstack.org/compute/api/v1.1"
19
20
21# NOTE(danms): This is just a silly implementation to help make generating
22# XML faster for prototyping. Could be replaced with proper etree gorp
23# if desired
24class Element(object):
25 def __init__(self, element_name, *args, **kwargs):
26 self.element_name = element_name
27 self._attrs = kwargs
28 self._elements = list(args)
29
30 def add_attr(self, name, value):
31 self._attrs[name] = value
32
33 def append(self, element):
34 self._elements.append(element)
35
36 def __str__(self):
37 args = " ".join(['%s="%s"' % (k, v) for k, v in self._attrs.items()])
38 string = '<%s %s' % (self.element_name, args)
39 if not self._elements:
40 string += '/>'
41 return string
42
43 string += '>'
44
45 for element in self._elements:
46 string += str(element)
47
48 string += '</%s>' % self.element_name
49
50 return string
51
52 def __getitem__(self, name):
53 for element in self._elements:
54 if element.element_name == name:
55 return element
56 raise KeyError("No such element `%s'" % name)
57
58 def __getattr__(self, name):
59 if name in self._attrs:
60 return self._attrs[name]
61 return object.__getattr__(self, name)
62
63 def attributes(self):
64 return self._attrs.items()
65
66 def children(self):
67 return self._elements
68
69
70class Document(Element):
71 def __init__(self, *args, **kwargs):
72 if 'version' not in kwargs:
73 kwargs['version'] = '1.0'
74 if 'encoding' not in kwargs:
75 kwargs['encoding'] = 'UTF-8'
76 Element.__init__(self, '?xml', *args, **kwargs)
77
78 def __str__(self):
79 args = " ".join(['%s="%s"' % (k, v) for k, v in self._attrs.items()])
80 string = '<?xml %s?>\n' % args
81 for element in self._elements:
82 string += str(element)
83 return string
84
85
86class Text(Element):
87 def __init__(self, content=""):
88 Element.__init__(self, None)
89 self.__content = content
90
91 def __str__(self):
92 return self.__content
93
94
95def xml_to_json(node):
96 """This does a really braindead conversion of an XML tree to
97 something that looks like a json dump. In cases where the XML
98 and json structures are the same, then this "just works". In
Attila Fazekasb2902af2013-02-16 16:22:44 +010099 others, it requires a little hand-editing of the result.
100 """
Dan Smithba6cb162012-08-14 07:22:42 -0700101 json = {}
102 for attr in node.keys():
Davanum Srinivas8d226cc2013-03-08 09:35:36 -0500103 if not attr.startswith("xmlns"):
104 json[attr] = node.get(attr)
Dan Smithba6cb162012-08-14 07:22:42 -0700105 if not node.getchildren():
106 return node.text or json
107 for child in node.getchildren():
108 tag = child.tag
109 if tag.startswith("{"):
110 ns, tag = tag.split("}", 1)
111 json[tag] = xml_to_json(child)
112 return json