-
Notifications
You must be signed in to change notification settings - Fork 4
/
preset2human.py
executable file
·232 lines (171 loc) · 4.7 KB
/
preset2human.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
224
225
226
227
228
229
230
231
232
# -*- coding:utf-8 -*-
"""
Usage:
preset2human.py <file> [options]
Options:
-f, --format <format> Ouput format: csv, txt, wiki, json [default: txt]
-o, --output <filepath> Ouput filepath [default: ./output.txt]
"""
import csv
import codecs
from lxml import etree
from docopt import docopt
class Node(object):
def __init__(self, node, depth=0, parent=None):
self._node = node
self.depth = depth
self.parent = parent
@property
def name(self):
return self._node.attrib['name']
@property
def txt_prefix(self):
return u""
def __unicode__(self):
return self.name
def __str__(self):
return self.__unicode__().encode('utf-8')
@property
def txt_str(self):
return self.__unicode__()
def to_txt(self):
return "%s%s%s\n" % (
u"\t" * self.depth,
self.txt_prefix,
self.txt_str
)
def to_csv(self):
return ["" for e in range(self.depth)] + [self]
def to_wiki(self):
return unicode(self)
def wiki_key(self, value):
return u"[[Key:%s|%s]]" % (value, value)
def wiki_tag(self, key, value):
return u"[[Tag:%s%%3D%s|%s]]" % (key, value, value)
class GroupNode(Node):
@property
def txt_prefix(self):
return u"— "
def to_wiki(self):
affix = u"=" * self.depth
return u"=%s %s =%s" % (
affix,
super(GroupNode, self).to_wiki(),
affix,
)
class ItemNode(Node):
@property
def txt_prefix(self):
return u"* "
def to_wiki(self):
return u"==== %s ====" % super(ItemNode, self).to_wiki()
class KeyNode(Node):
@property
def name(self):
return self._node.attrib['key']
@property
def value(self):
return self._node.attrib['value']
@property
def txt_str(self):
return u"%s => %s" % (self.name, self.value)
def to_wiki(self):
return u"* %s=%s" % (
self.wiki_key(self.name),
self.wiki_tag(self.name, self.value)
)
class ComboNode(Node):
@property
def name(self):
return self._node.attrib['key']
@property
def values(self):
return self._node.attrib['values']
def to_wiki(self):
return u"* %s=%s" % (
self.wiki_key(self.name),
self.values
)
class TextNode(Node):
@property
def name(self):
return self._node.attrib['key']
def __unicode__(self):
return u"%s => …" % self.name
def to_wiki(self):
return u"* %s=…" % (
self.wiki_key(self.name),
)
class CheckNode(Node):
@property
def name(self):
return self._node.attrib['key']
@property
def default(self):
try:
default = self._node.attrib['default']
except KeyError:
default = "no"
finally:
return default
@property
def on(self):
return self.default in ("yes", "on") and "[yes]" or "yes"
@property
def off(self):
return self.default in ("no", "off") and "[no]" or "no"
@property
def txt_str(self):
return u"%s => %s/%s" % (self.name, self.on, self.off)
def to_wiki(self):
return u"* %s=%s/%s" % (
self.wiki_key(self.name),
self.on,
self.off
)
def main(path):
with open(path) as f:
content = f.read()
content = content.replace('xmlns="', 'xmlnamespace="')
root = etree.XML(content)
NODES = []
def iternode(parent, depth=0):
for child in parent.getchildren():
try:
cls = globals()["%sNode" % child.tag.title()]
except (AttributeError, KeyError):
pass
else:
node = cls(child, depth)
NODES.append(node)
finally:
iternode(child, depth=depth+1)
iternode(root)
return NODES
def to_txt(nodes, filepath):
content = ""
for node in nodes:
content += node.to_txt()
f = codecs.open(filepath, 'w', "utf-8")
f.write(content)
f.close()
def to_csv(nodes, filepath):
writer = csv.writer(
open("%s.csv" % filepath, "wb"),
delimiter=",",
quotechar='"'
)
for node in nodes:
writer.writerow(node.to_csv())
def to_wiki(nodes, filepath):
for node in nodes:
print node.to_wiki()
def to_print(nodes, filepath):
for node in nodes:
print node.to_txt()
if __name__ == "__main__":
args = docopt(__doc__)
nodes = main(args['<file>'])
format = args['--format']
output = args['--output']
globals()["to_%s" % format](nodes, output)