-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMySimpleGUI.py
1069 lines (942 loc) · 35.1 KB
/
MySimpleGUI.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
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
# __ __ ____ _ _ ____ _ _ ___
# | \/ | _ _ / ___| (_) _ __ ___ _ __ | | ___ / ___|| | | ||_ _|
# | |\/| || | | |\___ \ | || '_ ` _ \ | '_ \ | | / _ \| | _ | | | | | |
# | | | || |_| | ___) || || | | | | || |_) || || __/| |_| || |_| | | |
# |_| |_| \__, ||____/ |_||_| |_| |_|| .__/ |_| \___| \____| \___/ |___|
# |___/ |_|
#
# See https://github.com/salabim/MySimpleGUI/blob/master/README.md for details
import sys
from pathlib import Path
import os
import collections
import datetime
pysimplegui_name = "PySimpleGUI"
pysimplegui_patched_name = pysimplegui_name + "_patched"
__version__ = "1.1.19"
class peekable:
def __init__(self, iterable):
self.iterator = iter(iterable)
self.peek_values = []
def peek(self):
if not self.peek_values:
self.peek_values.append(next(self.iterator))
return self.peek_values[0]
def __iter__(self):
return self
def __next__(self):
if self.peek_values:
return self.peek_values.pop()
else:
return next(self.iterator)
registered_patches = collections.defaultdict(int)
registered_patches[37] = 0
def register_patch(n):
registered_patches[n] += 1
def splitlines(s):
return s.splitlines()
def line_to_indent(line):
indent = 0
for i, c in enumerate(line):
if c == " ":
indent += 1
else:
if c == "#":
return float("inf")
return indent
return float("inf")
class CodeList(list):
def add(self, spec, indent=0):
if isinstance(spec, str):
if spec == "":
lines = [""]
else:
lines = spec.splitlines()
else:
lines = spec
indentstr = " " * indent
for line in lines:
self.append(indentstr + line.replace("\x1b", "\\x1b"))
for path in sys.path:
pysimplegui_path = Path(path) / (pysimplegui_name + ".py")
if pysimplegui_path.is_file():
break
pysimplegui_path = Path(path) / pysimplegui_name / (pysimplegui_name + ".py")
if pysimplegui_path.is_file():
break
else:
raise ImportError(pysimplegui_name + ".py" + " not found")
mysimplegui_path = Path(__file__)
pysimplegui_patched_path = mysimplegui_path.parent / (pysimplegui_patched_name + ".py")
patch_info = "# patch info "
stat = os.stat(pysimplegui_path)
patch_info += "{st_mtime:16.3f}{st_size:8d}".format(st_mtime=stat.st_mtime, st_size=stat.st_size)
stat = os.stat(mysimplegui_path)
patch_info += "#{st_mtime:16.3f}{st_size:8d}".format(st_mtime=stat.st_mtime, st_size=stat.st_size)
pysimplegui_patched_match = False
if pysimplegui_patched_path.is_file():
try:
with open(pysimplegui_patched_path, "rb") as f:
f.seek(-len(patch_info), os.SEEK_END)
read_patch_info = f.read().decode("utf-8")
if read_patch_info == patch_info:
pysimplegui_patched_match = True
except OSError:
pass
if not pysimplegui_patched_match:
with open(pysimplegui_path, "r", encoding="utf-8") as f:
lines = peekable(f.read().splitlines())
code = CodeList()
while lines.peek().startswith("#!"):
code.add(next(lines))
code.add(
"""\
# This is {pysimplegui_name}.py patched by MySimpleGUI version {version}
# Patches (c)2020 Ruud van der Ham, salabim.org
""".format(
version=__version__, pysimplegui_name=pysimplegui_name
)
)
this_class = "?"
for line in lines:
if " is True" in line and line.strip()[0] != ":":
s = line.split("is True", 1)[0]
s1 = s.split()[-1]
if s1 + " is True" not in line:
register_patch(37)
line = line.replace(s1 + " is True", "bool(" + s1 + ")")
if " is False" in line and line.strip()[0] != ":":
s = line.split("is False", 1)[0]
s1 = s.split()[-1]
if s1 + " is False" not in line:
register_patch(37)
line = line.replace(s1 + " is False", "(not bool(" + s1 + ") and " + s1 + " is not None)")
if " == True" in line and line.strip()[0] != ":":
s = line.split("== True", 1)[0]
s1 = s.split()[-1]
if "'" not in s1:
if s1 + " == True" not in line:
print('***', line)
register_patch(37)
line = line.replace(s1 + " == True", "bool(" + s1 + ")")
if " == False" in line and line.strip()[0] != ":":
s = line.split("== False", 1)[0]
s1 = s.split()[-1]
if s1 + " == False" not in line:
print('***', line)
register_patch(37)
line = line.replace(s1 + " == False", "(not bool(" + s1 + ") and " + s1 + " is not None)")
if line.startswith("class "):
parts = line.split()
if len(parts) > 1:
this_class = parts[1].split("(")[0].replace(":", "").strip()
if line.strip().startswith("self.config_count = 0"):
register_patch(0)
code.add(line)
indent = line_to_indent(line)
code.add(
"""\
self.internal = Window.Internal(self)""",
indent=indent,
)
elif "def read(self, timeout=none, timeout_key=timeout_key, close=false):" in line.lower(): # adds attributes to Read
register_patch(1)
indent = line_to_indent(line)
code.add(
"""\
@staticmethod
def _lookup(d, key):
try:
return d[key]
except KeyError:
pass
if isinstance(key, str):
for prefix in ("key", "k", "_"):
if key.startswith(prefix):
try:
return d[int(key[len(prefix) :])]
except (ValueError, KeyError):
pass
matches = []
for name in d.keys():
if isinstance(name, str):
norm_name = name
norm_name = norm_name.replace(WRITE_ONLY_KEY, "")
norm_name = norm_name.replace(TIMEOUT_KEY, "")
if not name or name[0].isdigit() or keyword.iskeyword(name):
norm_name = "_" + norm_name
norm_name = "".join(ch if ("A"[:i] + ch).isidentifier() else "_" for i, ch in enumerate(norm_name))
if norm_name == key:
matches.append(name)
if len(matches) == 1:
return d[matches[0]]
if len(matches) > 1:
raise KeyError("multiple matches for key " + repr(key) + " : " + repr(matches))
raise KeyError(key)
def type_name(self):
return "Window id: " + str(id(self))
def __repr__(self):
result = [self.type_name()]
if not hasattr(self, "taken"):
self.taken = True
for k in sorted(self.__dict__.keys(), key=str.upper):
v = self.__dict__[k]
if v not in (None, (None,None)):
if k in ("ParentContainer", "ParentForm"):
if hasattr(v, "type_name"):
result.append(" " + k + " = " + v.type_name())
else:
result.append(" " + k + " = " + repr(v))
else:
if k not in ("AllKeysDict"):
if k == "WindowIcon":
v = str(v)[:80] + "..."
if isinstance(v, list):
v = list_repr(v)
l = repr(v).split("\\n")
result.append(" " + k + " = " + l[0])
for x in l[1:]:
result.append(" " + x)
for k, v in self.AllKeysDict.items():
result.append(" [" + repr(k) + "] = " + v.type_name())
del self.taken
return "\\n".join(result)
class Internal:
def __init__(self, window):
self._window = window
def __getattr__(self, v):
return super(Window, self._window).__getattribute__(v)
def __getattribute__(self, key):
if key in super().__getattribute__("AllKeysDict"):
trace_details = traceback.format_stack()
if trace_details[-1].split(",")[0] != trace_details[-2].split(",")[0]: # internal or external?
return self[key]
return super().__getattribute__(key)
def __getattr__(self, key):
try:
return self[key]
except KeyError as e:
raise AttributeError(e) from None
""",
indent=indent,
)
code.add(line)
while not lines.peek().strip().startswith("return results"):
code.add(next(lines))
line = next(lines)
indent = line_to_indent(line)
code.add(
"""\
class AttributeDict(collections.UserDict):
def __getitem__(self, key):
return Window._lookup(self.data, key)
def __getattr__(self, key):
try:
return self[key]
except KeyError as e:
raise AttributeError(e) from None
events, values = results
if values is not None:
if isinstance(values, list):
values = {i: v for i, v in enumerate(values)}
values = AttributeDict(values)
results = events, values
""",
indent=indent,
)
code.add(line) # the original return result line
elif line.strip().startswith("def _RightClickMenuCallback(self, event):"):
register_patch(2)
indent = line_to_indent(line)
code.add(
"""\
def disable(self, disabled=True, exclude=[]):
set_state(self, not disabled, exclude)
def enable(self, enabled=True, exclude=[]):
set_state(self, enabled, exclude)
def type_name(self):
type_name = self.Type
type_name = type_name.replace("option menu","OptionMenu")
type_name = type_name.replace("spind","Spin")
type_name = type_name.replace("tabgroup","TabGroup")
type_name = type_name.replace("menubar","Menu")
type_name = type_name.replace("progressbar","ProgressBar")
type_name = type_name.replace("statusbar","StatusBar")
type_name = type_name.capitalize()
return type_name + " id: " + str(id(self))
def __repr__(self):
result = [self.type_name()]
if not hasattr(self, "taken"):
self.taken = True
for k in sorted(self.__dict__.keys(), key=str.upper):
v = self.__dict__[k]
if v not in (None, (None,None)):
if k in ("ParentContainer", "ParentForm"):
if hasattr(v, "type_name"):
result.append(" " + k + " = " + v.type_name())
else:
result.append(" " + k + " = " + repr(v))
else:
if k != "Type":
if isinstance(v, list):
v = list_repr(v)
l = repr(v).split("\\n")
result.append(" " + k + " = " + l[0])
for x in l[1:]:
result.append(" " + x)
del self.taken
return "\\n".join(result)
def freeze(self, enabled=True):
if enabled:
return Column([[self]], pad=(0,0))
else:
return self""",
indent=indent,
)
code.add(line)
elif line.startswith("class Element"):
register_patch(3)
code.add(
"""\
class list_repr(list):
def type_name(self):
return "list"
def __repr__(self):
result = [self.type_name()]
for v in self:
if isinstance(v, list):
v = list_repr(v)
l = repr(v).split("\\n")
for x in l:
result.append(" " + x)
return "\\n".join(result)
def set_state(item, enabled, exclude):
if hasattr(item, "Rows"):
for el in item.Rows:
if el not in exclude:
set_state(el, enabled, exclude)
elif isinstance(item, list):
for el in item:
if el not in exclude:
set_state(el, enabled,exclude)
else:
if item not in exclude:
if enabled:
item.Widget.configure(state="normal")
else:
item.Widget.configure(state="disabled")"""
)
code.add(line)
elif line.strip().startswith("return self.FindElement(key)"): # this the original Window.__getitem__ contents:
register_patch(4)
code.add(line.replace("return self.FindElement(key)", "return Window._lookup(self.AllKeysDict, key)"))
elif line.strip().startswith("if file_name"):
register_patch(5)
code.add(line.replace("file_name", "file_name or self is target_element"))
elif line.strip() == "if folder_name:":
register_patch(6)
code.add(line.replace("folder_name", "True"))
elif "target=target" in line:
register_patch(7)
code.add(line.replace("target=target", "target=target or key or k or button_text"))
elif line.strip().startswith("if element.Type == ELEM_TYPE_INPUT_TEXT:"):
register_patch(8)
code.add(line)
if lines.peek().strip().startswith("try:"):
while True:
line = next(lines)
if line.strip().startswith("elif"):
break
code.add(line)
indent = line_to_indent(line)
code.add(
"""\
elif element.Type == ELEM_TYPE_TEXT:
value = element.TKStringVar.get()""",
indent=indent,
)
code.add(line)
elif "ELEM_TYPE_SEPARATOR):" in line:
register_patch(9)
code.add(line.replace("ELEM_TYPE_SEPARATOR", "ELEM_TYPE_SEPARATOR, ELEM_TYPE_TEXT"))
elif "element.Type != ELEM_TYPE_TEXT and \\" in line:
register_patch(10)
pass # remove this condition
elif "if element.Key in key_dict.keys():" in line:
register_patch(11)
# code.add(line)
indent = line_to_indent(line)
code.add(
"""\
if element.Key == "AllKeysDictxx":
raise KeyError('key "AllKeysDict" not allowed in layout')""",
indent=indent,
)
while line_to_indent(lines.peek()) > indent: # remove original code
next(lines)
elif "self.AllKeysDict = self._BuildKeyDictForWindow(self, self, dict)" in line:
register_patch(34)
indent = line_to_indent(line)
code.add(line)
code.add(
"""\
AllKeysDictnewdict = {}
for k, v in dict.items():
if not(isinstance(k, tuple) and len(k) == 2 and k[0] == NONE_KEY):
AllKeysDictnewdict[k] = v
for k, v in dict.items():
if isinstance(k, tuple) and len(k) == 2 and k[0] == NONE_KEY:
for i in itertools.count(0):
if i not in AllKeysDictnewdict:
k = i
v.Key = i
AllKeysDictnewdict[k] = v
break""",
indent=indent,
)
elif "element.Key = top_window.DictionaryKeyCounter" in line:
register_patch(35)
indent = line_to_indent(line)
code.add("element.Key = (NONE_KEY, top_window.DictionaryKeyCounter)", indent=indent)
elif line.startswith("COLOR_SYSTEM_DEFAULT = "):
register_patch(36)
code.add("NONE_KEY = object()")
code.add(line)
elif line == "class Multiline(Element):":
register_patch(12)
code.add(line)
while not lines.peek().strip().startswith("self."):
code.add(next(lines))
indent = line_to_indent(lines.peek())
code.add(
"""\
self._closed = False
self.write_fg = None
self.write_bg = None
self.write_font = None""",
indent=indent,
)
elif "def write(self, txt):" in line and this_class == "Multiline":
register_patch(13)
indent = line_to_indent(line)
while line_to_indent(lines.peek()) > indent:
next(lines)
code.add(
"""\
class AttributeDict(collections.UserDict):
def __getitem__(self, key):
return self.data[key]
def __getattr__(self, key):
try:
return self[key]
except KeyError as e:
raise AttributeError(e) from None
global ansi
codes = (
("reset", "\x1b[0m", "default", "ondefault"),
("black", "\x1b[30m", "black", None),
("red", "\x1b[31m", "red", None),
("green", "\x1b[32m", "green", None),
("yellow", "\x1b[33m", "yellow", None),
("blue", "\x1b[34m", "blue", None),
("magenta", "\x1b[35m", "magenta", None),
("cyan", "\x1b[36m", "cyan", None),
("white", "\x1b[37m", "white", None),
("default", "\x1b[39m", "default", None),
("onblack", "\x1b[40m", None, "black"),
("onred", "\x1b[41m", None, "red"),
("ongreen", "\x1b[42m", None, "green"),
("onyellow", "\x1b[43m", None, "yellow"),
("onblue", "\x1b[44m", None, "blue"),
("onmagenta", "\x1b[45m", None, "magenta"),
("oncyan", "\x1b[46m", None, "cyan"),
("onwhite", "\x1b[47m", None, "white"),
("ondefault", "\x1b[49m", None, "ondefault"),
("font", "\x1b[font", None, None),
)
code_fg = {}
code_bg = {}
ansi = AttributeDict()
for name, code, fg, bg in codes:
code_fg[code] = fg
code_bg[code] = bg
ansi[name] = code
def write(self, s):
self._check_closed()
while s:
for i, c in enumerate(s):
if c == "\x1b":
for code in Multiline.code_bg:
if s[i:].startswith(code):
if s[i:].startswith("\x1b[font"):
parts = s[i:].split('|',1)
if len(parts) >1:
fontspec = parts[0][6:]
use = parts[0]+"|"
break
else:
use = code
break
else:
continue
_print_to_element(
self, s[:i], sep="", end="", text_color=self.write_fg, background_color=self.write_bg, font=self.write_font,
)
s = s[i + len(use) :]
if Multiline.code_fg[code] is not None:
if Multiline.code_fg[code] == "default":
self.write_fg = None
else:
self.write_fg = Multiline.code_fg[code]
if Multiline.code_bg[code] is not None:
if Multiline.code_bg[code] == "ondefault":
self.write_bg = None
else:
self.write_bg = Multiline.code_bg[code]
if code == "\x1b[0m":
self.write_font = None
if code == "\x1b[font":
self.write_font = fontspec
break
else:
_print_to_element(self, s, sep="", end="", text_color=self.write_fg, background_color=self.write_bg, font=self.write_font)
s = ""
def flush(self):
self._check_closed()
def close(self):
self._closed = True
def _check_closed(self):
if self._closed:
raise ValueError("I/O operation on closed file")
def writable(self):
return not self._closed""",
indent=indent,
)
elif "element.TKText.insert(1.0, element.DefaultText)" in line:
register_patch(14)
code.add(
line.replace(
"element.TKText.insert(1.0, element.DefaultText)", "print(element.DefaultText, file=element)"
)
)
elif "background_color_for_value=background_color" in line:
register_patch(15)
code.add(
line.replace(
"background_color_for_value=background_color",
"background_color_for_value=background_color, font_for_value=font",
)
)
elif "background_color_for_value=None" in line:
register_patch(16)
code.add(
line.replace("background_color_for_value=None", "background_color_for_value=None, font_for_value=None")
)
elif "if background_color_for_value is not None or text_color_for_value is not None:" in line:
register_patch(17)
code.add(line.replace(":", " or font_for_value is not None:"))
elif "str(background_color_for_value)+')'" in line:
register_patch(18)
code.add(
line.replace(
"str(background_color_for_value)+')'",
"str(background_color_for_value)+',' + str(font_for_value)+')'",
)
)
elif "text_color=None, background_color=None" in line and line.strip().startswith(
("def Print", "def print", "def _print_to_element")
):
register_patch(19)
code.add(
line.replace(
"text_color=None, background_color=None", "text_color=None, background_color=None, font=None"
)
)
elif line.strip().startswith("_print_to_element"):
register_patch(20)
code.add(
line.replace(
"text_color=text_color, background_color=background_color",
"text_color=text_color, background_color=background_color, font=font",
)
)
elif "if background_color_for_value is not None:" in line:
register_patch(21)
indent = line_to_indent(line)
code.add(
"""\
if font_for_value is not None:
self.TKText.tag_configure(tag, font=font_for_value)""",
indent=indent,
)
code.add(line)
elif "if element.Key is not None:" in line:
register_patch(22)
code.add(
line.replace(
"if element.Key is not None:",
"if element.Key is not None and isinstance(element.Key, collections.abc.Hashable):",
)
)
elif line.strip().startswith("form.ReturnValuesDictionary[element.Key] = value"):
register_patch(23)
indent = line_to_indent(line)
code.add(
"""\
if isinstance(element.Key, collections.abc.Hashable):
form.ReturnValuesDictionary[element.Key] = value""",
indent=indent,
)
elif line.startswith("def SetOptions(") or line.startswith("def set_options("): # generates extra function to get/set globals
register_patch(24)
names = {}
code.add(line)
while line_to_indent(lines.peek()) > 0:
line = next(lines)
code.add(line)
left, *right = line.split(" = ")
if "#" not in line and "." not in line and line.startswith(" ") and right and not "(" in right[0]:
names[left.strip()] = right[0]
code.add(
"""\
class _TemporaryChange:
def __init__(self, value, global_name, globals):
self.globals=globals
self.save_value = self.globals[global_name]
self.global_name=global_name
self.globals[global_name] = value
def __enter__(self):
return self.save_value
def __exit__(self, *args):
self.globals[self.global_name] = self.save_value
def __call__(self):
return self.globals[self.global_name]"""
)
names["RAISE_ERRORS"] = "raise_errors"
for name in sorted(names, key=lambda x: names[x]):
alias = names[name]
code.add(
"""\
def {alias}(value = None):
global {name}
if value is None:
return {name}
return _TemporaryChange(value, '{name}', globals())""".format(
name=name, alias=alias
)
)
elif line.startswith("def PopupError(") or line.startswith("def popup_error("): # changes PopupError behaviour to raise exception
register_patch(25)
indent = line_to_indent(line)
code.add(line)
while ":" not in lines.peek(): # read till final :
code.add(next(lines))
code.add(next(lines))
code.add(
"""
trace_details = traceback.format_stack()
if (trace_details[-1].split(",")[0] == trace_details[-2].split(",")[0]) and RAISE_ERRORS:
raise RuntimeError("\\n".join(args))""",
indent=indent + 4,
)
elif line == "import queue": # add some required modules
register_patch(26)
code.add(line)
code.add(
"""\
import keyword
import collections
import io"""
)
elif line.startswith("version = "): # check compatibilty of PySimpleGUI version (at patch time)
register_patch(27)
code.add(line)
save__version__ = __version__
exec(line)
__version__ = save__version__
this_pysimplegui_version = version.split()[0]
minimal_pysimplegui_version = "4.27.4"
this_pysimplegui_version_tuple = tuple(map(int, this_pysimplegui_version.split(".")))
minimal_pysimplegui_version_tuple = tuple(map(int, minimal_pysimplegui_version.split(".")))
if this_pysimplegui_version_tuple < minimal_pysimplegui_version_tuple:
raise NotImplementedError(
"MySimpleGUI requires "
+ pysimplegui_name
+ " >= "
+ minimal_pysimplegui_version
+ ", not "
+ this_pysimplegui_version
)
del version
del this_pysimplegui_version
del minimal_pysimplegui_version
elif line.strip() in ("except:", "except Exception as e:"): # exception handler insertion
indent = line_to_indent(line)
exception_line = line
buffered_lines = []
while line_to_indent(lines.peek()) > indent:
line = next(lines)
if line.strip().startswith("warnings.warn"):
line = line.replace("warnings.warn", "print")
buffered_lines.append(line)
requires_raise = False
for line in buffered_lines:
if (
line.strip().startswith("pass")
or line.strip().startswith("return")
or line.strip().startswith("break")
):
requires_raise = False
break
if line.strip().startswith("print("):
requires_raise = True
if requires_raise:
code.add("except Exception as e:", indent=indent)
code.add(
"""\
if RAISE_ERRORS:
save_stdout = sys.stdout
sys.stdout = io.StringIO()""",
indent=indent + 4,
)
code.add(buffered_lines)
code.add(
"""\
if RAISE_ERRORS:
captured = sys.stdout.getvalue()
sys.stdout = save_stdout
if captured:
raise type(e)(str(e) + '\\n' + captured) from None""",
indent=indent + 4,
)
else:
code.add(exception_line)
code.add(buffered_lines)
# elif line.strip().startswith("if key in settings.dict:"):
# indent = line_to_indent(line)
# while not lines.peek().strip().startswith("else:"):
# code.add(next(lines).strip(), indent=indent)
# while line_to_indent(next(lines)) == indent:
# next(lines)
elif "SUPPRESS_ERROR_POPUPS = False" in line: # set global RAISE_ERRORS
register_patch(29)
code.add(line)
indent = line_to_indent(line)
code.add("RAISE_ERRORS = True", indent=indent)
elif "ix = random.randint(0, len(lf" in line: # no more random theme selection, but exception
register_patch(30)
indent = line_to_indent(line)
code.add("raise ValueError(index + ' not a valid theme')", indent=indent)
while line_to_indent(lines.peek()) >= indent:
next(lines)
elif line.strip().startswith("warnings.warn("):
register_patch(31)
if "popup" in lines.peek().lower() and "error" in lines.peek().lower():
pass # remove line
else:
line = line.replace("warnings.warn", "raise RuntimeError")
line = line.replace(", UserWarning", "")
code.add(line)
elif "photo = tk.PhotoImage(file=element.Filename)" in line:
register_patch(32)
indent = line_to_indent(line)
code.add(
"""\
try:
import PIL
from PIL import ImageTk
from PIL import Image as PILImage
except ModuleNotFoundError:
PIL = None
if isinstance(element.Filename, (str, Path)):
if Path(element.Filename).is_file():
if PIL:
img = PILImage.open(element.Filename)
photo = ImageTk.PhotoImage(img)
else:
if Path(element.Filename).suffix.lower() in (".gif", ".png"):
photo = tk.PhotoImage(file=element.Filename)
else:
raise ValueError("file format not supported. Try installing PIL")
else:
if element.Filename == "":
photo = tk.PhotoImage(file="")
else:
raise FileNotFoundError(element.Filename)
else:
if PIL:
photo = ImageTk.PhotoImage(element.Filename)
else:
raise ValueError("PIL image format not supported. Try installing PIL")
""",
indent=indent,
)
elif "image = tk.PhotoImage(file=filename)" in line:
register_patch(33)
indent = line_to_indent(line)
code.add(
"""\
try:
import PIL
from PIL import ImageTk
from PIL import Image as PILImage
except ModuleNotFoundError:
PIL = None
if isinstance(filename, (str, Path)):
if Path(filename).is_file():
if PIL:
img = PILImage.open(filename)
image = ImageTk.PhotoImage(img)
else:
if Path(filename).suffix.lower() in (".gif", ".png"):
image = tk.PhotoImage(file=filename)
else:
raise ValueError("file format not supported. Try installing PIL")
else:
if filename == "":
image = tk.PhotoImage(file="")
else:
raise FileNotFoundError(filename)
else:
if PIL:
image = ImageTk.PhotoImage(filename)
else:
raise ValueError("PIL image format not supported. Try installing PIL")
""",
indent=indent,
)
elif "__delitem__" in line:
register_patch(35)
code.add(line)
indent = line_to_indent(line)
while line_to_indent(lines.peek()) >= indent:
code.add(next(lines))
code.add(
"""\
def __getattr__(self, item):
if item in self.dict:
return self[item]
else:
return super().__getattr(item)
def __setattr__(self, item, value):
trace_details = traceback.format_stack()
if trace_details[-1].split(",")[0] == trace_details[-2].split(",")[0]: # internal or external?
return super().__setattr__(item, value)
else:
self[item] = value
def __delattr__(self, item):
del self[item]""",
indent=indent,
)
elif line.strip() == "return self.update(*args, **kwargs)":
register_patch(38)
code.add("return self.get()", indent=line_to_indent(line))
elif line.startswith("if __name__ == "): # no more PySimpleGUI startup screen
register_patch(36)
break
else:
code.add(line)
code.add(
f"""\
class _Info:
pass
pysimplegui = _Info()
pysimplegui.version = version
pysimplegui.__version__ = __version__
del _Info
version = __version__ = mysimplegui_version = "{__version__}\"""".format(
__version__=__version__
)
)
code.add(patch_info)
to_be_registered_patches = {
0: 1,
1: 1,
2: 1,
3: 1,
4: 1,
5: 3,
6: 1,
7: 7,
8: 1,
9: 1,
10: 1,
11: 1,
12: 1,
13: 1,
14: 1,
15: 2,
16: 1,
17: 1,
18: 1,
19: 3,
20: 1,
21: 1,
22: 6,
23: 1,
24: 1,
25: 1,
26: 1,
27: 1,
29: 1,
30: 1,
31: 9,