/
options.py
executable file
·278 lines (232 loc) · 8.66 KB
/
options.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
270
271
272
273
274
275
276
277
278
# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4
"Main module for running pychecker a Tkinter GUI for all the options"
import sys
import os
import Tkinter, tkFileDialog
from OptionTypes import *
from string import capitalize, strip, rstrip, split
import Config
MAX_SUBBOX_ROWS = 8
MAX_BOX_COLS = 3
PAD = 10
EDITOR = "xterm -e vi -n +%(line)d %(file)s"
if sys.platform == 'win32':
EDITOR = "notepad %(file)s"
def col_weight(grid):
"Set column weights so that sticky grid settings actually work"
unused, col = grid.grid_size()
for c in range(col):
grid.columnconfigure(c, weight=1)
def spawn(cmd_list):
try:
if os.fork():
try:
os.execvp(cmd_list[0], cmd_list)
finally:
sys.exit()
except AttributeError:
os.execvp(cmd_list[0], cmd_list)
def edit(file, line):
"Fire up an external editor to see the file at the given line"
unused = file, line
args = split(EDITOR)
cmd_list = []
for word in args:
cmd_list.append(word % locals())
spawn(cmd_list)
def closeCB():
sys.exit(0)
class Results:
"Display the warnings produced by checker"
def __init__(self, w):
self.top = Tkinter.Toplevel(w, name="results")
self.top.transient(w)
self.top.bind('<Return>', self.hide)
self.top.bind('<Escape>', self.hide)
self.text = Tkinter.Text(self.top, name="text")
self.text.grid()
self.text.bind('<Double-Button-1>', self.showFile)
close = Tkinter.Button(self.top,
name="close",
default=Tkinter.ACTIVE,
command=self.hide)
close.grid()
self.text.update_idletasks()
def show(self, text):
self.text.delete("0.1", "end")
self.text.insert("0.1", text)
self.top.deiconify()
self.top.lift()
def hide(self, *unused):
self.top.withdraw()
def line(self):
return split(self.text.index(Tkinter.CURRENT), ".")[0]
def showFile(self, unused):
import re
line = self.line()
text = self.text.get(line + ".0", line + ".end")
text = rstrip(text)
result = re.search("(.*):([0-9]+):", text)
if result:
path, line = result.groups()
edit(path, int(line))
self.text.after(0, self.selectLine)
def selectLine(self):
line = self.line()
self.text.tag_remove(Tkinter.SEL, "1.0", Tkinter.END)
self.text.tag_add(Tkinter.SEL, line + ".0", line + ".end")
class ConfigDialog:
"Dialog for editing options"
def __init__(self, tk):
self._tk = tk
self._cfg, _, _ = Config.setupFromArgs(sys.argv)
self._help = None
self._optMap = {}
self._opts = []
self._file = Tkinter.StringVar()
self._results = None
if len(sys.argv) > 1:
self._file.set(sys.argv[1])
for name, group in Config._OPTIONS:
opts = []
for _, useValue, longArg, member, description in group:
value = None
if member:
value = getattr(self._cfg, member)
description = member + ": " + capitalize(description)
description = strip(description)
tk.option_add('*' + longArg + ".help", description)
if useValue:
if type(value) == type([]):
field = List(longArg, value)
elif type(value) == type(1):
field = Number(longArg, int(value))
elif type(value) == type(''):
field = Text(longArg, value)
else:
field = Boolean(longArg, value)
else:
field = Boolean(longArg, value)
self._optMap[longArg] = field
opts.append(field)
self._opts.append( (name, opts))
def _add_fields(self, w, opts):
count = 0
for opt in opts:
f = opt.field(w)
c, r = divmod(count, MAX_SUBBOX_ROWS)
f.grid(row=r, column=c, sticky=Tkinter.NSEW)
count = count + 1
def _add_group(self, w, name, opts):
colFrame = Tkinter.Frame(w)
label = Tkinter.Label(colFrame, text=name + ":")
label.grid(row=0, column=0, sticky=Tkinter.NSEW)
gframe = Tkinter.Frame(colFrame, relief=Tkinter.GROOVE, borderwidth=2)
gframe.grid(row=1, column=0, sticky=Tkinter.NSEW)
self._add_fields(gframe, opts)
label = Tkinter.Label(colFrame)
label.grid(row=2, column=0, sticky=Tkinter.NSEW)
colFrame.rowconfigure(2, weight=1)
return colFrame
def main(self):
frame = Tkinter.Frame(self._tk, name="opts")
frame.grid()
self._tk.option_readfile('Options.ad')
self._fields = {}
row, col = 0, 0
rowFrame = Tkinter.Frame(frame)
rowFrame.grid(row=row)
row = row + 1
for name, opts in self._opts:
w = self._add_group(rowFrame, name, opts)
w.grid(row=row, column=col, sticky=Tkinter.NSEW, padx=PAD)
col = col + 1
if col >= MAX_BOX_COLS:
col_weight(rowFrame)
rowFrame=Tkinter.Frame(frame)
rowFrame.grid(row=row, sticky=Tkinter.NSEW)
col = 0
row = row + 1
col_weight(rowFrame)
self._help = Tkinter.Label(self._tk, name="helpBox")
self._help.grid(row=row)
self._help.config(takefocus=0)
buttons = Tkinter.Frame(self._tk, name="buttons")
ok = Tkinter.Button(buttons, name="ok", command=self.ok, default=Tkinter.ACTIVE)
ok.grid(row=row, column=0)
default = Tkinter.Button(buttons, name="default", command=self.default)
default.grid(row=row, column=1)
close = Tkinter.Button(buttons, name="close", command=closeCB)
close.grid(row=row, column=2)
buttons.grid()
f = Tkinter.Frame(self._tk, name="fileStuff")
Tkinter.Button(f, name="getfile", command=self.file).grid(row=0, column=1)
fileEntry = Tkinter.Entry(f, name="fname", textvariable=self._file)
fileEntry.grid(row=0, column=2)
Tkinter.Button(f, name="check", command=self.check).grid(row=0, column=3)
f.grid(sticky=Tkinter.EW)
self._tk.bind_all('<FocusIn>', self.focus)
self._tk.bind_all('<Enter>', self.focus)
self._tk.bind_all('<ButtonPress>', self.click)
fileEntry.bind('<Return>', self.check)
self._tk.mainloop()
#
# Callbacks
#
def help(self, w):
if type(w) == type(''): # occurs with file dialog...
return
if self._help == w: # ignore help events on help...
return
text = w.option_get("help", "help")
self._help.configure(text=text)
def focus(self, ev):
self.help(ev.widget)
def click(self, ev):
self.help(ev.widget)
def ok(self):
opts = []
# Pull command-line args
for _, group in self._opts:
for opt in group:
arg = opt.arg()
if arg:
opts.append(arg)
# Calculate config
self._cfg, _, _ = Config.setupFromArgs(opts)
# Set controls based on new config
for _, group in Config._OPTIONS:
for _, _, longArg, member, _ in group:
if member:
self._optMap[longArg].set(getattr(self._cfg, member))
def default(self):
self._cfg, _, _ = Config.setupFromArgs(sys.argv)
for _, group in Config._OPTIONS:
for _, _, longArg, member, _ in group:
if member:
self._optMap[longArg].set(getattr(self._cfg, member))
else:
self._optMap[longArg].set(0)
def file(self):
self._file.set(tkFileDialog.askopenfilename())
def check(self, *unused):
import check
import StringIO
self.ok() # show effect of all settings
checker._allModules = {}
warnings = check.getWarnings([self._file.get()], self._cfg)
capture = StringIO.StringIO()
if not self._results:
self._results = Results(self._help)
checker._printWarnings(warnings, capture)
value = strip(capture.getvalue())
if not value:
value = "None"
self._results.show(value)
if __name__=='__main__':
dirs = os.path.join(os.path.split(os.getcwd())[:-1])
sys.path.append(dirs[0])
tk = Tkinter.Tk()
tk.title('PyChecker')
ConfigDialog(tk).main()