-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate_ui.py
133 lines (118 loc) · 4.85 KB
/
generate_ui.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
#!/usr/bin/env python
"""This file is intended as a code generation tool to keep sate.settings.py
up-to-date when the UI changes.
Putting the UI in XML will also make the UI constraints easier to
handle in GUI and web apps.
"""
import sys, os
# from elementtree import ElementTree
from xml.etree import ElementTree
# elementtree.ElementTree is not the same as xml.etree.ElementTree in standard library?
class UserOption(object):
def __init__(self, xml_node):
self.name = xml_node.attrib['name']
self.default = xml_node.attrib.get('default')
self.category = xml_node.attrib.get('category')
self.subcategory = xml_node.attrib.get('subcategory')
self.short_name = xml_node.attrib.get('short')
h = xml_node.find("help")
if h is not None:
self.help = h.text.strip()
else:
self.help = ''
self.map_to = 'UserSetting'
def as_user_setting_code(self):
return '%s(name=%s, default=%s, short_name=%s, help=%s, subcategory=%s)' % (
self.map_to,
repr(self.name),
repr(self.default),
repr(self.short_name),
repr(self.help),
repr(self.subcategory))
class StringOption(UserOption):
def __init__(self, xml_node):
UserOption.__init__(self, xml_node)
self.map_to = 'StringUserSetting'
class BoolOption(UserOption):
def __init__(self, xml_node):
UserOption.__init__(self, xml_node)
self.map_to = 'BoolUserSetting'
class ChoiceOption(StringOption):
def __init__(self, xml_node):
StringOption.__init__(self, xml_node)
self.multi_choice = bool(xml_node.attrib.get('multichoice','false').lower() == 'true')
self.choices = [i.attrib.get('value') for i in xml_node.findall('choice')]
def as_user_setting_code(self):
return 'ChoiceUserSetting(name=%s, default=%s, choices=%s, multiple_choices=%s, short_name=%s, help=%s, subcategory=%s)' % (
repr(self.name),
repr(self.default),
repr(self.choices),
repr(bool(self.multi_choice)),
repr(self.short_name),
repr(self.help),
repr(self.subcategory))
class NumberOption(UserOption):
def __init__(self, xml_node, conv_to=float):
UserOption.__init__(self, xml_node)
self.map_to = 'FloatUserSetting'
if self.default:
self.default = conv_to(self.default)
self.conv_to = conv_to
self.min = xml_node.attrib.get('min')
if self.min:
self.min = conv_to(self.min)
self.max = xml_node.attrib.get('max')
if self.max:
self.max = conv_to(self.min)
def as_user_setting_code(self):
return '%s(name=%s, default=%s, min=%s, max=%s, short_name=%s, help=%s, subcategory=%s)' % (
self.map_to,
repr(self.name),
repr(self.default),
repr(self.min),
repr(self.max),
repr(self.short_name),
repr(self.help),
repr(self.subcategory))
class IntegerOption(NumberOption):
def __init__(self, xml_node):
NumberOption.__init__(self, xml_node, conv_to=int)
self.map_to = 'IntUserSetting'
class DurationOption(NumberOption):
def __init__(self, xml_node):
NumberOption.__init__(self, xml_node)
element_to_class = {
'choice-option' : ChoiceOption,
'bool-option' : BoolOption,
'int-option' : IntegerOption,
'outfile-option' : StringOption,
'infile-option' : StringOption,
'string-option' : StringOption,
'number-option' : NumberOption,
'duration-option' : DurationOption
}
if __name__ == '__main__':
xml_filepath = sys.argv[1]
xml_inp = open(xml_filepath, "rU")
xml_doc = ElementTree.parse(xml_inp)
s = xml_doc.getroot()
assert s.tag == 'option-list'
option_list = [element_to_class[i.tag](i) for i in s.getchildren()]
categories = set()
for o in option_list:
categories.add(o.category)
sys.stdout.write("""#! /usr/bin/env python
# autogenerated from %s by %s
"""% (xml_filepath, os.path.split(sys.argv[0])[1]))
sys.stdout.write("""
from sate.usersettingclasses import ChoiceUserSetting, UserSettingsContainer, UserSettingGroup
from sate.usersettingclasses import FloatUserSetting, StringUserSetting, IntUserSetting, BoolUserSetting
class SateUserSettings(UserSettingsContainer):
def __init__(self):
UserSettingsContainer.__init__(self)
""")
for c in categories:
sys.stdout.write(" self.%s = UserSettingGroup(%s)\n" % (c, repr(c)))
sys.stdout.write(" self._categories.append(%s)\n" % repr(c))
for o in option_list:
sys.stdout.write(" self.%s.add_option(%s, %s)\n" % (o.category, repr(o.name), o.as_user_setting_code()))