-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathuntangle.py
executable file
·223 lines (173 loc) · 5.88 KB
/
untangle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
untangle
Converts xml to python objects.
The only method you need to call is parse()
Partially inspired by xml2obj
(http://code.activestate.com/recipes/149368-xml2obj/)
Author: Christian Stefanescu (http://0chris.com)
License: MIT License - http://www.opensource.org/licenses/mit-license.php
"""
import os
import keyword
from defusedxml.sax import make_parser
from xml.sax import handler
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
try:
from types import StringTypes
def is_string(x):
return isinstance(x, StringTypes)
except ImportError:
def is_string(x):
return isinstance(x, str)
__version__ = "1.2.1"
class Element(object):
"""
Representation of an XML element.
"""
def __init__(self, name, attributes):
self._name = name
self._attributes = attributes
self.children = []
self.is_root = False
self.cdata = ""
def add_child(self, element):
"""
Store child elements.
"""
self.children.append(element)
def add_cdata(self, cdata):
"""
Store cdata
"""
self.cdata = self.cdata + cdata
def get_attribute(self, key):
"""
Get attributes by key
"""
return self._attributes.get(key)
def get_elements(self, name=None):
"""
Find a child element by name
"""
if name:
return [e for e in self.children if e._name == name]
else:
return self.children
def __getitem__(self, key):
return self.get_attribute(key)
def __getattr__(self, key):
matching_children = [x for x in self.children if x._name == key]
if matching_children:
if len(matching_children) == 1:
self.__dict__[key] = matching_children[0]
return matching_children[0]
else:
self.__dict__[key] = matching_children
return matching_children
else:
raise AttributeError("'%s' has no attribute '%s'" % (self._name, key))
def __hasattribute__(self, name):
if name in self.__dict__:
return True
return any(x._name == name for x in self.children)
def __iter__(self):
yield self
def __str__(self):
return "Element <%s> with attributes %s, children %s and cdata %s" % (
self._name,
self._attributes,
self.children,
self.cdata,
)
def __repr__(self):
return "Element(name = %s, attributes = %s, cdata = %s)" % (
self._name,
self._attributes,
self.cdata,
)
def __bool__(self):
return self.is_root or self._name is not None
__nonzero__ = __bool__
def __eq__(self, val):
return self.cdata == val
def __dir__(self):
children_names = [x._name for x in self.children]
return children_names
def __len__(self):
return len(self.children)
def __contains__(self, key):
return key in dir(self)
class Handler(handler.ContentHandler):
"""
SAX handler which creates the Python object structure out of ``Element``s
"""
def __init__(self):
self.root = Element(None, None)
self.root.is_root = True
self.elements = []
def startElement(self, name, attributes):
name = name.replace("-", "_")
name = name.replace(".", "_")
name = name.replace(":", "_")
# adding trailing _ for keywords
if keyword.iskeyword(name):
name += "_"
attrs = dict()
for k, v in attributes.items():
attrs[k] = v
element = Element(name, attrs)
if len(self.elements) > 0:
self.elements[-1].add_child(element)
else:
self.root.add_child(element)
self.elements.append(element)
def endElement(self, name):
self.elements.pop()
def characters(self, cdata):
self.elements[-1].add_cdata(cdata)
def parse(filename, **parser_features):
"""
Interprets the given string as a filename, URL or XML data string,
parses it and returns a Python object which represents the given
document.
Extra arguments to this function are treated as feature values that are
passed to ``parser.setFeature()``. For example, ``feature_external_ges=False``
will set ``xml.sax.handler.feature_external_ges`` to False, disabling
the parser's inclusion of external general (text) entities such as DTDs.
Raises ``ValueError`` if the first argument is None / empty string.
Raises ``AttributeError`` if a requested xml.sax feature is not found in
``xml.sax.handler``.
Raises ``xml.sax.SAXParseException`` if something goes wrong
during parsing.
Raises ``defusedxml.common.EntitiesForbidden``
or ``defusedxml.common.ExternalReferenceForbidden``
when a potentially malicious entity load is attempted. See also
https://github.com/tiran/defusedxml#attack-vectors
"""
if filename is None or (is_string(filename) and filename.strip()) == "":
raise ValueError("parse() takes a filename, URL or XML string")
parser = make_parser()
for feature, value in parser_features.items():
parser.setFeature(getattr(handler, feature), value)
sax_handler = Handler()
parser.setContentHandler(sax_handler)
if is_string(filename) and (os.path.exists(filename) or is_url(filename)):
parser.parse(filename)
else:
if hasattr(filename, "read"):
parser.parse(filename)
else:
parser.parse(StringIO(filename))
return sax_handler.root
def is_url(string):
"""
Checks if the given string starts with 'http(s)'.
"""
try:
return string.startswith("http://") or string.startswith("https://")
except AttributeError:
return False
# vim: set expandtab ts=4 sw=4: