Repository navigation
Expand file tree
/
Copy pathdesigner_python_generator.py
More file actions
767 lines (662 loc) · 28 KB
/
Copy pathdesigner_python_generator.py
File metadata and controls
767 lines (662 loc) · 28 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
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
# -*- coding: utf-8 -*-
"""
DesignerPythonGenerator: converts a designer dict (as produced by
getDesignerDict()) into a Python class source file.
"""
import ast
import json
import os
from dabo.lib.DesignerUtils import getDefaultSizerProps
# Attributes on the form root that are excluded from the generated output
# (either handled specially or irrelevant at class-definition time).
_FORM_ATTRS_EXCLUDE = frozenset(
[
"designerClass",
"Name",
"Left",
"Top",
"code-ID",
"classID",
"SaveRestorePosition",
]
)
# Attributes on controls that the generator never emits
_CTRL_ATTRS_EXCLUDE = frozenset(
[
"designerClass",
"sizerInfo",
"code-ID",
"classID",
"rowColPos",
]
)
# Attribute names whose values should be treated as integers
_INT_ATTRS = frozenset(
[
"Height",
"Width",
"Left",
"Top",
"FontSize",
"Spacing",
"SlotCount",
"Rows",
"Columns",
"HGap",
"VGap",
"Order",
"ColumnCount",
]
)
# Sizer class name mapping
_SIZER_CLASS = {
"LayoutSizer": "dSizer",
"LayoutBorderSizer": "dBorderSizer",
"LayoutGridSizer": "dGridSizer",
}
# Dialogs that add their standard buttons after the user's controls. Their
# content must be built in addControls() and appended to the dialog's own
# Sizer; replacing the Sizer in afterInit() would misplace the buttons.
_STD_BUTTON_DIALOGS = frozenset(["dOkCancelDialog", "dYesNoDialog", "dStandardButtonDialog"])
# Orientation shorthands
_ORIENTATION_SHORT = {"Vertical": "v", "Horizontal": "h"}
# Short variable name prefix per dabo class
_CLASS_SHORT_NAME = {
"dLabel": "label",
"dButton": "button",
"dBitmapButton": "bmp_btn",
"dTextBox": "textbox",
"dPasswordTextBox": "pwd_textbox",
"dDateTextBox": "date_textbox",
"dRichTextBox": "rich_text",
"dMemo": "memo",
"dSpinner": "spinner",
"dSlider": "slider",
"dCheckBox": "checkbox",
"dComboBox": "combobox",
"dDropdownList": "dropdown",
"dRadioList": "radio_list",
"dListBox": "list_box",
"dPanel": "panel",
"dScrollPanel": "scroll_panel",
"dBox": "box",
"dLine": "line",
"dImage": "image",
"dBitmap": "bitmap",
"dHtmlBox": "html",
"dGrid": "grid",
"dColumn": "col",
"dPageFrame": "page_frame",
"dPageList": "page_list",
"dPageSelect": "page_select",
"dPageStyled": "page_styled",
"dPageFrameNoTabs": "page_frame",
"dSlidePanelControl": "slide_panel",
"dSplitter": "splitter",
"dTreeView": "tree",
"dToggleButton": "toggle",
"dTimer": "timer",
"dMediaControl": "media",
# Sizers
"dSizer": "sz",
"dBorderSizer": "border_sz",
"dGridSizer": "grid_sz",
}
def _dq(s: str) -> str:
"""Return a double-quoted Python string literal for *s* (Black/Ruff style)."""
return json.dumps(s)
class DesignerPythonGenerator:
"""Converts a designer dict to Python source."""
def generate(self, dct: dict, file_stem: str = "") -> str:
"""
Top-level entry point. *dct* is what getDesignerDict() returns.
Returns a string of Python source code.
"""
self._imports = set()
self._counters = {} # short_name → next integer
self._methods = [] # (method_name, method_source) from child controls
self._last_var = None # last control var generated (for grid sizer appends)
# Orientation ("V", "H", or "G" for grid) of each enclosing sizer; the
# designer only saves sizer settings that differ from per-class
# defaults that depend on this.
self._sizer_types = []
# (sizer_var, "row"/"col", index) already set to expand in a grid sizer
self._grid_expanded = set()
atts = dct.get("attributes", {})
base_class = dct.get("name", "dForm")
dc = atts.get("designerClass", "")
if dc != "DesignerForm":
raise ValueError(
f"generate() only supports form-mode dicts "
f"(designerClass='DesignerForm'), got {dc!r}"
)
self._collect_imports(dct)
class_name = self._make_class_name(base_class, file_stem)
self._class_name = class_name
is_std_dialog = base_class in _STD_BUTTON_DIALOGS
self._build_method = "addControls" if is_std_dialog else "afterInit"
after_init_lines = self._generate_after_init(dct, is_std_dialog)
code_dict = dct.get("code", {})
method_lines = self._generate_methods(code_dict)
import_block = self._build_import_block(base_class, code_dict)
lines = ["# -*- coding: utf-8 -*-"]
lines.append(import_block)
lines.append("")
lines.append("")
lines.append(f"class {class_name}({base_class}):")
if after_init_lines:
lines.append(f" def {self._build_method}(self):")
for ln in after_init_lines:
lines.append(" " + ln if ln.strip() else "")
lines.append("")
if method_lines:
lines.extend(method_lines)
# Runnable entry point
lines.append("")
lines.append("")
lines.append('if __name__ == "__main__":')
lines.append(" from dabo.application import dApp")
lines.append(f" app = dApp(MainFormClass={class_name})")
lines.append(" app.start()")
lines.append("")
return "\n".join(lines)
# ------------------------------------------------------------------
# Import helpers
# ------------------------------------------------------------------
def _collect_imports(self, dct: dict):
"""Walk the dict tree and collect all dabo class names needed."""
atts = dct.get("attributes", {})
dc = atts.get("designerClass", "")
name = dct.get("name", "")
if dc == "DesignerForm":
self._imports.add(name)
elif dc == "controlMix":
self._imports.add(name)
elif dc in _SIZER_CLASS:
self._imports.add(_SIZER_CLASS[dc])
for child in dct.get("children", []):
self._collect_imports(child)
def _build_import_block(self, base_class: str, code_dict: dict) -> str:
"""Return the import block as a string."""
extra_imports = code_dict.get("importStatements", "").strip()
sorted_imports = sorted(self._imports)
lines = [f"from dabo.ui import {cls}" for cls in sorted_imports]
if extra_imports and "events" in extra_imports:
lines.append("from dabo import events")
if extra_imports:
return extra_imports + "\n" + "\n".join(lines)
return "\n".join(lines)
# ------------------------------------------------------------------
# Class name
# ------------------------------------------------------------------
def _make_class_name(self, base_class: str, file_stem: str) -> str:
"""Derive a CamelCase class name from the file stem, or fall back to base class."""
if file_stem:
parts = file_stem.replace("-", "_").split("_")
return "".join(p.title() for p in parts if p)
return base_class
# ------------------------------------------------------------------
# afterInit generation
# ------------------------------------------------------------------
def _generate_after_init(self, dct: dict, is_std_dialog: bool = False) -> list:
"""Return lines for the afterInit (or addControls) body (indented 4 spaces)."""
lines = []
atts = dct.get("attributes", {})
# 1. Form-level property assignments
for key, val in atts.items():
if key in _FORM_ATTRS_EXCLUDE:
continue
py_val = self._python_value(val, key)
if isinstance(py_val, str):
lines.append(f" self.{key} = {_dq(py_val)}")
else:
lines.append(f" self.{key} = {py_val}")
if lines:
lines.append("")
# 2. Sizer / children
children = dct.get("children", [])
if children:
root_child = children[0]
child_lines = self._generate_object_code(
root_child,
parent_var="self",
sizer_var="self.Sizer" if is_std_dialog else None,
indent=1,
is_root_sizer=not is_std_dialog,
)
lines.extend(child_lines)
return lines
# ------------------------------------------------------------------
# Recursive code generation
# ------------------------------------------------------------------
def _generate_object_code(
self,
dct: dict,
parent_var: str,
sizer_var: str | None,
indent: int,
is_root_sizer: bool = False,
) -> list:
"""Route to the appropriate generator based on designerClass."""
atts = dct.get("attributes", {})
dc = atts.get("designerClass", "")
name = dct.get("name", "")
pad = " " * indent
if dc == "LayoutPanel":
return []
elif dc == "LayoutSpacerPanel":
return self._generate_spacer(dct, sizer_var, pad)
elif name == "Spacer":
return self._generate_spacer(dct, sizer_var, pad)
elif dc == "LayoutSizer":
return self._generate_layout_sizer(dct, parent_var, sizer_var, indent, is_root_sizer)
elif dc == "LayoutBorderSizer":
return self._generate_border_sizer(dct, parent_var, sizer_var, indent, is_root_sizer)
elif dc == "LayoutGridSizer":
return self._generate_grid_sizer(dct, parent_var, sizer_var, indent, is_root_sizer)
elif dc == "controlMix":
return self._generate_control(dct, parent_var, sizer_var, indent)
else:
return []
def _generate_layout_sizer(self, dct, parent_var, sizer_var, indent, is_root) -> list:
atts = dct.get("attributes", {})
orientation = atts.get("Orientation", "Vertical")
orient_short = _ORIENTATION_SHORT.get(orientation, "v")
pad = " " * indent
lines = []
sz_var = self._next_var("dSizer")
if is_root:
lines.append(f"{pad}{sz_var} = {parent_var}.Sizer = dSizer({_dq(orient_short)})")
else:
lines.append(f"{pad}{sz_var} = dSizer({_dq(orient_short)})")
self._sizer_types.append(orient_short.upper())
for child in dct.get("children", []):
lines.extend(self._generate_object_code(child, parent_var, sz_var, indent))
self._sizer_types.pop()
if not is_root and sizer_var:
si_args = self._nested_sizer_append_args(atts, sizer_var)
lines.append(f"{pad}{sizer_var}.append({sz_var}{si_args})")
lines.append("")
return lines
def _generate_border_sizer(self, dct, parent_var, sizer_var, indent, is_root) -> list:
atts = dct.get("attributes", {})
caption = atts.get("Caption", "")
orientation = atts.get("Orientation", "Vertical")
orient_short = _ORIENTATION_SHORT.get(orientation, "v")
pad = " " * indent
lines = []
sz_var = self._next_var("dBorderSizer")
# dBorderSizer draws a dBox on its parent window, so the parent comes first.
args = f"{parent_var}, {_dq(orient_short)}"
if caption:
args += f", Caption={_dq(caption)}"
if is_root:
lines.append(f"{pad}{sz_var} = {parent_var}.Sizer = dBorderSizer({args})")
else:
lines.append(f"{pad}{sz_var} = dBorderSizer({args})")
self._sizer_types.append(orient_short.upper())
for child in dct.get("children", []):
lines.extend(self._generate_object_code(child, parent_var, sz_var, indent))
self._sizer_types.pop()
if not is_root and sizer_var:
si_args = self._nested_sizer_append_args(atts, sizer_var)
lines.append(f"{pad}{sizer_var}.append({sz_var}{si_args})")
lines.append("")
return lines
def _generate_grid_sizer(self, dct, parent_var, sizer_var, indent, is_root) -> list:
atts = dct.get("attributes", {})
rows = atts.get("Rows", "0")
cols = atts.get("Columns", "0")
hgap = atts.get("HGap", "0")
vgap = atts.get("VGap", "0")
pad = " " * indent
lines = []
sz_var = self._next_var("dGridSizer")
# dGridSizer only accepts keyword properties; MaxCols takes precedence,
# matching the runtime's own default of filling by columns.
if cols not in ("0", ""):
args = f"MaxCols={cols}"
elif rows not in ("0", ""):
args = f"MaxRows={rows}"
else:
args = ""
if hgap != "0" or vgap != "0":
args += f"{', ' if args else ''}HGap={hgap}, VGap={vgap}"
if is_root:
lines.append(f"{pad}{sz_var} = {parent_var}.Sizer = dGridSizer({args})")
else:
lines.append(f"{pad}{sz_var} = dGridSizer({args})")
self._sizer_types.append("G")
for child in dct.get("children", []):
child_atts = child.get("attributes", {})
child_dc = child_atts.get("designerClass", "")
row_col_str = child_atts.get("rowColPos")
if child_dc == "LayoutPanel":
# Empty grid slot — nothing to emit
continue
if row_col_str is not None and child_dc == "controlMix":
# Control placed at a specific grid position
try:
if isinstance(row_col_str, (tuple, list)):
r, c = row_col_str
else:
r, c = eval(row_col_str)
except Exception:
r, c = 0, 0
self._last_var = None
ctrl_lines = self._generate_object_code(
child, parent_var, sizer_var=None, indent=indent
)
# Strip trailing blank so the grid append follows immediately
while ctrl_lines and not ctrl_lines[-1].strip():
ctrl_lines.pop()
lines.extend(ctrl_lines)
if self._last_var is not None:
si = self._control_sizer_info(child.get("name", ""), child_atts)
si_args = self._sizer_info_to_append_args(si)
lines.append(
f"{pad}{sz_var}.append({self._last_var}{si_args}, row={r}, col={c})"
)
for flag, kind, idx in (("RowExpand", "row", r), ("ColExpand", "col", c)):
key = (sz_var, kind, idx)
if si.get(flag) and key not in self._grid_expanded:
self._grid_expanded.add(key)
method = "setRowExpand" if kind == "row" else "setColExpand"
lines.append(f"{pad}{sz_var}.{method}(True, {idx})")
lines.append("")
else:
lines.extend(self._generate_object_code(child, parent_var, sz_var, indent))
self._sizer_types.pop()
if not is_root and sizer_var:
si_args = self._nested_sizer_append_args(atts, sizer_var)
lines.append(f"{pad}{sizer_var}.append({sz_var}{si_args})")
lines.append("")
return lines
def _generate_control(
self,
dct: dict,
parent_var: str,
sizer_var: str | None,
indent: int,
) -> list:
atts = dict(dct.get("attributes", {}))
name = dct.get("name", "dPanel")
code_dict = dct.get("code", {})
children = dct.get("children", [])
pad = " " * indent
lines = []
reg_id = atts.get("RegID", "")
# Guard against invalid identifiers (e.g. RegID accidentally set to "0")
if reg_id and not reg_id.isidentifier():
reg_id = ""
has_code = bool(code_dict)
has_sizer_child = any(
c.get("attributes", {}).get("designerClass", "") in _SIZER_CLASS for c in children
)
# Determine variable names:
# local_var — bare name used as a Python identifier within afterInit
# var — the full assignment target (may have "self." prefix)
if reg_id:
local_var = reg_id
var = f"self.{reg_id}"
elif has_code:
local_var = self._next_var(name)
var = f"self.{local_var}"
else:
local_var = self._next_var(name)
var = local_var
kwargs = self._attrs_to_kwargs(atts, name)
kwargs_str = parent_var + (f", {kwargs}" if kwargs else "")
# Special case: dPanel with a nested sizer child
if name == "dPanel" and has_sizer_child:
lines.append(f"{pad}{var} = {name}({kwargs_str})")
sizer_children_list = [
c
for c in children
if c.get("attributes", {}).get("designerClass", "") in _SIZER_CLASS
]
for sc in sizer_children_list:
lines.extend(
self._generate_object_code(
sc, local_var, sizer_var=None, indent=indent, is_root_sizer=True
)
)
# Special case: dGrid with dColumn children
elif name == "dGrid" and children and all(c.get("name") == "dColumn" for c in children):
lines.extend(self._generate_grid_control(dct, parent_var, var, pad))
# Special case: dPageFrame / dPageFrameNoTabs
elif name in ("dPageFrame", "dPageFrameNoTabs") and children:
lines.extend(self._generate_pageframe(dct, parent_var, var, local_var, pad, indent))
# Special case: dSplitter
elif name == "dSplitter" and children:
lines.extend(self._generate_splitter(dct, parent_var, var, pad, indent))
else:
lines.append(f"{pad}{var} = {name}({kwargs_str})")
# Collect methods from child controls into the class-level buffer
if code_dict:
for mname, mcode in sorted(code_dict.items()):
if mname != "importStatements":
self._methods.append((mname, mcode.strip()))
self._last_var = var
# Append to containing sizer
if sizer_var:
si_args = self._sizer_info_to_append_args(self._control_sizer_info(name, atts))
lines.append(f"{pad}{sizer_var}.append({var}{si_args})")
lines.append("")
return lines
def _generate_grid_control(self, dct, parent_var, var, pad) -> list:
"""Generate a dGrid with its dColumn children."""
atts = dict(dct.get("attributes", {}))
name = dct.get("name", "dGrid")
kwargs = self._attrs_to_kwargs(atts, name)
kwargs_str = parent_var + (f", {kwargs}" if kwargs else "")
lines = [f"{pad}{var} = {name}({kwargs_str})"]
cols = sorted(
dct.get("children", []),
key=lambda c: int(c.get("attributes", {}).get("Order", 0)),
)
for col in cols:
col_atts = dict(col.get("attributes", {}))
for drop_key in ("designerClass", "sizerInfo", "Order", "code-ID"):
col_atts.pop(drop_key, None)
parts = []
for k, v in col_atts.items():
py_val = self._python_value(v, k)
parts.append(f"{k}={_dq(py_val)}" if isinstance(py_val, str) else f"{k}={py_val}")
col_kwargs = ", ".join(parts)
col_args = var + (f", {col_kwargs}" if col_kwargs else "")
lines.append(f"{pad}{var}.addColumn(dColumn({col_args}))")
return lines
def _generate_pageframe(self, dct, parent_var, var, local_var, pad, indent) -> list:
"""Generate a dPageFrame with its pages."""
atts = dict(dct.get("attributes", {}))
name = dct.get("name", "dPageFrame")
children = dct.get("children", [])
kwargs = self._attrs_to_kwargs(atts, name)
kwargs_str = parent_var + (f", {kwargs}" if kwargs else "")
lines = [f"{pad}{var} = {name}({kwargs_str})"]
lines.append(f"{pad}{var}.PageCount = {len(children)}")
for i, page in enumerate(children):
pg_var = f"{local_var}_pg{i}"
lines.append(f"{pad}{pg_var} = {var}.Pages[{i}]")
for child in page.get("children", []):
lines.extend(
self._generate_object_code(
child, pg_var, sizer_var=None, indent=indent, is_root_sizer=True
)
)
return lines
def _generate_splitter(self, dct, parent_var, var, pad, indent) -> list:
"""Generate a dSplitter with its two panels."""
atts = dict(dct.get("attributes", {}))
name = dct.get("name", "dSplitter")
kwargs = self._attrs_to_kwargs(atts, name)
kwargs_str = parent_var + (f", {kwargs}" if kwargs else "")
lines = [f"{pad}{var} = {name}({kwargs_str}, createPanes=True)"]
for i, (panel_attr, child) in enumerate(zip(["Panel1", "Panel2"], dct.get("children", []))):
pnl_var = f"pnl{i + 1}"
lines.append(f"{pad}{pnl_var} = {var}.{panel_attr}")
for pc in child.get("children", []):
lines.extend(
self._generate_object_code(
pc, pnl_var, sizer_var=None, indent=indent, is_root_sizer=True
)
)
return lines
def _generate_spacer(self, dct: dict, sizer_var: str | None, pad: str) -> list:
atts = dct.get("attributes", {})
spacing = atts.get("Spacing", atts.get("size", "10"))
try:
spacing = int(spacing)
except (ValueError, TypeError):
spacing = 10
lines = []
if sizer_var:
lines.append(f"{pad}{sizer_var}.appendSpacer({spacing})")
lines.append("")
return lines
# ------------------------------------------------------------------
# Attribute → kwarg helpers
# ------------------------------------------------------------------
def _attrs_to_kwargs(self, atts: dict, ctrl_name: str) -> str:
"""Return a comma-separated kwarg string for a constructor call."""
parts = []
for key, val in atts.items():
if key in _CTRL_ATTRS_EXCLUDE or key == "RegID":
continue
py_val = self._python_value(val, key)
parts.append(f"{key}={_dq(py_val)}" if isinstance(py_val, str) else f"{key}={py_val}")
return ", ".join(parts)
def _python_value(self, val_str, attr_name: str = ""):
"""Convert a designer attribute string to the best Python literal."""
if not isinstance(val_str, str):
return val_str
if val_str == "True":
return True
if val_str == "False":
return False
if attr_name in _INT_ATTRS:
try:
return int(val_str)
except ValueError:
pass
try:
return int(val_str)
except ValueError:
pass
try:
return float(val_str)
except ValueError:
pass
try:
return ast.literal_eval(val_str)
except (ValueError, SyntaxError):
pass
return val_str
def _nested_sizer_append_args(self, atts: dict, sizer_var: str) -> str:
"""
Return the .append() arguments for a sizer nested in another sizer. A
design's root sizer placed in a standard-button dialog's own Sizer fills
it, as DesignerClassConverter does, so the buttons stay at the bottom.
"""
si_args = self._sizer_info_to_append_args(atts.get("sizerInfo", "{}"))
if sizer_var == "self.Sizer" and not si_args:
si_args = ', proportion=1, layout="expand"'
return si_args
def _control_sizer_info(self, ctrl_name: str, atts: dict) -> dict:
"""
Return the effective sizer settings for a control: the designer's
per-class defaults for the enclosing sizer type, overridden by the
values saved in the design. This mirrors DesignerClassConverter, which
is what runs .cdxml designs.
"""
si = atts.get("sizerInfo", "{}")
if isinstance(si, str):
try:
si = ast.literal_eval(si) if si else {}
except (ValueError, SyntaxError):
si = {}
typ = self._sizer_types[-1] if self._sizer_types else "H"
merged = getDefaultSizerProps(ctrl_name, typ)
merged.update(si or {})
return merged
def _sizer_info_to_append_args(self, si_str) -> str:
"""Convert a sizerInfo string or dict to .append() keyword arguments."""
if not si_str or si_str == "{}":
return ""
if isinstance(si_str, str):
try:
si = ast.literal_eval(si_str)
except (ValueError, SyntaxError):
return ""
elif isinstance(si_str, dict):
si = si_str
else:
return ""
if not si:
return ""
parts = []
border = si.get("Border", 0)
proportion = si.get("Proportion", 0)
expand = si.get("Expand", False)
halign = si.get("HAlign", "Left")
valign = si.get("VAlign", "Top")
border_sides = si.get("BorderSides", ["All"])
if border:
parts.append(f"border={border}")
if proportion:
parts.append(f"proportion={proportion}")
if expand:
parts.append('layout="expand"')
# An expanded item fills its space, so alignment is meaningless; wxPython
# 4.2+ also raises an assertion if the two are combined.
if not expand and halign.lower() != "left":
parts.append(f"halign={_dq(halign.lower())}")
if not expand and valign.lower() != "top":
parts.append(f"valign={_dq(valign.lower())}")
if [side.lower() for side in border_sides] != ["all"]:
parts.append(f"borderSides={json.dumps(border_sides)}")
if not parts:
return ""
return ", " + ", ".join(parts)
# ------------------------------------------------------------------
# Method generation
# ------------------------------------------------------------------
def _generate_methods(self, code_dict: dict) -> list:
"""Generate class method lines from form code dict + child control methods."""
all_lines = []
for mname, mcode in self._methods:
all_lines.extend(self._format_method(mcode))
all_lines.append("")
for mname, mcode in sorted(code_dict.items()):
if mname == "importStatements":
continue
if mname == self._build_method:
all_lines.append(
f" # NOTE: original {mname} renamed to avoid conflict "
"with the generated one."
)
mcode = mcode.replace(f"def {mname}(", f"def _orig_{mname}(", 1)
all_lines.extend(self._format_method(mcode))
all_lines.append("")
return all_lines
def _format_method(self, mcode: str) -> list:
"""Indent method source for the class body (4 spaces)."""
return [" " + ln for ln in mcode.strip().splitlines()]
# ------------------------------------------------------------------
# Variable naming helpers
# ------------------------------------------------------------------
def _class_short_name(self, class_name: str) -> str:
"""Return the short variable prefix for a class name."""
if class_name in _CLASS_SHORT_NAME:
return _CLASS_SHORT_NAME[class_name]
# Generic fallback: strip leading "d", lowercase remainder
if class_name.startswith("d") and len(class_name) > 1:
return class_name[1].lower() + class_name[2:]
return class_name.lower()
def _next_var(self, class_name: str) -> str:
"""Return the next available variable name for *class_name*."""
short = self._class_short_name(class_name)
n = self._counters.get(short, 0)
self._counters[short] = n + 1
return f"{short}{n}"