forked from zengxs/py-kms
-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathuxml2dict.py
71 lines (64 loc) · 2.03 KB
/
uxml2dict.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
try: # pragma no cover
from collections import OrderedDict
except ImportError: # pragma no cover
try:
from ordereddict import OrderedDict
except ImportError:
try:
from ucollections import OrderedDict # micropython
except ImportError:
OrderedDict = dict
TEXT = "TEXT"
START_TAG = "START_TAG"
#START_TAG_DONE = "START_TAG_DONE"
END_TAG = "END_TAG"
PI = "PI"
#PI_DONE = "PI_DONE"
ATTR = "ATTR"
#ATTR_VAL = "ATTR_VAL"
def parseitem(iter_tok, parsed, lesslist):
while True:
try:
tok = next(iter_tok)
except (StopIteration, RuntimeError): # RuntimeError in micropython
return iter_tok
if tok[0] == PI:
pass
elif tok[0] == ATTR:
_, (namespace, attr), value = tok
if namespace:
attr = namespace + ':' + attr
parsed['@' + attr] = value
elif tok[0] == TEXT:
_, text = tok
parsed['#text'] = text
elif tok[0] == START_TAG:
_, (namespace, tag) = tok
if namespace:
tag = namespace + ':' + tag
d = OrderedDict()
iter_tok = parseitem(iter_tok, d, lesslist)
if not d:
d = None
elif len(d) == 1 and '#text' in d:
d = d['#text']
parsed.setdefault(tag, [])
if lesslist and len(parsed[tag]) == 1:
parsed[tag] = [parsed[tag]]
parsed[tag].append(d)
if lesslist and len(parsed[tag]) == 1:
parsed[tag] = parsed[tag][0]
elif tok[0] == END_TAG:
return iter_tok
else:
raise NotImplementedError('Token %s not support' % tok[0])
def parse(iter_tok, lesslist=True):
parsed = OrderedDict()
parseitem(iter_tok, parsed, lesslist)
return parsed
if __name__ == '__main__':
import json
import xmltok
iter_tok = xmltok.tokenize(open('vector-text.svg'))
parsed = parse(iter_tok)
print(json.dumps(parsed, indent=4))