Debian Codegraph @

scripts/print_datalab_menus.py

raw
1 # Copyright (c) DataLab Platform Developers, BSD 3-Clause license, see LICENSE file.
2
3 """Print available DataLab menus and their actions."""
4
5 from __future__ import annotations
6
7 import os.path as osp
8
9 import sigima.objects
10
11 from datalab.gui.main import DLMainWindow
12 from datalab.tests import datalab_test_app_context
13
14
15 def parse_menu_as_text(win: DLMainWindow) -> str:
16 """Recursively parse a menu and its actions into a text representation.
17
18 Args:
19 menu: The menu to parse
20 indent: Current indentation level
21
22 Returns:
23 A string representation of the menu and its actions
24 """
25
26 def parse_actions(actions, indent_level: int) -> list[str]:
27 """Recursively parse menu actions including submenus"""
28 lines = []
29 indent = " " * indent_level
30 for action in actions:
31 if action.isSeparator():
32 # Skip separators
33 continue
34 if action.menu() is not None:
35 # Submenu: recursively parse its actions
36 lines.append(f"{indent}{action.text()}")
37 submenu_actions = action.menu().actions()
38 lines.extend(parse_actions(submenu_actions, indent_level + 1))
39 else:
40 lines.append(f"{indent}{action.text()}")
41 return lines
42
43 txtlist = []
44 for panel_name in ("signal", "image"):
45 win.set_current_panel(panel_name)
46 txtlist.append(f"Menus for {panel_name} panel:")
47 for name in (
48 "file",
49 "create",
50 "edit",
51 "roi",
52 "operation",
53 "processing",
54 "analysis",
55 "view",
56 "help",
57 ):
58 menu = getattr(win, f"{name}_menu")
59 # Update menu content before parsing
60 if name == "file":
61 win._DLMainWindow__update_file_menu()
62 elif name == "view":
63 win._DLMainWindow__update_view_menu()
64 elif name != "help":
65 win._DLMainWindow__update_generic_menu(menu)
66 txtlist.append(f" {menu.title().replace('&', '')}:")
67 txtlist.extend(parse_actions(menu.actions(), 2))
68 txtlist.append("")
69 return "\n".join(txtlist)
70
71
72 def print_datalab_menus() -> None:
73 """Print available DataLab menus and their actions."""
74 with datalab_test_app_context(console=False, exec_loop=False) as win:
75 # Add a signal and an image to have more actions in the menus
76 sig = sigima.objects.create_signal_from_param(sigima.objects.LorentzParam())
77 win.signalpanel.add_object(sig)
78 param = sigima.objects.Gauss2DParam.create(height=100, width=100)
79 ima = sigima.objects.create_image_from_param(param)
80 win.imagepanel.add_object(ima)
81 txt = parse_menu_as_text(win)
82 count = txt.count("......")
83 if count > 0:
84 raise RuntimeError(
85 f"Double ellipsis found in menu text output ({count} occurrences)."
86 )
87 print(txt)
88 output_path = osp.join(osp.dirname(__file__), "datalab_menus.txt")
89 with open(output_path, "w", encoding="utf-8") as f:
90 f.write(txt)
91 print(f"Menu text output saved to {output_path}")
92
93
94 if __name__ == "__main__":
95 print_datalab_menus()