-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsconf.py
269 lines (231 loc) · 11.6 KB
/
sconf.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
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
#!/usr/bin/env python
# Populate a tree of all items in the configuration
import os
import sys
from Tkinter import *
from ScrolledText import *
import ttk
import kconf
class PopupWindow(object):
def __init__(self, root, config, value):
self.top=top=Toplevel(root)
self.l=Label(top, text=config)
self.l.grid(row=0, column=0)
self.e=Entry(top)
self.e.delete(0, END)
self.e.insert(0, value)
self.e.grid(row=0, column=1)
self.b=Button(top, text='Submit', command=self.Submit)
self.b.grid(row=1, column=0)
self.c=Button(top, text='Cancel', command=self.Cancel)
self.c.grid(row=1, column=1)
self.v=False
self.Center()
self.top.grid()
self.top.wm_title("Set " + config)
def Submit(self):
self.value=self.e.get()
self.v=True
self.top.destroy()
def Cancel(self):
self.v=False
self.top.destroy()
def Center(self):
self.top.update()
w_req, h_req = self.top.winfo_width(), self.top.winfo_height()
w_form = self.top.winfo_rootx() - self.top.winfo_x()
w = w_req + w_form*2
h = h_req + (self.top.winfo_rooty() - self.top.winfo_y()) + w_form
x = (self.top.winfo_screenwidth() // 2) - (w // 2)
y = (self.top.winfo_screenheight() // 2) - (h // 2)
self.top.geometry('{0}x{1}+{2}+{3}'.format(w_req, h_req, x, y))
class App():
def __init__(self, root, conf):
self.conf = conf
self.items = conf.get_top_level_items()
self.tree = ttk.Treeview(root, selectmode="browse", columns=("name", "value", "type"), displaycolumns=("value", "type"), height=30)
ysb = ttk.Scrollbar(orient=VERTICAL, command= self.tree.yview)
xsb = ttk.Scrollbar(orient=HORIZONTAL, command= self.tree.xview)
self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
self.tree.heading('#0', text='Configuration Tree', anchor='w')
self.tree.column("#0", minwidth=0, width=800, stretch=True)
self.tree.heading("name", text="Name")
self.tree.column("name", minwidth=0, width=200, stretch=True)
self.tree.heading("value", text="Value")
self.tree.column("value", minwidth=0, width=50, stretch=True)
self.tree.heading("type", text="Type")
self.tree.column("type", minwidth=0, width=50, stretch=True)
self.add_root_items(self.items)
self.help = ScrolledText(root, width=130, height=10)
self.msg = StringVar()
self.info = Label(root, fg="green", font=("Helvetica", 16), anchor=W, justify=LEFT, textvariable=self.msg)
self.msg.set("Please set configurations, then you can save it!")
self.tree.grid(row=0, column=0)
self.help.grid(row=1, column=0)
self.info.grid(row=2, column=0)
ysb.grid(row=0, column=1, sticky='ns')
xsb.grid(row=1, column=0, sticky='ew')
root.grid()
self.root = root
# create a toplevel menu
menubar = Menu(root)
menubar.add_command(label="Save Config!", command=self.OnSaveConfig)
menubar.add_command(label="Quit Config!", command=self.OnQuitConfig)
# display the menu
root.config(menu=menubar)
self.tree.bind("<Double-1>", self.OnDoubleClick)
self.tree.bind("<<TreeviewSelect>>", self.OnSelection)
def add_subr_items(self, parent, items):
for item in items:
if item.get_visibility() != "n":
if item.is_symbol():
str = 'config {0}'.format(item.get_name())
self.tree.insert(parent, "end", text=str, values=[item.get_name(), item.get_value(), kconf.TYPENAME[item.get_type()]], open=True)
elif item.is_menu():
str = 'menu "{0}"'.format(item.get_title())
parent = self.tree.insert(parent, "end", text=str, open=True)
self.add_subr_items(parent, item.get_items())
elif item.is_choice():
str = 'choice "{0}"'.format(item.get_prompts()[0])
parent = self.tree.insert(parent, "end", text=str, open=True)
self.add_subr_items(parent, item.get_items())
elif item.is_comment():
str = 'comment "{0}"'.format(item.get_text())
self.tree.insert(parent, "end", text=str, open=True)
def add_root_items(self, items):
for item in items:
if item.get_visibility() != "n":
if item.is_symbol():
str = 'config {0}'.format(item.get_name())
self.tree.insert("", "end", text=str, values=[item.get_name(), item.get_value(), kconf.TYPENAME[item.get_type()]], open=True)
elif item.is_menu():
str = 'menu "{0}"'.format(item.get_title())
parent = self.tree.insert("", "end", text=str, open=True)
self.add_subr_items(parent, item.get_items())
elif item.is_choice():
str = 'choice "{0}"'.format(item.get_prompts()[0])
parent = self.tree.insert("", "end", text=str, open=True)
self.add_subr_items(parent, item.get_items())
elif item.is_comment():
str = 'comment "{0}"'.format(item.get_text())
self.tree.insert("", "end", text=str, open=True)
def search_for_item(self, name, item=None):
children = self.tree.get_children(item)
for child in children:
nameval = self.tree.set(child, "name")
if nameval == name:
return child
else:
res = self.search_for_item(name, child)
if res:
return res
return None
def OnDoubleClick(self, event):
mitem = self.tree.identify('item',event.x, event.y)
nameval = self.tree.set(mitem, "name")
symbol = self.conf.get_symbol(nameval)
if symbol is None:
return
if (symbol.get_type() == kconf.BOOL or (symbol.get_type() == kconf.TRISTATE and not self.conf.is_tristate_enabled())):
if (symbol.is_choice_symbol() and len(symbol.get_parent().get_items()) == 1):
self.msg.set("A boolean choice, exactly one config option must be set to y, so no change here!")
return
if (symbol.get_value() == "y"):
print("Setting " + symbol.get_name() + " to n")
symbol.set_user_value("n")
if (symbol.get_value() == "y"):
self.msg.set("A boolean choice that is the only 'y' in the choice group can not be set to 'n'")
symbol.set_user_value("y")
return
self.tree.set(mitem, "value", symbol.get_value())
else:
print("Setting " + symbol.get_name() + " to y")
symbol.set_user_value("y")
self.tree.set(mitem, "value", symbol.get_value())
print("Dependents for " + symbol.get_name() + " {" + symbol.get_visibility() + "} " + " [" + symbol.get_value() + "]:")
for sym in symbol.get_dependent_symbols():
print(sym.get_name() + " {"+sym.get_visibility() + "} " + " [" + sym.get_value() + "] ")
mitem = self.search_for_item(sym.get_name(), None)
if (mitem):
self.tree.set(mitem, "value", sym.get_value())
print("========================================")
elif (symbol.get_type() == kconf.TRISTATE and self.conf.is_tristate_enabled()):
nono = False
if (symbol.is_choice_symbol() and len(symbol.get_parent().get_items()) == 1):
self.msg.set("A single tristate choice in one choice group can not be set to 'n'")
nono = True
# 'y'->'m'->'n'->'y'->'m'->'n'->...
if (symbol.get_value() == "y"):
print("Setting " + symbol.get_name() + " to m")
symbol.set_user_value("m")
if (symbol.get_value() == "y"):
self.msg.set("A tristate choice that is the only 'y' in the choice group can not be set to 'n'")
symbol.set_user_value("y")
return
self.tree.set(mitem, "value", symbol.get_value())
elif (symbol.get_value() == "m"):
print("Setting " + symbol.get_name() + " to n")
symbol.set_user_value("n")
if (symbol.get_value() == "m"):
self.msg.set("A tristate choice that is the only 'm' in the choice group can not be set to 'n'")
symbol.set_user_value("m")
return
self.tree.set(mitem, "value", symbol.get_value())
else:
print("Setting " + symbol.get_name() + " to y")
symbol.set_user_value("y")
self.tree.set(mitem, "value", symbol.get_value())
print("Dependents for " + symbol.get_name() + " {" + symbol.get_visibility() + "} " + " [" + symbol.get_value() + "]:")
for sym in symbol.get_dependent_symbols():
print(sym.get_name() + " {"+sym.get_visibility() + "} " + " [" + sym.get_value() + "] ")
mitem = self.search_for_item(sym.get_name(), None)
if (mitem):
self.tree.set(mitem, "value", sym.get_value())
print("========================================")
elif (symbol.get_type() == kconf.INT or symbol.get_type() == kconf.HEX or symbol.get_type() == kconf.STRING):
self.pop = PopupWindow(self.root, symbol.get_name(), symbol.get_value())
self.root.wait_window(self.pop.top)
if (self.pop.v == True):
print("Setting " + symbol.get_name() + " to " + self.pop.value)
symbol.set_user_value(self.pop.value)
print("Now " + symbol.get_name() + " is " + symbol.get_value())
self.tree.set(mitem, "value", symbol.get_value())
return
def OnSelection(self, event):
mitem = self.tree.focus()
values = self.tree.item(mitem,"values");
if (values):
symbol = self.conf.get_symbol(values[0])
if (symbol):
self.msg.set("Please Double Click to update config option value!")
self.help.delete(1.0, END)
help = symbol.get_help()
if (help):
self.help.insert(INSERT, help)
return
def OnSaveConfig(self):
self.conf.write_config(".config")
self.conf.write_config_python("config.py")
header_dir = conf.get_config_header_dir()
dstfile = os.path.join(header_dir, "config.h")
dstdir = os.path.split(dstfile)[0]
if not os.path.exists(dstdir):
os.makedirs(dstdir)
self.conf.write_config_header(dstfile)
self.msg.set("Configurations Saved!")
return
def OnQuitConfig(self):
self.root.quit()
return
if __name__ == "__main__":
if len(sys.argv) <= 1:
cfgfile = os.path.abspath('.').replace('\\', '/') + "/" + "SConfigure"
else:
cfgfile = os.path.abspath('.').replace('\\', '/') + "/" + sys.argv[1]
conf = kconf.Config(cfgfile)
if os.path.exists(".config"):
conf.load_config(".config")
root = Tk()
app = App(root, conf)
root.title(cfgfile)
root.mainloop()