-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdesigner_python_parser.py
More file actions
541 lines (473 loc) · 21.3 KB
/
designer_python_parser.py
File metadata and controls
541 lines (473 loc) · 21.3 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
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
# -*- coding: utf-8 -*-
"""
DesignerPythonParser: parses a Python class file (as generated by
DesignerPythonGenerator) back into a designer dict compatible with
recreateChildren().
"""
import ast
import os
import re
import textwrap
from designer_python_generator import _CLASS_SHORT_NAME
# These are the sizer classes we recognise in the generated source
_SIZER_CLASS_NAMES = frozenset(["dSizer", "dBorderSizer", "dGridSizer"])
# Pattern that matches auto-generated variable names like "label0", "button0", "sz0"
_GENERATED_NAME_RE = re.compile(
r"^(" + "|".join(re.escape(v) for v in set(_CLASS_SHORT_NAME.values())) + r")\d+$"
)
# designerClass strings for sizer dict nodes
_SIZER_DESIGNER_CLASS = {
"dSizer": "LayoutSizer",
"dBorderSizer": "LayoutBorderSizer",
"dGridSizer": "LayoutGridSizer",
}
# Orientation shorthands → full name
_ORIENTATION_FULL = {"v": "Vertical", "h": "Horizontal"}
class DesignerPythonParser:
"""Parses a Python class file back into a designer dict."""
def dict_from_python_file(self, path: str) -> dict:
with open(path, "r", encoding="utf-8") as f:
source = f.read()
return self.dict_from_python_source(source)
def dict_from_python_source(self, source: str) -> dict:
self._source_lines = source.splitlines(keepends=True)
tree = ast.parse(source)
class_node, base_name, methods = self._find_class(tree)
if class_node is None:
raise ValueError("No class definition found in source")
imported = self._collect_imports(tree)
# Separate afterInit from other methods
after_init_node = None
other_methods = []
for m in methods:
if isinstance(m, ast.FunctionDef) and m.name == "afterInit":
after_init_node = m
else:
other_methods.append(m)
# Parse afterInit to extract form attributes and children
form_attrs = {}
root_sizer_dict = None
if after_init_node is not None:
form_attrs, root_sizer_dict = self._parse_after_init(after_init_node.body, imported)
children = [root_sizer_dict] if root_sizer_dict is not None else []
# Build code dict from other methods
code = {}
for m in other_methods:
if isinstance(m, ast.FunctionDef):
code[m.name] = self._method_to_code_str(m)
# Build the form-level attributes
form_attrs["designerClass"] = "DesignerForm"
return {
"name": base_name,
"attributes": form_attrs,
"code": code,
"properties": {},
"cdata": "",
"children": children,
}
# ------------------------------------------------------------------
# AST helpers
# ------------------------------------------------------------------
def _find_class(self, tree):
"""Find the first class definition; return (node, base_name, method_nodes)."""
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
base_name = ""
if node.bases:
b = node.bases[0]
if isinstance(b, ast.Name):
base_name = b.id
elif isinstance(b, ast.Attribute):
base_name = b.attr
methods = [
n for n in node.body if isinstance(n, (ast.FunctionDef, ast.AsyncFunctionDef))
]
return node, base_name, methods
return None, "", []
def _collect_imports(self, tree) -> set:
"""Return a set of names imported from dabo."""
imported = set()
for node in ast.walk(tree):
if isinstance(node, ast.ImportFrom):
if node.module and "dabo" in node.module:
for alias in node.names:
imported.add(alias.asname or alias.name)
return imported
# ------------------------------------------------------------------
# afterInit parser
# ------------------------------------------------------------------
def _parse_after_init(self, stmts, imported):
"""
Parse the body of afterInit().
Returns:
(form_attrs: dict, root_sizer_dict: dict | None)
"""
# objects: var_name → designer_dict
# sizer_children: sizer_var → [(ctrl_var, ctrl_dict), ...]
# panel_sizer_map: ctrl_var → sizer_var (for dPanel.Sizer = dSizer(...))
objects = {}
sizer_children = {} # sizer_var → [(var, dict), ...]
panel_sizer_map = {} # ctrl_var → sizer_var
root_sizer_var = None
form_attrs = {}
for stmt in stmts:
if isinstance(stmt, ast.Assign):
info = self._handle_assign(
stmt, imported, objects, sizer_children, panel_sizer_map, form_attrs
)
if info is not None:
is_root, var = info
if is_root and root_sizer_var is None:
root_sizer_var = var
elif isinstance(stmt, ast.Expr):
self._handle_expr(stmt, objects, sizer_children)
# Build nested sizer tree
if root_sizer_var and root_sizer_var in objects:
root_sizer_dict = self._build_sizer_tree(
root_sizer_var, objects, sizer_children, panel_sizer_map
)
else:
root_sizer_dict = None
return form_attrs, root_sizer_dict
def _handle_assign(self, stmt, imported, objects, sizer_children, panel_sizer_map, form_attrs):
"""
Handle an assignment statement in afterInit.
Returns (is_root_sizer, var_name) if a sizer is created, else None.
"""
targets = stmt.targets
value = stmt.value
# Parse all LHS targets. Recognised forms:
# Name("sz0") → ("local", "sz0")
# Attribute(Name("self"), "Sizer") → ("self", "Sizer") — root sizer
# Attribute(Name("self"), X) → ("self", X) — instance attr
# Attribute(Attribute(self, P), "Sizer") → ("panel_sizer", P) — self.P.Sizer
# Attribute(Name("panel0"), "Sizer") → ("panel_sizer", "panel0") — local.Sizer
parsed_targets = []
for t in targets:
if isinstance(t, ast.Name):
parsed_targets.append(("local", t.id))
elif isinstance(t, ast.Attribute):
inner = t.value
if isinstance(inner, ast.Name) and inner.id == "self":
parsed_targets.append(("self", t.attr))
elif (
isinstance(inner, ast.Attribute)
and isinstance(inner.value, ast.Name)
and inner.value.id == "self"
and t.attr == "Sizer"
):
# self.X.Sizer = dSizer(...)
parsed_targets.append(("panel_sizer", inner.attr))
elif isinstance(inner, ast.Name) and t.attr == "Sizer":
# local_var.Sizer = dSizer(...) (new generator style)
parsed_targets.append(("panel_sizer", inner.id))
if not parsed_targets:
return None
# -------- RHS is a constructor call --------
if isinstance(value, ast.Call):
cls_name = self._get_call_name(value)
# --- Sizer ---
if cls_name in _SIZER_CLASS_NAMES:
dct = self._build_sizer_dict(cls_name, value)
# Primary local var
var = None
is_root = False
panel_parent = None
for kind, name in parsed_targets:
if kind == "local":
if var is None:
var = name
elif kind == "self" and name == "Sizer":
is_root = True
elif kind == "panel_sizer":
panel_parent = name
# If only assigned as panel.Sizer, generate a var name
if var is None:
var = f"_panel_sz_{panel_parent}" if panel_parent else "_sz_root"
if panel_parent and not is_root:
is_root = False
objects[var] = dct
sizer_children[var] = []
if panel_parent:
panel_sizer_map[panel_parent] = var
if is_root:
return (True, var)
return (False, var)
# --- Control (any imported class) ---
elif cls_name and cls_name in imported:
dct = self._build_control_dict(cls_name, value)
var = None
reg_id = None
for kind, name in parsed_targets:
if kind == "local":
if var is None:
var = name
elif kind == "self":
# self.X = ctrl(...).
# Only treat X as a user RegID if it doesn't look like a
# generator-produced name (e.g. "button0", "panel0").
if not name.startswith("_") and not _GENERATED_NAME_RE.match(name):
reg_id = name
if var is None:
var = name # Use as key regardless
if reg_id:
dct["attributes"]["RegID"] = reg_id
if var is None:
var = f"_ctrl_{id(stmt)}"
objects[var] = dct
return None
# -------- self.Property = literal (form attribute) --------
for kind, name in parsed_targets:
if kind == "self" and name != "Sizer":
val_str = self._literal_to_str(value)
if val_str is not None:
form_attrs[name] = val_str
return None
return None
def _handle_expr(self, stmt, objects, sizer_children):
"""Handle sizer/control method calls in afterInit."""
if not isinstance(stmt.value, ast.Call):
return
call = stmt.value
func = call.func
if not isinstance(func, ast.Attribute):
return
method_name = func.attr
receiver_var = self._get_name(func.value)
if receiver_var is None:
return
# ---- Sizer methods ----
if receiver_var in sizer_children:
if method_name == "appendSpacer":
size = "10"
if call.args:
size = self._literal_to_str(call.args[0]) or "10"
spacer_atts = {
"designerClass": "LayoutSpacerPanel",
"Spacing": size,
"sizerInfo": "{}",
}
# Check for row/col kwargs (grid sizer spacer)
row_val = None
col_val = None
for kw in call.keywords:
if kw.arg == "row":
row_val = self._literal_to_str(kw.value)
elif kw.arg == "col":
col_val = self._literal_to_str(kw.value)
if row_val is not None and col_val is not None:
spacer_atts["rowColPos"] = f"({row_val}, {col_val})"
spacer_dct = {
"name": "dPanel",
"attributes": spacer_atts,
"code": {},
"properties": {},
"cdata": "",
"children": [],
}
sizer_children[receiver_var].append(("_spacer", spacer_dct))
elif method_name in ("append", "append1x"):
if not call.args:
return
ctrl_var = self._get_name(call.args[0])
if ctrl_var is None or ctrl_var not in objects:
return
ctrl_dct = objects[ctrl_var]
# Separate row/col kwargs from sizerInfo kwargs
row_val = None
col_val = None
si = {}
if method_name == "append1x":
si["Proportion"] = 1
si["Expand"] = True
else:
for kw in call.keywords:
k = kw.arg
v = self._literal_to_str(kw.value)
if k == "row":
row_val = v
elif k == "col":
col_val = v
elif k == "border":
si["Border"] = int(v) if v is not None else 0
elif k == "proportion":
si["Proportion"] = int(v) if v is not None else 0
elif k == "expand":
si["Expand"] = v == "True"
elif k == "halign":
val = v.strip("'\"") if v else "left"
si["HAlign"] = val.capitalize()
elif k == "valign":
val = v.strip("'\"") if v else "top"
si["VAlign"] = val.capitalize()
elif k == "borderSides":
try:
si["BorderSides"] = ast.literal_eval(v)
except Exception:
pass
si_str = repr(si) if si else "{}"
ctrl_dct["attributes"]["sizerInfo"] = si_str
# Set rowColPos: explicit from kwargs, or auto-assign for grid sizers
if row_val is not None and col_val is not None:
ctrl_dct["attributes"]["rowColPos"] = f"({row_val}, {col_val})"
else:
sizer_dct = objects.get(receiver_var, {})
if sizer_dct.get("attributes", {}).get("designerClass") == "LayoutGridSizer":
num_cols = int(sizer_dct["attributes"].get("Columns", "1") or "1")
idx = len(sizer_children[receiver_var])
r = idx // num_cols
c = idx % num_cols
ctrl_dct["attributes"]["rowColPos"] = f"({r}, {c})"
sizer_children[receiver_var].append((ctrl_var, ctrl_dct))
# ---- Grid methods ----
elif receiver_var in objects:
parent_dct = objects[receiver_var]
if method_name == "addColumn" and parent_dct.get("name") == "dGrid":
# grid_var.addColumn(dColumn(grid_var, Caption=..., ...))
if call.args and isinstance(call.args[0], ast.Call):
col_call = call.args[0]
col_name = self._get_call_name(col_call)
if col_name == "dColumn":
col_dct = self._build_control_dict("dColumn", col_call)
if not parent_dct.get("children"):
parent_dct["children"] = []
parent_dct["children"].append(col_dct)
# ------------------------------------------------------------------
# Sizer tree builder
# ------------------------------------------------------------------
def _build_sizer_tree(self, sizer_var, objects, sizer_children, panel_sizer_map):
"""Recursively build a nested sizer dict."""
dct = objects[sizer_var]
kids_with_vars = sizer_children.get(sizer_var, [])
built_kids = []
for kid_var, kid_dct in kids_with_vars:
dc = kid_dct.get("attributes", {}).get("designerClass", "")
if dc in ("LayoutSizer", "LayoutBorderSizer", "LayoutGridSizer"):
# Recurse into nested sizer
if kid_var in sizer_children:
kid_dct = self._build_sizer_tree(
kid_var, objects, sizer_children, panel_sizer_map
)
elif dc == "controlMix":
# Check if this control has a nested panel sizer
if kid_var in panel_sizer_map:
nested_sizer_var = panel_sizer_map[kid_var]
if nested_sizer_var in objects:
nested_sizer = self._build_sizer_tree(
nested_sizer_var, objects, sizer_children, panel_sizer_map
)
kid_dct["children"] = [nested_sizer]
built_kids.append(kid_dct)
dct["children"] = built_kids
dct["attributes"]["SlotCount"] = str(len(built_kids))
return dct
# ------------------------------------------------------------------
# Dict builders
# ------------------------------------------------------------------
def _build_sizer_dict(self, cls_name: str, call: ast.Call) -> dict:
"""Build a designer dict for a sizer from its constructor call."""
atts = {}
dc = _SIZER_DESIGNER_CLASS.get(cls_name, "LayoutSizer")
atts["designerClass"] = dc
if cls_name == "dSizer":
orient_short = self._ast_str_value(call.args[0]) if call.args else "v"
atts["Orientation"] = _ORIENTATION_FULL.get(orient_short, "Vertical")
elif cls_name == "dBorderSizer":
orient_short = self._ast_str_value(call.args[0]) if call.args else "v"
atts["Orientation"] = _ORIENTATION_FULL.get(orient_short, "Vertical")
for kw in call.keywords:
if kw.arg == "Caption":
atts["Caption"] = self._ast_str_value(kw.value) or ""
elif cls_name == "dGridSizer":
if len(call.args) >= 2:
atts["Rows"] = self._literal_to_str(call.args[0]) or "0"
atts["Columns"] = self._literal_to_str(call.args[1]) or "0"
for kw in call.keywords:
if kw.arg == "HGap":
atts["HGap"] = self._literal_to_str(kw.value) or "0"
elif kw.arg == "VGap":
atts["VGap"] = self._literal_to_str(kw.value) or "0"
return {
"name": cls_name,
"attributes": atts,
"code": {},
"properties": {},
"cdata": "",
"children": [],
}
def _build_control_dict(self, cls_name: str, call: ast.Call) -> dict:
"""Build a designer dict for a control from its constructor call."""
atts = {"designerClass": "controlMix"}
for kw in call.keywords:
if kw.arg is None:
continue # **kwargs expansion
val_str = self._literal_to_str(kw.value)
if val_str is not None:
atts[kw.arg] = val_str
return {
"name": cls_name,
"attributes": atts,
"code": {},
"properties": {},
"cdata": "",
"children": [],
}
# ------------------------------------------------------------------
# Value helpers
# ------------------------------------------------------------------
def _literal_to_str(self, node) -> str | None:
"""Convert an AST node representing a Python literal to its string repr."""
if isinstance(node, ast.Constant):
v = node.value
if isinstance(v, bool):
return "True" if v else "False"
elif isinstance(v, (int, float)):
return str(v)
elif isinstance(v, str):
return v
else:
return repr(v)
elif isinstance(node, (ast.List, ast.Tuple, ast.Dict)):
try:
val = ast.literal_eval(node)
return repr(val)
except Exception:
return None
elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
inner = self._literal_to_str(node.operand)
return f"-{inner}" if inner is not None else None
return None
def _ast_str_value(self, node) -> str:
"""Extract a string value from a Constant node."""
if isinstance(node, ast.Constant) and isinstance(node.value, str):
return node.value
return ""
def _get_call_name(self, call: ast.Call) -> str | None:
"""Get the function name from a Call node."""
func = call.func
if isinstance(func, ast.Name):
return func.id
elif isinstance(func, ast.Attribute):
return func.attr
return None
def _get_name(self, node) -> str | None:
"""
Get a variable name from a Name or Attribute(Name("self"), X) node.
For `self.X`, returns just `X` (since that's how we key objects).
"""
if isinstance(node, ast.Name):
return node.id
elif isinstance(node, ast.Attribute):
if isinstance(node.value, ast.Name) and node.value.id == "self":
return node.attr
return None
# ------------------------------------------------------------------
# Method source extraction
# ------------------------------------------------------------------
def _method_to_code_str(self, func_def) -> str:
"""Extract and dedent source lines for a function definition."""
start = func_def.lineno - 1
end = func_def.end_lineno
lines = self._source_lines[start:end]
src = "".join(lines)
return textwrap.dedent(src).rstrip()