-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDEBUG.py
More file actions
250 lines (209 loc) · 8.79 KB
/
DEBUG.py
File metadata and controls
250 lines (209 loc) · 8.79 KB
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
# -*- coding: utf-8 -*-
import copy
import logging
from time import time
import wx
continue_sign: bool = True
def times(func, *arge):
a = time()
b = func()
print(*arge, time() - a)
return b
def times2(flag=None, *args):
if flag is None:
time1 = time()
return time1
else:
print(time() - flag, *args)
class MyTreeFrame(wx.Frame):
def __init__(self, parent, title, object_thing):
super(MyTreeFrame, self).__init__(parent, title=title, size=(600, 400))
self.__object = object_thing
# 创建一个垂直的 BoxSizer
sizer = wx.BoxSizer(wx.VERTICAL)
self.__button = wx.Button(self, label="Update Tree", id=1)
self.Bind(wx.EVT_BUTTON, self.update_tree3, self.__button)
# 创建树控件
self.__tree = wx.TreeCtrl(self, style=wx.TR_DEFAULT_STYLE | wx.TR_HIDE_ROOT)
self.__root = self.__tree.AddRoot("Root")
# 将树控件添加到 sizer
sizer.Add(self.__tree, 1, wx.EXPAND)
# 将按钮添加到 sizer
sizer.Add(self.__button, 0, wx.ALL | wx.CENTER, 5)
# 设置窗口的 sizer
self.SetSizer(sizer)
# 展开根节点
self.__ID: list[wx.TreeItemId] = [self.__root]
self.__dict: dict[int, list[int]] = {0: []}
self.__data_name: list[[str, str]] = [None]
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.update_tree3, self.timer)
self.timer.Start(1000)
@property
def object(self):
return self.__object
@object.setter
def object(self, value):
self.__object = value
self.__update_tree()
def update_tree3(self, a):
a.GetId()
# times(self.__update_tree, 'update_time:')
self.__update_tree()
def __update_tree(self):
global add_instance
add_instance = set()
value, new_dict = get_instance_attributes_and_methods(self.__object, 0)
self.__update_tree2(0, self.__object, value, new_dict)
def __update_tree2(self, parent, self_name, self_value, new_dict):
if parent != 0:
a, b = self.__data_name[parent]
self_value = str(self_value)
self_name = str(self_name) + ':' + self_value
if a != self_name:
self.__tree.SetItemText(self.__ID[parent], self_name)
self.__data_name[parent][0] = self_name
if self_value != b:
self.__tree.SetItemData(self.__ID[parent], self_value)
self.__data_name[parent][1] = self_value
parent_id = self.__ID[parent]
child = self.__dict[parent]
for key, value in new_dict.items():
key = str(key)
for i in child:
if self.__data_name[i][0] == f'{key}:{value[0]}':
if value[1] != 'function':
self.__update_tree2(i, key, value[0], value[1])
break
else:
child_id = self.__tree.AppendItem(parent_id, f'{key}:{value[0]}')
ids = len(self.__ID)
self.__data_name.append([f'{key}:{value[0]}', value[0]])
self.__ID.append(child_id)
self.__dict[parent].append(ids)
self.__dict[ids] = []
if value[1] != 'function':
self.__update_tree2(ids, key, value[0], value[1])
delet_id = []
for i in child:
if self.__data_name[i][0] not in (f'{key}:{value[0]}' for key, value in new_dict.items()):
self.__tree.Delete(self.__ID[i])
delet_id.append(i)
for i in delet_id:
self.__dict[parent].remove(i)
add_instance: set = set()
def get_instance_attributes_and_methods(instance, death):
if isinstance(instance, (dict, list, set)):
attributes = copy.copy(instance)
else:
attributes = instance
child_tree = {}
if death > 3:
return attributes, child_tree
if id(instance) not in add_instance:
if isinstance(instance, dict):
for key, value in instance.items():
child_tree[key] = get_instance_attributes_and_methods(value, death + 1)
elif isinstance(instance, list) or isinstance(instance, tuple) or isinstance(instance, set):
for i, item in enumerate(instance):
child_tree[i] = get_instance_attributes_and_methods(item, death + 1)
else:
if hasattr(instance, '__dict__'):
# 获取所有属性
for attr_name, attr_value in vars(instance).items():
if not callable(attr_value):
child_tree[attr_name] = get_instance_attributes_and_methods(attr_value, death + 1)
add_instance.add(id(instance))
if not (isinstance(instance, str) or isinstance(instance, int) or isinstance(instance, float) or isinstance(
instance, bool) or isinstance(instance, list) or isinstance(instance, tuple) or isinstance(instance, set)
or isinstance(instance, dict)):
# 获取所有方法
for method_name in dir(instance):
try:
method_value = getattr(instance, method_name)
except AttributeError:
method_value = None
if callable(method_value) and not method_name.startswith('__'):
child_tree[method_name] = (method_value, 'function')
return attributes, child_tree
class WxLogHandler(logging.Handler):
def __init__(self, text_ctrl, max_lines=1000, page_size=100):
super().__init__()
self.text_ctrl = text_ctrl
self.max_lines = max_lines
self.page_size = page_size
self.log_buffer = []
self.current_page = 0
def emit(self, record):
log_entry = self.format(record)
self.log_buffer.append(log_entry)
if len(self.log_buffer) > self.max_lines:
self.log_buffer.pop(0)
self.update_display()
def update_display(self):
start_index = self.current_page * self.page_size
end_index = min(start_index + self.page_size, len(self.log_buffer))
self.text_ctrl.SetValue('\n'.join(self.log_buffer[start_index:end_index]))
def next_page(self):
if self.current_page < (len(self.log_buffer) // self.page_size):
self.current_page += 1
self.update_display()
def prev_page(self):
if self.current_page > 0:
self.current_page -= 1
self.update_display()
def first_page(self):
self.current_page = 0
self.update_display()
def last_page(self):
self.current_page = len(self.log_buffer) // self.page_size
self.update_display()
def continue_func():
global continue_sign
continue_sign = True
def stop_func():
global continue_sign
continue_sign = False
def setup_wx_logger():
app = wx.App(False)
frame = wx.Frame(None, title="Game Log", size=(800, 600))
panel = wx.Panel(frame)
# 创建一个文本控件
log_text = wx.TextCtrl(panel, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL | wx.VSCROLL)
log_text.SetFont(wx.Font(10, wx.FONTFAMILY_MODERN, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
# 创建分页按钮
prev_button = wx.Button(panel, label="Previous")
next_button = wx.Button(panel, label="Next")
first_button = wx.Button(panel, label="First")
last_button = wx.Button(panel, label="Last")
continue_button = wx.Button(panel, label="Continue")
stop_button = wx.Button(panel, label="Stop")
# 设置布局
sizer = wx.BoxSizer(wx.VERTICAL)
button_sizer = wx.BoxSizer(wx.HORIZONTAL)
button_sizer.Add(first_button, 0, wx.ALL, 5)
button_sizer.Add(prev_button, 0, wx.ALL, 5)
button_sizer.Add(next_button, 0, wx.ALL, 5)
button_sizer.Add(last_button, 0, wx.ALL, 5)
button_sizer.Add(continue_button, 0, wx.ALL, 5)
button_sizer.Add(stop_button, 0, wx.ALL, 5)
sizer.Add(log_text, 1, wx.EXPAND | wx.ALL, 5)
sizer.Add(button_sizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
panel.SetSizer(sizer)
wx_handler = WxLogHandler(log_text, 5000, 37)
wx_handler.setLevel(logging.INFO)
wx_formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
wx_handler.setFormatter(wx_formatter)
# 绑定按钮事件
prev_button.Bind(wx.EVT_BUTTON, lambda event: wx_handler.prev_page())
next_button.Bind(wx.EVT_BUTTON, lambda event: wx_handler.next_page())
first_button.Bind(wx.EVT_BUTTON, lambda event: wx_handler.first_page())
last_button.Bind(wx.EVT_BUTTON, lambda event: wx_handler.last_page())
continue_button.Bind(wx.EVT_BUTTON, lambda event: continue_func())
stop_button.Bind(wx.EVT_BUTTON, lambda event: stop_func())
frame.Show()
frame2 = MyTreeFrame(None, '类debug', object())
frame2.Show()
return app, wx_handler, frame, frame2
def start_wx_mainloop(app):
app.MainLoop()