This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
130 lines (107 loc) · 3.73 KB
/
convert.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
#!/usr/bin/env python
import os
import sys
import argparse
import xml.etree.ElementTree as ET
__doc__ = """A simple python script to convert a XML form definition
written in formed into a formbar form definiton."""
OENTITY = u'<entity id="{id}" name="{name}" label="{label}" type="{type}" {desired}>'
CENTITY = u'</entity>'
def get_values(e, t="string"):
values = {}
values["name"] = e.attrib.get("name")
values["id"] = e.attrib.get("name")
values["type"] = t
values["label"] = e.attrib.get("description")
flags = e.attrib.get("flags")
values["desired"] = ""
if flags and flags.startswith("required"):
values["desired"] = 'desired="true"'
return values
def convert_element(e, t=None):
x = []
options = []
values = get_values(e, t)
x.append(OENTITY.format(**values))
for c in e.findall("bool"):
value = c.attrib["value"]
if value == "-1":
value = ""
options.append(" "*8 + u'<option value="{}">{}</option>'.format(value, c.attrib["description"]))
if options:
y = []
y.append("\n" + " "*4 + "<options>")
for o in options:
y.append(o)
y.append(" "*4 + "</options>\n")
x.append("\n".join(y))
x.append(CENTITY+"\n")
return (values["name"], "".join(x))
def convert_text(e):
return convert_element(e, "string")
def convert_int(e):
return convert_element(e, "integer")
def convert_date(e):
return convert_element(e, "date")
def convert_choice(e):
return convert_element(e, "integer")
def find_elements(et):
# Find all text fields
elements = []
for e in et.findall(".//text"):
elements.append(convert_text(e))
for e in et.findall(".//int"):
elements.append(convert_int(e))
for e in et.findall(".//date"):
elements.append(convert_date(e))
for e in et.findall(".//choice"):
elements.append(convert_choice(e))
return elements
def parse_infile(xml, rg=None):
"""TODO: Docstring for parse_infile.
:returns: TODO
"""
ignored_elements = []
et = ET.fromstring(xml.read())
# Find all repeat groups:
for e in et.findall(".//repeat"):
if rg and e.attrib["name"] == rg:
return [x[1] for x in find_elements(e)]
all_elements = find_elements(et)
ignored_names = [x[0] for x in ignored_elements]
elements = []
for e in all_elements:
if e[0] in ignored_names:
continue
elements.append(e[1])
return elements
def main(arguments):
parser = argparse.ArgumentParser(description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('infile', help="Input file", type=argparse.FileType('r'))
parser.add_argument('-o', '--outfile', help="Output file",
default=sys.stdout, type=argparse.FileType('w'))
parser.add_argument('-l', '--list-modules',
action="store_true",
dest="list",
help="Will list all modules/repeat groups")
parser.add_argument('-r', '--repeat-group',
dest="rg",
help="Parse the given repeat group.")
args = parser.parse_args(arguments)
if args.list:
et = ET.fromstring(args.infile.read())
# Find all repeat groups:
for e in et.findall(".//repeat"):
print(e.attrib["name"])
else:
elements = parse_infile(args.infile, args.rg)
content = "".join(e.encode("utf-8") for e in elements)
if args.outfile:
args.outfile.write(content)
args.outfile.close()
else:
print(content)
if __name__ == '__main__':
main(sys.argv[1:])
sys.exit(0)