-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathutils.py
More file actions
360 lines (289 loc) · 12.1 KB
/
utils.py
File metadata and controls
360 lines (289 loc) · 12.1 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
'''
© 2012-2013 eBay Software Foundation
Authored by: Tim Keefer
Licensed under CDDL 1.0
'''
import sys
from lxml import etree as ET
from xml.sax.saxutils import escape
if sys.version_info[0] >= 3:
unicode = str
long = int
def parse_yaml(yaml_file):
"""
This is simple approach to parsing a yaml config that is only
intended for this SDK as this only supports a very minimal subset
of yaml options.
"""
with open(yaml_file) as f:
data = {None: {}}
current_key = None
for line in f.readlines():
# ignore comments
if line.startswith('#'):
continue
# parse the header
elif line[0].isalnum():
key = line.strip().replace(':', '')
current_key = key
data[current_key] = {}
# parse the key: value line
elif line[0].isspace():
values = line.strip().split(':', 1)
if len(values) == 2:
cval = values[1].strip()
if cval == '0':
cval = False
elif cval == '1':
cval = True
data[current_key][values[0].strip()] = cval
return data
def python_2_unicode_compatible(klass):
"""
A decorator that defines __unicode__ and __str__ methods under Python 2.
Under Python 3 it does nothing.
To support Python 2 and 3 with a single code base, define a __str__ method
returning text and apply this decorator to the class.
"""
if sys.version_info[0] < 3:
if '__str__' not in klass.__dict__:
raise ValueError("@python_2_unicode_compatible cannot be applied "
"to %s because it doesn't define __str__()." %
klass.__name__)
klass.__unicode__ = klass.__str__
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
return klass
def get_dom_tree(xml):
tree = ET.fromstring(xml) # pylint: disable=no-member
return tree.getroottree().getroot()
def attribute_check(root):
attrs = []
value = None
if isinstance(root, dict):
if '#text' in root:
value = root['#text']
if '@attrs' in root:
for ak, av in sorted(root['@attrs'].items()):
attrs.append(str('{0}="{1}"').format(ak, smart_encode(av)))
return attrs, value
def smart_encode_request_data(value):
try:
if sys.version_info[0] < 3:
return value
if isinstance(value, str):
return value.encode('utf-8')
else:
return value
except UnicodeDecodeError as e:
return value
def smart_encode(value):
try:
if sys.version_info[0] < 3:
return unicode(value).encode('utf-8') # pylint: disable-msg=E0602
else:
return value
# return str(value)
except UnicodeDecodeError:
return value
def smart_decode(str):
try:
if sys.version_info[0] < 3:
return str.decode('utf-8')
return str
except UnicodeEncodeError:
return str
def to_xml(root):
return dict2xml(root)
def dict2xml(root, escape_xml=False):
'''
Doctests:
>>> dict1 = {'Items': {'ItemId': ['1234', '2222']}}
>>> dict2xml(dict1)
'<Items><ItemId>1234</ItemId><ItemId>2222</ItemId></Items>'
>>> dict2 = {
... 'searchFilter': {'categoryId': {'#text': 222, '@attrs': {'site': 'US'} }},
... 'paginationInput': {
... 'pageNumber': '1',
... 'pageSize': '25'
... },
... 'sortOrder': 'StartTimeNewest'
... }
>>> dict2xml(dict2)
'<paginationInput><pageNumber>1</pageNumber><pageSize>25</pageSize></paginationInput><searchFilter><categoryId site="US">222</categoryId></searchFilter><sortOrder>StartTimeNewest</sortOrder>'
>>> dict3 = {
... 'parent': {'child': {'#text': 222, '@attrs': {'site': 'US', 'id': 1234}}}
... }
>>> dict2xml(dict3)
'<parent><child id="1234" site="US">222</child></parent>'
>>> dict5 = {
... 'parent': {'child': {'@attrs': {'site': 'US', 'id': 1234}, }}
... }
>>> dict2xml(dict5)
'<parent><child id="1234" site="US"></child></parent>'
>>> dict4 = {
... 'searchFilter': {'categoryId': {'#text': 0, '@attrs': {'site': 'US'} }},
... 'paginationInput': {
... 'pageNumber': '1',
... 'pageSize': '25'
... },
... 'itemFilter': [
... {'name': 'Condition',
... 'value': 'Used'},
... {'name': 'LocatedIn',
... 'value': 'GB'},
... ],
... 'sortOrder': 'StartTimeNewest'
... }
>>> dict2xml(dict4)
'<itemFilter><name>Condition</name><value>Used</value></itemFilter><itemFilter><name>LocatedIn</name><value>GB</value></itemFilter><paginationInput><pageNumber>1</pageNumber><pageSize>25</pageSize></paginationInput><searchFilter><categoryId site="US">0</categoryId></searchFilter><sortOrder>StartTimeNewest</sortOrder>'
>>> dict2xml({})
''
>>> dict2xml('<a>b</a>')
'<a>b</a>'
>>> dict2xml(None)
''
>>> common_attrs = {'xmlns:xs': 'http://www.w3.org/2001/XMLSchema', 'xsi:type': 'xs:string'}
>>> attrdict = { 'attributeAssertion': [
... {'@attrs': {'Name': 'DevId', 'NameFormat': 'String', 'FriendlyName': 'DeveloperID'},
... 'urn:AttributeValue': {
... '@attrs': common_attrs,
... '#text': 'mydevid'
... },
... },
... {'@attrs': {'Name': 'AppId', 'NameFormat': 'String', 'FriendlyName': 'ApplicationID'},
... 'urn:AttributeValue': {
... '@attrs': common_attrs,
... '#text': 'myappid',
... },
... },
... {'@attrs': {'Name': 'CertId', 'NameFormat': 'String', 'FriendlyName': 'Certificate'},
... 'urn:AttributeValue': {
... '@attrs': common_attrs,
... '#text': 'mycertid',
... },
... },
... ],
... }
>>> print(dict2xml(attrdict))
<attributeAssertion FriendlyName="DeveloperID" Name="DevId" NameFormat="String"><urn:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">mydevid</urn:AttributeValue></attributeAssertion><attributeAssertion FriendlyName="ApplicationID" Name="AppId" NameFormat="String"><urn:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">myappid</urn:AttributeValue></attributeAssertion><attributeAssertion FriendlyName="Certificate" Name="CertId" NameFormat="String"><urn:AttributeValue xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">mycertid</urn:AttributeValue></attributeAssertion>
>>> dict2xml("łśżźć") # doctest: +SKIP
'\\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87'
>>> dict_special = {
... 'searchFilter': {'categoryId': {'#text': 'SomeID - łśżźć', '@attrs': {'site': 'US - łśżźć'} }},
... 'paginationInput': {
... 'pageNumber': '1 - łśżźć',
... 'pageSize': '25 - łśżźć'
... },
... 'itemFilter': [
... {'name': 'Condition - łśżźć',
... 'value': 'Used - łśżźć'},
... {'name': 'LocatedIn - łśżźć',
... 'value': 'GB - łśżźć'},
... ],
... 'sortOrder': 'StartTimeNewest - łśżźć'
... }
>>> dict2xml(dict_special) # doctest: +SKIP
'<itemFilter><name>Condition - \\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87</name><value>Used - \\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87</value></itemFilter><itemFilter><name>LocatedIn - \\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87</name><value>GB - \\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87</value></itemFilter><paginationInput><pageNumber>1 - \\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87</pageNumber><pageSize>25 - \\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87</pageSize></paginationInput><searchFilter><categoryId site="US - \\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87">SomeID - \\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87</categoryId></searchFilter><sortOrder>StartTimeNewest - \\xc5\\x82\\xc5\\x9b\\xc5\\xbc\\xc5\\xba\\xc4\\x87</sortOrder>'
'''
xml = str('')
if root is None:
return xml
if isinstance(root, dict):
for key in sorted(root.keys()):
if isinstance(root[key], dict):
attrs, value = attribute_check(root[key])
if value is None:
value = dict2xml(root[key], escape_xml)
elif isinstance(value, dict):
value = dict2xml(value, escape_xml)
attrs_sp = str('')
if len(attrs) > 0:
attrs_sp = str(' ')
xml = str('{xml}<{tag}{attrs_sp}{attrs}>{value}</{tag}>') \
.format(**{'tag': key, 'xml': str(xml), 'attrs': str(' ').join(attrs),
'value': smart_encode(value), 'attrs_sp': attrs_sp})
elif isinstance(root[key], list):
for item in root[key]:
attrs, value = attribute_check(item)
if value is None:
value = dict2xml(item, escape_xml)
elif isinstance(value, dict):
value = dict2xml(value, escape_xml)
attrs_sp = ''
if len(attrs) > 0:
attrs_sp = ' '
xml = str('{xml}<{tag}{attrs_sp}{attrs}>{value}</{tag}>') \
.format(**{'xml': str(xml), 'tag': key, 'attrs': ' '.join(attrs), 'value': smart_encode(value),
'attrs_sp': attrs_sp})
else:
value = root[key]
if escape_xml and hasattr(value, 'startswith') and not value.startswith('<![CDATA['):
value = escape(value)
xml = str('{xml}<{tag}>{value}</{tag}>') \
.format(**{'xml': str(xml), 'tag': key, 'value': smart_encode(value)})
elif isinstance(root, str) or isinstance(root, int) \
or isinstance(root, float) or isinstance(root, long) \
or isinstance(root, unicode):
xml = str('{0}{1}').format(str(xml), smart_encode(root))
else:
raise Exception('Unable to serialize node of type %s (%s)' %
(type(root), root))
return xml
def getValue(response_dict, *args, **kwargs):
args_a = [w for w in args]
first = args_a[0]
args_a.remove(first)
h = kwargs.get('mydict', {})
if h:
h = h.get(first, {})
else:
h = response_dict.get(first, {})
if len(args) == 1:
try:
return h.get('value', None)
except Exception as e:
return h
last = args_a.pop()
for a in args_a:
h = h.get(a, {})
h = h.get(last, {})
try:
return h.get('value', None)
except Exception as e:
return h
def getNodeText(node):
"Returns the node's text string."
rc = []
if hasattr(node, 'childNodes'):
for cn in node.childNodes:
if cn.nodeType == cn.TEXT_NODE:
rc.append(cn.data)
elif cn.nodeType == cn.CDATA_SECTION_NODE:
rc.append(cn.data)
return ''.join(rc)
def perftest_dict2xml():
sample_dict = {
'searchFilter': {'categoryId': {'#text': 222, '@attrs': {'site': 'US'}}},
'paginationInput': {
'pageNumber': '1',
'pageSize': '25'
},
'itemFilter': [
{'name': 'Condition',
'value': 'Used'},
{'name': 'LocatedIn',
'value': 'GB'},
],
'sortOrder': 'StartTimeNewest'
}
xml = dict2xml(sample_dict)
if __name__ == '__main__':
import timeit
print("perftest_dict2xml() %s" %
timeit.timeit("perftest_dict2xml()", number=50000,
setup="from __main__ import perftest_dict2xml"))
import doctest
failure_count, test_count = doctest.testmod()
sys.exit(failure_count)