/
GUI.pyw
executable file
·1398 lines (1354 loc) · 70.6 KB
/
GUI.pyw
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# By Elvish_Hunter, April 2014
# You may notice that this script, unlike all our other Python mainline scripts,
# has a .pyw extension, instead of .py. *This is deliberate*. On Windows, .pyw
# scripts are started directly in GUI mode, without opening a prompt.
# This is, after all, the behavior that we want.
# threading and subprocess are needed to run wmllint without freezing the window
# codecs is used to save files as UTF8
# Queue (queue in Python3) is needed to exchange informations between threads
# if we use the run_tool thread to do GUI stuff we obtain weird crashes
# This happens because Tk is a single-thread GUI
import sys,os,threading,subprocess,codecs
# sys.version_info checks the interpreter version
# this is used to have a script that can run on both Python2 and Python3
# not that useful until the mainline tools are updated, but still...
if sys.version_info.major >= 3:
import queue
# tkinter modules
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
# ttk must be called last
from tkinter.ttk import *
else: # we are on Python 2
import Queue
# tkinter modules
from Tkinter import *
from tkMessageBox import *
from tkFileDialog import *
# ttk must be called last
from ttk import *
# we need to know in what series we are
# so set it in a constant and change it for every new series
# it must be a string
WESNOTH_SERIES="1.13"
# get the location where the script is placed
# we'll check later if this is a Wesnoth directory
# and use it to generate the command lines
# os.path.realpath gets the full path of this script,
# while removing any symlink
# This allows users to create a link to the app on their desktop
# os.path.normpath allows Windows users to see their standard path separators
APP_DIR,APP_NAME=os.path.split(os.path.realpath(sys.argv[0]))
upper_dir=APP_DIR.split(os.sep)
upper_dir.pop()
WESNOTH_DATA_DIR=os.sep.join(upper_dir)
WESNOTH_CORE_DIR=os.path.normpath(os.path.join(WESNOTH_DATA_DIR,"core"))
def wrap_elem(line):
"""If the supplied line contains spaces, return it wrapped between double quotes"""
if ' ' in line:
return "\"{0}\"".format(line)
return line
def run_tool(tool,queue,command):
"""Runs a maintenance tool with the desired arguments and pushes the output in the supplied queue"""
if sys.platform=="win32":
# Windows wants a string, Linux wants a list and Polly wants a cracker
# Windows wants also strings flavoured with double quotes
# since maps return iterators, we must cast them as lists, otherwise join won't work
# not doing this causes an OSError: [WinError 87]
# this doesn't happen on Python 2.7, because here map() returns a list
wrapped_line=list(map(wrap_elem,command))
queue.put_nowait(' '.join(wrapped_line)+"\n")
si=subprocess.STARTUPINFO()
si.dwFlags=subprocess.STARTF_USESHOWWINDOW|subprocess.SW_HIDE # to avoid showing a DOS prompt
try:
output=subprocess.check_output(' '.join(wrapped_line),stderr=subprocess.STDOUT,startupinfo=si)
queue.put_nowait(output)
except subprocess.CalledProcessError as error:
# post the precise message and the remaining output as a tuple
queue.put_nowait((tool,error.returncode,error.output))
else: # STARTUPINFO is not available, nor needed, outside of Windows
queue.put_nowait(' '.join(command)+"\n")
try:
output=subprocess.check_output(command,stderr=subprocess.STDOUT)
queue.put_nowait(output)
except subprocess.CalledProcessError as error:
# post the precise message and the remaining output as a tuple
queue.put_nowait((tool,error.returncode,error.output))
def is_wesnoth_tools_path(path):
"""Checks if the supplied path may be a wesnoth/data/tools directory"""
lower_path=path.lower()
if "wesnoth" in lower_path and \
"data" in lower_path and \
"tools" in lower_path:
return True
return False
def attach_context_menu(widget,function):
# on Mac the right button fires a Button-2 event, or so I'm told
# some mice don't even have two buttons, so the user is forced
# to use Command + the only button
# bear in mind that I don't have a Mac, so this point may be bugged
# bind also the context menu key, for those keyboards that have it
# that is, most of the Windows and Linux ones (however, in Win it's
# called App, while on Linux is called Menu)
# Mac doesn't have a context menu key on its keyboards, so no binding
# bind also the Shift+F10 shortcut (same as Menu/App key)
# the call to tk windowingsystem is justified by the fact
# that it is possible to install X11 over Darwin
windowingsystem = widget.tk.call('tk', 'windowingsystem')
if windowingsystem == "win32": # Windows, both 32 and 64 bit
widget.bind("<Button-3>", function)
widget.bind("<KeyPress-App>", function)
widget.bind("<Shift-KeyPress-F10>", function)
elif windowingsystem == "aqua": # MacOS with Aqua
widget.bind("<Button-2>", function)
widget.bind("<Control-Button-1>", function)
elif windowingsystem == "x11": # Linux, FreeBSD, Darwin with X11
widget.bind("<Button-3>", function)
widget.bind("<KeyPress-Menu>", function)
widget.bind("<Shift-KeyPress-F10>", function)
def attach_select_all(widget,function):
# bind the "select all" key shortcut
# again, Mac uses Command instead of Control
windowingsystem = widget.tk.call('tk', 'windowingsystem')
if windowingsystem == "win32":
widget.bind("<Control-KeyRelease-a>", function)
elif windowingsystem == "aqua":
widget.bind("<Command-KeyRelease-a>", function)
elif windowingsystem == "x11":
widget.bind("<Control-KeyRelease-a>", function)
class Popup(Toplevel):
def __init__(self,parent,tool,thread):
"""Creates a popup that informs the user that the desired tool is running.
Self destroys when the tool thread is over"""
self.thread=thread
if sys.version_info.major>=3:
super().__init__(parent)
else:
Toplevel.__init__(self,parent)
self.transient(parent)
self.grab_set()
self.protocol("WM_DELETE_WINDOW",
lambda: None) # disable close button
self.resizable(width=False,
height=False)
frame=Frame(self)
frame.pack(fill=BOTH,expand=YES)
wait_label=Label(frame,
text="{0} is running\nPlease wait...".format(tool),
justify=CENTER)
wait_label.grid(row=0,
column=0,
padx=5,
pady=5)
wait_progress=Progressbar(frame,
mode="indeterminate")
wait_progress.grid(row=1,
column=0,
sticky=E+W,
padx=5,
pady=5)
frame.columnconfigure(0,weight=1)
# place the popup in the middle of the main window
# get the main window position and dimension
self.geometry("{0}x{1}+{2}+{3}".format(400,
80,
int(root.winfo_rootx()+(root.winfo_width()-400)/2),
int(root.winfo_rooty()+(root.winfo_height()-80)/2)))
wait_progress.start(10)
self.check_thread_alive()
def check_thread_alive(self):
"""Checks if the thread is still alive, and destroys the window if it isn't"""
# placing this in a for or while cycle freezes the app
# so we need to use the .after method and recursively call the function
# that's one of the many quirks of Tkinter
if self.thread.isAlive():
self.after(100,self.check_thread_alive)
else:
self.after(1,self.destroy)
class ContextMenu(Menu):
def __init__(self,x,y,widget):
"""A subclass of Menu, used to display a context menu in Text and Entry widgets
If the widget isn't active, some options do not appear"""
if sys.version_info.major>=3:
super().__init__(None,tearoff=0) # otherwise Tk allows splitting it in a new window
else:
Menu.__init__(self,None,tearoff=0)
self.widget=widget
# MacOS uses a key called Command, instead of the usual Control used by Windows and Linux
# so prepare the accelerator strings accordingly
# For future reference, Mac also uses Option instead of Alt
# also, a little known fact about Python is that it *does* support using the ternary operator
# like in this case
control_key = "Command" if self.tk.call('tk', 'windowingsystem') == "aqua" else "Ctrl"
# str is necessary because in some instances a Tcl_Obj is returned instead of a string
if str(widget.cget('state')) in (ACTIVE,NORMAL): # do not add if state is readonly or disabled
self.add_command(label="Cut",
image=ICONS['cut'],
compound=LEFT,
accelerator='%s+X' % (control_key),
command=lambda: self.widget.event_generate("<<Cut>>"))
self.add_command(label="Copy",
image=ICONS['copy'],
compound=LEFT,
accelerator='%s+C' % (control_key),
command=lambda: self.widget.event_generate("<<Copy>>"))
if str(widget.cget('state')) in (ACTIVE,NORMAL):
self.add_command(label="Paste",
image=ICONS['paste'],
compound=LEFT,
accelerator='%s+V' % (control_key),
command=lambda: self.widget.event_generate("<<Paste>>"))
self.add_separator()
self.add_command(label="Select all",
image=ICONS['select_all'],
compound=LEFT,
accelerator='%s+A' % (control_key),
command=self.on_select_all)
self.tk_popup(x,y) # self.post does not destroy the menu when clicking out of it
def on_select_all(self):
# disabled Text widgets have a different way to handle selection
if isinstance(self.widget,Text):
# adding a SEL tag to a chunk of text causes it to be selected
self.widget.tag_add(SEL,"1.0",END)
elif isinstance(self.widget,Entry) or \
isinstance(self.widget,Combobox):
# apparently, the <<SelectAll>> event doesn't fire correctly if the widget is readonly
self.widget.select_range(0,END)
elif isinstance(self.widget,Spinbox):
self.widget.selection("range",0,END)
class EntryContext(Entry):
def __init__(self,parent,**kwargs):
"""An enhanced Entry widget that has a right-click menu
Use like any other Entry widget"""
if sys.version_info.major>=3:
super().__init__(parent,**kwargs)
else:
Entry.__init__(self,parent,**kwargs)
attach_context_menu(self,self.on_context_menu)
attach_select_all(self,self.on_select_all)
def on_context_menu(self,event):
if str(self.cget('state')) != DISABLED:
ContextMenu(event.x_root,event.y_root,event.widget)
def on_select_all(self,event):
self.select_range(0,END)
class SpinboxContext(Spinbox):
def __init__(self,parent,**kwargs):
"""An enhanced Spinbox widget that has a right-click menu
Use like any other Spinbox widget"""
if sys.version_info.major>=3:
super().__init__(parent,**kwargs)
else:
Spinbox.__init__(self,parent,**kwargs)
attach_context_menu(self,self.on_context_menu)
attach_select_all(self,self.on_select_all)
def on_context_menu(self,event):
if str(self.cget('state')) != DISABLED:
ContextMenu(event.x_root,event.y_root,event.widget)
def on_select_all(self,event):
self.selection("range",0,END)
class EnhancedText(Text):
def __init__(self,*args,**kwargs):
"""A subclass of Text with a context menu
Use it like any other Text widget"""
if sys.version_info.major>=3:
super().__init__(*args,**kwargs)
else:
Text.__init__(self,*args,**kwargs)
attach_context_menu(self,self.on_context_menu)
attach_select_all(self,self.on_select_all)
def on_context_menu(self,event):
# the disabled state in a Text widget is pretty much
# like the readonly state in Entry, hence no state check
ContextMenu(event.x_root,event.y_root,event.widget)
def on_select_all(self,event):
self.tag_add(SEL,"1.0",END)
class SelectDirectory(LabelFrame):
def __init__(self,parent,textvariable=None,**kwargs):
"""A subclass of LabelFrame sporting a readonly Entry and a Button with a folder icon.
It comes complete with a context menu and a directory selection screen"""
if sys.version_info.major>=3:
super().__init__(parent,text="Directory",**kwargs)
else:
LabelFrame.__init__(self,parent,text="Directory",**kwargs)
self.textvariable=textvariable
self.dir_entry=EntryContext(self,
width=40,
textvariable=self.textvariable,
state="readonly")
self.dir_entry.pack(side=LEFT,
fill=BOTH,
expand=YES)
self.dir_button=Button(self,
image=ICONS['browse'],
compound=LEFT,
text="Browse...",
command=self.on_browse)
self.dir_button.pack(side=LEFT)
self.clear_button=Button(self,
image=ICONS['clear16'],
compound=LEFT,
text="Clear",
command=self.on_clear)
self.clear_button.pack(side=LEFT)
def on_browse(self):
# if the user already selected a directory, try to use it
current_dir=self.textvariable.get()
if os.path.exists(current_dir):
directory=askdirectory(initialdir=current_dir)
# otherwise attempt to detect the user's userdata folder
else:
# os.path.expanduser gets the current user's home directory on every platform
if sys.platform=="win32":
# get userdata directory on Windows
# it assumes that you choose to store userdata in the My Games directory
# while installing Wesnoth
userdata=os.path.join(os.path.expanduser("~"),
"Documents",
"My Games",
"Wesnoth"+WESNOTH_SERIES,
"data",
"add-ons")
elif sys.platform.startswith("linux") or "bsd" in sys.platform:
# we're on Linux or on a BSD system like FreeBSD
userdata=os.path.join(os.path.expanduser("~"),
".local",
"share",
"wesnoth",
WESNOTH_SERIES,
"data",
"add-ons")
elif sys.platform=="darwin": # we're on MacOS
# bear in mind that I don't have a Mac, so this point may be bugged
userdata=os.path.join(os.path.expanduser("~"),
"Library",
"Application Support",
"Wesnoth_"+WESNOTH_SERIES,
"data",
"add-ons")
else: # unknown system; if someone else wants to add other rules, be my guest
userdata="."
if os.path.exists(userdata): # we may have gotten it wrong
directory=askdirectory(initialdir=userdata)
else:
directory=askdirectory(initialdir=".")
if directory:
# use os.path.normpath, so on Windows the usual backwards slashes are correctly shown
self.textvariable.set(os.path.normpath(directory))
def on_clear(self):
self.textvariable.set("")
class WmllintTab(Frame):
def __init__(self,parent):
# it means super(WmllintTab,self), that in turn means
# Frame.__init__(self,parent)
if sys.version_info.major>=3:
super().__init__(parent)
else:
Frame.__init__(self,parent)
self.mode_variable=IntVar()
self.mode_frame=LabelFrame(self,
text="wmllint mode")
self.mode_frame.grid(row=0,
column=0,
sticky=N+E+S+W)
self.radio_normal=Radiobutton(self.mode_frame,
text="Normal",
variable=self.mode_variable,
value=0)
self.radio_normal.grid(row=0,
column=0,
sticky=W,
padx=10)
self.radio_dryrun=Radiobutton(self.mode_frame,
text="Dry run\nDo not perform changes",
variable=self.mode_variable,
value=1)
self.radio_dryrun.grid(row=1,
column=0,
sticky=W,
padx=10)
self.radio_clean=Radiobutton(self.mode_frame,
text="Clean\nDelete *.bak files",
variable=self.mode_variable,
value=2)
self.radio_clean.grid(row=2,
column=0,
sticky=W,
padx=10)
self.radio_diff=Radiobutton(self.mode_frame,
text="Diff\nShow differences in converted files",
variable=self.mode_variable,
value=3)
self.radio_diff.grid(row=3,
column=0,
sticky=W,
padx=10)
self.radio_revert=Radiobutton(self.mode_frame,
text="Revert\nRevert conversions using *.bak files",
variable=self.mode_variable,
value=4)
self.radio_revert.grid(row=4,
column=0,
sticky=W,
padx=10)
self.verbosity_frame=LabelFrame(self,
text="Verbosity level")
self.verbosity_frame.grid(row=0,
column=1,
sticky=N+E+S+W)
self.verbosity_variable=IntVar()
self.radio_v0=Radiobutton(self.verbosity_frame,
text="Terse",
variable=self.verbosity_variable,
value=0)
self.radio_v0.grid(row=0,
column=0,
sticky=W,
padx=10)
self.radio_v1=Radiobutton(self.verbosity_frame,
text="List changes",
variable=self.verbosity_variable,
value=1)
self.radio_v1.grid(row=1,
column=0,
sticky=W,
padx=10)
self.radio_v2=Radiobutton(self.verbosity_frame,
text="Name files\nbefore processing",
variable=self.verbosity_variable,
value=2)
self.radio_v2.grid(row=2,
column=0,
sticky=W,
padx=10)
self.radio_v3=Radiobutton(self.verbosity_frame,
text="Show parse details",
variable=self.verbosity_variable,
value=3)
self.radio_v3.grid(row=3,
column=0,
sticky=W,
padx=10)
self.options_frame=LabelFrame(self,
text="wmllint options")
self.options_frame.grid(row=0,
column=2,
sticky=N+E+S+W)
self.stripcr_variable=BooleanVar()
self.stripcr_check=Checkbutton(self.options_frame,
text="Convert EOL characters to Unix style",
variable=self.stripcr_variable)
self.stripcr_check.grid(row=0,
column=0,
sticky=W,
padx=10)
self.missing_variable=BooleanVar()
self.missing_check=Checkbutton(self.options_frame,
text="Don't warn about tags without side= keys",
variable=self.missing_variable)
self.missing_check.grid(row=1,
column=0,
sticky=W,
padx=10)
self.known_variable=BooleanVar()
self.known_check=Checkbutton(self.options_frame,
text="Disable checks for unknown units",
variable=self.known_variable)
self.known_check.grid(row=2,
column=0,
sticky=W,
padx=10)
self.spell_variable=BooleanVar()
self.spell_check=Checkbutton(self.options_frame,
text="Disable spellchecking",
variable=self.spell_variable)
self.spell_check.grid(row=3,
column=0,
sticky=W,
padx=10)
self.freeze_variable=BooleanVar()
self.freeze_check=Checkbutton(self.options_frame,
text="Ignore newlines in messages",
variable=self.freeze_variable)
self.freeze_check.grid(row=4,
column=0,
sticky=W,
padx=10)
self.skip_variable=BooleanVar()
self.skip_core=Checkbutton(self.options_frame,
text="Skip core directory",
variable=self.skip_variable,
command=self.skip_core_dir_callback)
self.skip_core.grid(row=5,
column=0,
sticky=W,
padx=10)
self.columnconfigure(0,weight=1)
self.columnconfigure(1,weight=1)
self.columnconfigure(2,weight=1)
def skip_core_dir_callback(self):
# if Skip core directory is enabled
# avoid checking for unknown unit types
if self.skip_variable.get():
self.known_variable.set(True)
self.known_check.configure(state=DISABLED)
else:
self.known_variable.set(False)
self.known_check.configure(state=NORMAL)
class WmlscopeTab(Frame):
def __init__(self,parent):
if sys.version_info.major>=3:
super().__init__(parent)
else:
Frame.__init__(self,parent)
self.options_frame=LabelFrame(self,
text="wmlscope options")
self.options_frame.grid(row=0,
column=0,
sticky=N+E+S+W)
self.normal_options=Frame(self.options_frame)
self.normal_options.grid(row=0,
column=0,
sticky=N+E+S+W)
self.crossreference_variable=BooleanVar() # equivalent to warnlevel 1
self.crossreference_check=Checkbutton(self.normal_options,
text="Check for duplicate macro definitions",
variable=self.crossreference_variable)
self.crossreference_check.grid(row=0,
column=0,
sticky=W,
padx=10)
self.collisions_variable=BooleanVar()
self.collisions_check=Checkbutton(self.normal_options,
text="Check for duplicate resource files",
variable=self.collisions_variable)
self.collisions_check.grid(row=1,
column=0,
sticky=W,
padx=10)
self.definitions_variable=BooleanVar()
self.definitions_check=Checkbutton(self.normal_options,
text="Make definition list",
variable=self.definitions_variable)
self.definitions_check.grid(row=2,
column=0,
sticky=W,
padx=10)
self.listfiles_variable=BooleanVar()
self.listfiles_check=Checkbutton(self.normal_options,
text="List files that will be processed",
variable=self.listfiles_variable)
self.listfiles_check.grid(row=3,
column=0,
sticky=W,
padx=10)
self.unresolved_variable=BooleanVar()
self.unresolved_check=Checkbutton(self.normal_options,
text="Report unresolved macro references",
variable=self.unresolved_variable)
self.unresolved_check.grid(row=4,
column=0,
sticky=W,
padx=10)
self.extracthelp_variable=BooleanVar()
self.extracthelp_check=Checkbutton(self.normal_options,
text="Extract help from macro definition comments",
variable=self.extracthelp_variable)
self.extracthelp_check.grid(row=5,
column=0,
sticky=W,
padx=10)
self.unchecked_variable=BooleanVar()
self.unchecked_check=Checkbutton(self.normal_options,
text="Report all macros with untyped formals",
variable=self.unchecked_variable)
self.unchecked_check.grid(row=6,
column=0,
sticky=W,
padx=10)
self.progress_variable=BooleanVar()
self.progress_check=Checkbutton(self.normal_options,
text="Show progress",
variable=self.progress_variable)
self.progress_check.grid(row=7,
column=0,
sticky=W,
padx=10)
self.separator=Separator(self.options_frame,
orient=VERTICAL)
self.separator.grid(row=0,
column=1,
sticky=N+S)
self.options_with_regexp=Frame(self.options_frame)
self.options_with_regexp.grid(row=0,
column=2,
sticky=N+E+S+W)
self.exclude_variable=BooleanVar()
self.exclude_check=Checkbutton(self.options_with_regexp,
text="Exclude files matching regexp:",
variable=self.exclude_variable,
command=self.exclude_callback)
self.exclude_check.grid(row=0,
column=0,
sticky=W,
padx=10)
self.exclude_regexp=StringVar()
self.exclude_entry=EntryContext(self.options_with_regexp,
textvariable=self.exclude_regexp,
state=DISABLED)
self.exclude_entry.grid(row=0,
column=1,
sticky=E+W,
padx=10)
self.from_variable=BooleanVar()
self.from_check=Checkbutton(self.options_with_regexp,
text="Exclude files not matching regexp:",
variable=self.from_variable,
command=self.from_callback)
self.from_check.grid(row=1,
column=0,
sticky=W,
padx=10)
self.from_regexp=StringVar()
self.from_entry=EntryContext(self.options_with_regexp,
textvariable=self.from_regexp,
state=DISABLED)
self.from_entry.grid(row=1,
column=1,
sticky=E+W,
padx=10)
self.refcount_variable=BooleanVar()
self.refcount_check=Checkbutton(self.options_with_regexp,
text="Report only on macros referenced\nin at least n files:",
variable=self.refcount_variable,
command=self.refcount_callback)
self.refcount_check.grid(row=2,
column=0,
sticky=W,
padx=10)
self.refcount_number=IntVar()
self.refcount_spin=SpinboxContext(self.options_with_regexp,
from_=0,to=999,
textvariable=self.refcount_number,
width=3,
state=DISABLED)
self.refcount_spin.grid(row=2,
column=1,
sticky=E+W,
padx=10)
self.typelist_variable=BooleanVar()
self.typelist_check=Checkbutton(self.options_with_regexp,
text="List actual & formal argtypes\nfor calls in fname",
variable=self.typelist_variable,
command=self.typelist_callback)
self.typelist_check.grid(row=3,
column=0,
sticky=W,
padx=10)
self.typelist_string=StringVar()
self.typelist_entry=EntryContext(self.options_with_regexp,
textvariable=self.typelist_string,
state=DISABLED)
self.typelist_entry.grid(row=3,
column=1,
sticky=E+W,
padx=10)
self.force_variable=BooleanVar()
self.force_check=Checkbutton(self.options_with_regexp,
text="Ignore refcount 0 on names\nmatching regexp:",
variable=self.force_variable,
command=self.force_callback)
self.force_check.grid(row=4,
column=0,
sticky=W,
padx=10)
self.force_regexp=StringVar()
self.force_entry=EntryContext(self.options_with_regexp,
textvariable=self.force_regexp,
state=DISABLED)
self.force_entry.grid(row=4,
column=1,
sticky=E+W,
padx=10)
self.columnconfigure(0,weight=1)
self.options_frame.columnconfigure(0,weight=1)
self.options_frame.columnconfigure(2,weight=1)
self.options_with_regexp.columnconfigure(1,weight=1)
# uniform= makes the options in the options_with_regexp frame evenly sized
# please note that "regexp" is an ID of the widget group, not a special value
# you can replace "regexp" with "bacon" and it still works...
for row in range(5):
self.options_with_regexp.rowconfigure(row,uniform="regexp")
def exclude_callback(self):
if self.exclude_variable.get():
self.exclude_entry.configure(state=NORMAL)
else:
self.exclude_entry.configure(state=DISABLED)
def from_callback(self):
if self.from_variable.get():
self.from_entry.configure(state=NORMAL)
else:
self.from_entry.configure(state=DISABLED)
def refcount_callback(self):
if self.refcount_variable.get():
self.refcount_spin.configure(state="readonly")
else:
self.refcount_spin.configure(state=DISABLED)
def typelist_callback(self):
if self.typelist_variable.get():
self.typelist_entry.configure(state=NORMAL)
else:
self.typelist_entry.configure(state=DISABLED)
def force_callback(self):
if self.force_variable.get():
self.force_entry.configure(state=NORMAL)
else:
self.force_entry.configure(state=DISABLED)
class WmlindentTab(Frame):
def __init__(self,parent):
if sys.version_info.major>=3:
super().__init__(parent)
else:
Frame.__init__(self,parent)
self.mode_variable=IntVar()
self.mode_frame=LabelFrame(self,
text="wmlindent mode")
self.mode_frame.grid(row=0,
column=0,
sticky=N+E+S+W)
self.radio_normal=Radiobutton(self.mode_frame,
text="Normal",
variable=self.mode_variable,
value=0)
self.radio_normal.grid(row=0,
column=0,
sticky=W,
padx=10)
self.radio_dryrun=Radiobutton(self.mode_frame,
text="Dry run\nDo not perform changes",
variable=self.mode_variable,
value=1)
self.radio_dryrun.grid(row=1,
column=0,
sticky=W,
padx=10)
self.verbosity_frame=LabelFrame(self,
text="Verbosity level")
self.verbosity_frame.grid(row=0,
column=1,
sticky=N+E+S+W)
self.verbosity_variable=IntVar()
self.radio_v0=Radiobutton(self.verbosity_frame,
text="Terse",
variable=self.verbosity_variable,
value=0)
self.radio_v0.grid(row=0,
column=0,
sticky=W,
padx=10)
self.radio_v1=Radiobutton(self.verbosity_frame,
text="Verbose",
variable=self.verbosity_variable,
value=1)
self.radio_v1.grid(row=1,
column=0,
sticky=W,
padx=10)
self.radio_v2=Radiobutton(self.verbosity_frame,
text="Also report\nunchanged files",
variable=self.verbosity_variable,
value=2)
self.radio_v2.grid(row=2,
column=0,
sticky=W,
padx=10)
self.options_frame=LabelFrame(self,
text="wmlindent options")
self.options_frame.grid(row=0,
column=2,
sticky=N+E+S+W)
self.exclude_variable=BooleanVar()
self.exclude_check=Checkbutton(self.options_frame,
text="Exclude files\nmatching regexp:",
variable=self.exclude_variable,
command=self.exclude_callback)
self.exclude_check.grid(row=1,
column=0,
columnspan=2,
sticky=W,
padx=10)
self.regexp_variable=StringVar()
self.regexp_entry=EntryContext(self.options_frame,
textvariable=self.regexp_variable,
state=DISABLED)
self.regexp_entry.grid(row=1,
column=1,
sticky=E+W,
padx=10)
self.quiet_variable=BooleanVar()
self.quiet_check=Checkbutton(self.options_frame,
text="Do not generate output",
variable=self.quiet_variable)
self.quiet_check.grid(row=2,
column=0,
sticky=W,
padx=10)
self.columnconfigure(0,weight=1)
self.columnconfigure(1,weight=1)
self.columnconfigure(2,weight=1)
self.options_frame.columnconfigure(1,weight=1)
def exclude_callback(self):
if self.exclude_variable.get():
self.regexp_entry.configure(state=NORMAL)
else:
self.regexp_entry.configure(state=DISABLED)
class MainFrame(Frame):
def __init__(self,parent):
self.parent=parent
if sys.version_info.major>=3:
self.queue=queue.Queue()
super().__init__(parent)
else:
self.queue=Queue.Queue()
Frame.__init__(self,parent)
self.grid(sticky=N+E+S+W)
self.buttonbox=Frame(self)
self.buttonbox.grid(row=0,
column=0,
sticky=E+W)
self.run_button=Button(self.buttonbox,
text="Run wmllint",
image=ICONS['run'],
compound=LEFT,
width=15, # to avoid changing size when callback is called
command=self.on_run_wmllint)
self.run_button.pack(side=LEFT,
padx=5,
pady=5)
self.save_button=Button(self.buttonbox,
text="Save as text...",
image=ICONS['save'],
compound=LEFT,
command=self.on_save)
self.save_button.pack(side=LEFT,
padx=5,
pady=5)
self.clear_button=Button(self.buttonbox,
text="Clear output",
image=ICONS['clear'],
compound=LEFT,
command=self.on_clear)
self.clear_button.pack(side=LEFT,
padx=5,
pady=5)
self.about_button=Button(self.buttonbox,
text="About...",
image=ICONS['about'],
compound=LEFT,
command=self.on_about)
self.about_button.pack(side=LEFT,
padx=5,
pady=5)
self.exit_button=Button(self.buttonbox,
text="Exit",
image=ICONS['exit'],
compound=LEFT,
command=parent.destroy)
self.exit_button.pack(side=RIGHT,
padx=5,
pady=5)
self.dir_variable=StringVar()
self.dir_frame=SelectDirectory(self,
textvariable=self.dir_variable)
self.dir_frame.grid(row=1,
column=0,
sticky=E+W)
# Notebook is one of the new widgets introduced by ttk
# it isn't available on Python 2.6 and lower, like the rest of ttk widgets
# please note that the Frames that become tabs don't need to be packed or gridded
self.notebook=Notebook(self)
self.notebook.grid(row=2,
column=0,
sticky=E+W)
self.wmllint_tab=WmllintTab(None)
self.notebook.add(self.wmllint_tab,
text="wmllint",
sticky=N+E+S+W)
self.wmlscope_tab=WmlscopeTab(None)
self.notebook.add(self.wmlscope_tab,
text="wmlscope",
sticky=N+E+S+W)
self.wmlindent_tab=WmlindentTab(None)
self.notebook.add(self.wmlindent_tab,
text="wmlindent",
sticky=N+E+S+W)
self.output_frame=LabelFrame(self,
text="Output")
self.output_frame.grid(row=3,
column=0,
sticky=N+E+S+W)
# in former versions of this script, I disabled the text widget at its creation
# it turned out that doing so on Aqua (Mac OS) causes the widget to ignore
# any additional binding set after its disabling
# the subclass EnhancedText first calls the constructor of the original Text widget
# and only later it creates its own bindings
# so first create the widget, and disable it later
self.text=EnhancedText(self.output_frame,
wrap=WORD,
takefocus=True)
self.text.configure(state=DISABLED)
self.text.grid(row=0,
column=0,
sticky=N+E+S+W)
self.update_text()
self.yscrollbar=Scrollbar(self.output_frame,
command=self.text.yview)
self.yscrollbar.grid(row=0,
column=1,
sticky=N+S)
self.text["yscrollcommand"]=self.yscrollbar.set
self.xscrollbar=Scrollbar(self.output_frame,
orient=HORIZONTAL,
command=self.text.xview)
self.xscrollbar.grid(row=1,
column=0,
sticky=E+W)
self.text["xscrollcommand"]=self.xscrollbar.set
self.grip=Sizegrip(self.output_frame)
self.grip.grid(row=1,column=1)
self.output_frame.rowconfigure(0,weight=1)
self.output_frame.columnconfigure(0,weight=1)
self.columnconfigure(0,weight=1)
self.rowconfigure(3,weight=1)
self.notebook.bind("<<NotebookTabChanged>>",self.tab_callback)
# this allows using the mouse wheel even on the disabled Text widget
# without the need to clic on said widget
self.tk_focusFollowsMouse()
def tab_callback(self,event):
# we check the ID of the active tab and ask its position
# the order of the tabs is pretty obvious
active_tab=self.notebook.index(self.notebook.select())
if active_tab==0:
self.run_button.configure(text="Run wmllint",command=self.on_run_wmllint)
elif active_tab==1:
self.run_button.configure(text="Run wmlscope",command=self.on_run_wmlscope)
elif active_tab==2:
self.run_button.configure(text="Run wmlindent",command=self.on_run_wmlindent)
def on_run_wmllint(self):
# first of all, check if we have something to run wmllint on it
# if not, stop here
umc_dir=self.dir_variable.get()
if not umc_dir and self.wmllint_tab.skip_variable.get():
showerror("Error","""wmllint cannot run because there is no directory selected.
Please select a directory or disable the "Skip core directory" option""")
return
# build the command line
wmllint_command_string=[]
# get the path of the Python interpreter
wmllint_command_string.append(sys.executable)
# get the path of the desired tool (wmllint, in this case)
wmllint_command_string.append(os.path.join(APP_DIR,"wmllint"))
mode=self.wmllint_tab.mode_variable.get()
if mode==0:
pass
elif mode==1:
wmllint_command_string.append("--dryrun")
elif mode==2:
wmllint_command_string.append("--clean")
elif mode==3:
wmllint_command_string.append("--diff")
elif mode==4:
wmllint_command_string.append("--revert")
verbosity=self.wmllint_tab.verbosity_variable.get()
for n in range(verbosity):
wmllint_command_string.append("-v")
if self.wmllint_tab.stripcr_variable.get():
wmllint_command_string.append("--stripcr")
if self.wmllint_tab.missing_variable.get():
wmllint_command_string.append("--missing")
if self.wmllint_tab.known_variable.get():
wmllint_command_string.append("--known")
if self.wmllint_tab.spell_variable.get():
wmllint_command_string.append("--nospellcheck")