forked from wxGlade/wxGlade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
180 lines (139 loc) · 5.38 KB
/
utilities.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
"""
Utilities, e.g. for debugging
@copyright: 2018-2020 Dietmar Schwertberger
@license: MIT (see LICENSE.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""
from __future__ import print_function
import sys, os
import wx
def hx(obj):
return hex(id(obj)).upper()
class StructurePrinter:
# print the structure and sizes of a wx window with all it's children
# this is not the wxGlade data structure
def __init__(self, window):
self.window(window, 0)
def _sizer_item(self, si, indent):
expand = (si.GetFlag() & wx.EXPAND) and "EXPAND" or ""
print( " "*indent, " SI:", si.GetMinSize(), " ", si.GetSize(), " ", si.GetProportion(), expand )
def sizer(self, sizer, si, indent=0):
cname = sizer.GetClassName() # u'wxPanel'
print( " "*indent, "%s"%cname, sizer.GetSize(), sizer )
if si: self._sizer_item(si, indent)
print()
# list all by sizer
for si in sizer.GetChildren():
if si is None:
print("Child is None")
continue
child = si.GetWindow()
if child is not None:
self.window(child, si, indent+1)
continue
child = si.GetSizer()
if child is not None:
self.sizer(child, si, indent+1)
continue
child = si.GetSpacer()
if child is not None:
self.spacer(child, si, indent+1)
continue
raise ValueError("XXX")
def window(self, widget, si, indent=0):
cname = widget.GetClassName() # u'wxPanel'
name = widget.GetName() # u'panel'
HEX = hx(widget)
try:
best_size = widget.GetBestSize()
except AttributeError:
best_size ="???"
print( " "*indent, "%s: %s %s"%(cname, name, HEX), widget.GetSize(), best_size, widget.GetEffectiveMinSize() )
if si: self._sizer_item(si, indent)
sizer = widget.GetSizer()
if sizer:
# list all by sizer
self.sizer(sizer, None, indent+1)
else:
# list all children
for child in widget.GetChildren():
self.window(child, None, indent+1)
print()
def spacer(self, spacer, si, indent=0):
# a spacer
print( " "*indent, "Spacer", spacer.Get() )
self._sizer_item(si, indent)
print()
class TreePrinter:
# print the structure of the TreeCtrl
def __init__(self, root):
print("=============================================================================")
self.prn(root, 0)
print("=============================================================================")
def prn(self, editor, indent=0):
all_children = editor.get_all_children()
if not all_children: return
for child in all_children:
klass = "class" in child.PROPERTIES and child.klass or None
print( " "*indent, child.WX_CLASS, klass, getattr(child, "custom_class", "---"), child.IS_TOPLEVEL)
self.prn(child, indent+1)
print()
def trace1(func, *args, **kwargs):
# use built-in trace module to write an execution trace to stdout
# e.g. utilities.trace1(clipboard.paste, focused_widget)
import sys, trace
tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix],
ignoremods=["gui_mixins", "new_properties", "decorators", "__init__", "clipboard"],
trace=1, count=0)
editor = common.root.children[-1]
tracer.runfunc(editor.create_widgets)
# make a report, placing output in the current directory
r = tracer.results()
if filename:
r.write_results_file(path, lines, lnotab, lines_hit, encoding=None)
else:
r.write_results(show_missing=True, coverdir=".")
# own implementation for wxGlade
TRACING = False
def trace(filename, func, *args, **kwargs):
start_trace(filename)
func(*args, **kwargs)
stop_trace()
def start_trace():
global TRACING
if TRACING: return
TRACING = True
sys.settrace(trace_calls)
def stop_trace():
sys.settrace(None)
global TRACING
TRACING = False
_FILES = {}
def _get_line(filename, line_no):
if not filename in _FILES:
_FILES[filename] = open(filename, "r").readlines()
return _FILES[filename][line_no-1]
def trace_lines(frame, event, arg):
if event != 'line': return
co = frame.f_code
func_name = co.co_name
line_no = frame.f_lineno
filename = co.co_filename
if os.path.isfile(filename):
print( ' %30s: %5d' % (func_name, line_no), _get_line(filename, line_no), end="" )
else:
print( ' %30s: %5d' % (func_name, line_no) )
def trace_calls(frame, event, arg):
if event != 'call': return
co = frame.f_code
func_name = co.co_name
if func_name in ("__getattr__", "<genexpr>", "<listcomp>"): return
if func_name in ("cn",): return
#if func_name.startswith("_",): return
if func_name in ("wxname2attr",): return
filename = co.co_filename
if filename.startswith(sys.prefix) or filename.startswith(sys.exec_prefix): return
modulename = filename.split(os.sep)[-1].split(".")[0]
if modulename in ("new_properties", "gui_mixins", "decorators", "log", "main", "misc", "compat",): return
line_no = frame.f_lineno
print( ' -> %s:%s %s' % (modulename, line_no, func_name))
return trace_lines