From 80afb3946d9bb1dcbcd1a7108d3e3ce39b1da641 Mon Sep 17 00:00:00 2001 From: Tapeline Date: Sat, 21 Dec 2024 15:14:01 +0500 Subject: [PATCH] Add debug tools --- embark/localization/en.py | 7 ++++ embark/ui/debug_frames/__init__.py | 0 embark/ui/debug_frames/exec_frame.py | 53 ++++++++++++++++++++++++++++ embark/ui/debug_frames/vars_frame.py | 51 ++++++++++++++++++++++++++ embark/ui/logger_frame/frame.py | 23 ++++++++++++ embark/ui/main.py | 1 - 6 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 embark/ui/debug_frames/__init__.py create mode 100644 embark/ui/debug_frames/exec_frame.py create mode 100644 embark/ui/debug_frames/vars_frame.py diff --git a/embark/localization/en.py b/embark/localization/en.py index 8adfa96..0d3746a 100644 --- a/embark/localization/en.py +++ b/embark/localization/en.py @@ -19,3 +19,10 @@ class UI: ask_title = "Question" you_sure_want_exit = "Are you sure you want to interrupt the playbook and exit immediately?" stop_playbook = "Stop execution" + + debug = "Debug tools:" + debug_vars = "Debug variables" + debug_exec = "Execute in scope" + debug_vars_section = "Playbook variables" + debug_envs_section = "Environment variables" + execute_expression = "Execute code" diff --git a/embark/ui/debug_frames/__init__.py b/embark/ui/debug_frames/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/embark/ui/debug_frames/exec_frame.py b/embark/ui/debug_frames/exec_frame.py new file mode 100644 index 0000000..62e8c46 --- /dev/null +++ b/embark/ui/debug_frames/exec_frame.py @@ -0,0 +1,53 @@ +"""Eval debug UI frame""" + +from customtkinter import CTk, CTkFont, CTkLabel, CTkEntry, CTkButton, CTkTextbox + +from embark.domain.execution.playbook import Playbook +from embark.localization.i18n import L +from embark.resources import get_resource +from embark.ui import utils + + +class DebugExecFrame(CTk): + """Main UI frame""" + + def __init__(self, playbook: Playbook): + super().__init__() + self._playbook = playbook + self.title(L("UI.debug_exec")) + self.geometry("500x600") + self.resizable(False, False) + self.iconbitmap(get_resource("icon.ico")) + utils.center(self) + self._font = CTkFont("Consolas", 14, "normal") + self._setup_ui() + + def _setup_ui(self): + """Create and place UI components""" + self._cmd = CTkTextbox(self, font=self._font) + self._execute_btn = CTkButton( + self, + text=L("UI.execute_expression"), + command=self._exec + ) + self._result_box = CTkTextbox(self, font=self._font) + self._cmd.pack(anchor="nw", padx=4, pady=4, fill="both", expand=True) + self._execute_btn.pack(anchor="nw", padx=4, pady=4, fill="x") + self._result_box.pack(anchor="nw", padx=4, pady=4, fill="both", expand=True) + + def _log(self, *args): + self._result_box.insert("end", " ".join(map(str, args)) + "\n") + + def _exec(self): + exec( + self._cmd.get("0.0", "end"), + { + "playbook": self._playbook, + "print": self._log + } + ) + + +def show(playbook): + win = DebugExecFrame(playbook) + win.mainloop() diff --git a/embark/ui/debug_frames/vars_frame.py b/embark/ui/debug_frames/vars_frame.py new file mode 100644 index 0000000..a8cb9ad --- /dev/null +++ b/embark/ui/debug_frames/vars_frame.py @@ -0,0 +1,51 @@ +"""Variables debug UI frame""" + +from customtkinter import CTk, CTkFont, CTkLabel, CTkScrollableFrame + +from embark.domain.execution.playbook import Playbook +from embark.localization.i18n import L +from embark.resources import get_resource +from embark.ui import utils + + +class DebugVariablesFrame(CTk): + """Main UI frame""" + + def __init__(self, playbook: Playbook): + super().__init__() + self._playbook = playbook + self.title(L("UI.debug_vars")) + self.geometry("500x600") + self.resizable(False, False) + self.iconbitmap(get_resource("icon.ico")) + utils.center(self) + self._font = CTkFont("Consolas", 14, "normal") + self._setup_ui() + + def _setup_ui(self): + """Create and place UI components""" + variables = self._playbook.variables.vars.items() + envs = self._playbook.variables.envs.items() + self._scrollable1 = CTkScrollableFrame(self) + self._scrollable = CTkScrollableFrame(self._scrollable1, orientation="horizontal") + for i in range(len(variables) + len(envs) + 2): + self._scrollable.rowconfigure(index=i, weight=1) + self._scrollable.columnconfigure(index=0, weight=1) + self._scrollable.columnconfigure(index=1, weight=1) + self._vars_title = CTkLabel(self._scrollable, text=L("UI.debug_vars_section")) + self._envs_title = CTkLabel(self._scrollable, text=L("UI.debug_envs_section")) + self._vars_title.grid(row=0, column=0, columnspan=2, sticky="nw") + row = 1 + for var, val in variables: + var_label = CTkLabel(self._scrollable, text=var, font=self._font) + val_label = CTkLabel(self._scrollable, text=val, font=self._font) + var_label.grid(row=row, column=0, sticky="nw") + val_label.grid(row=row, column=1, sticky="nw", padx=4) + row += 1 + self._scrollable.pack(fill="both", expand=True) + self._scrollable1.pack(fill="both", expand=True) + + +def show(playbook): + win = DebugVariablesFrame(playbook) + win.mainloop() diff --git a/embark/ui/logger_frame/frame.py b/embark/ui/logger_frame/frame.py index 128927b..41fbad3 100644 --- a/embark/ui/logger_frame/frame.py +++ b/embark/ui/logger_frame/frame.py @@ -13,6 +13,7 @@ from embark.localization.i18n import L from embark.resources import get_resource from embark.ui import utils +from embark.ui.debug_frames import vars_frame, exec_frame from embark.ui.logger_frame.components import GUILoggerFrame from embark.ui.logger_frame.context_factory import GUIContextFactory @@ -47,9 +48,23 @@ def _setup_ui(self): text=L("UI.stop_playbook"), command=self._stop_playbook_cmd ) + self._lp_debug_title = CTkLabel(self._left_pane, text=L("UI.debug")) + self._lp_debug_vars_btn = CTkButton( + self._left_pane, + text=L("UI.debug_vars"), + command=self._debug_vars_cmd + ) + self._lp_debug_eval_btn = CTkButton( + self._left_pane, + text=L("UI.debug_exec"), + command=self._debug_eval_cmd + ) self._lp_title.pack(padx=8, pady=8, fill="x") self._lp_subtitle.pack(fill="x") self._lp_stop_btn.pack(padx=8, pady=8, fill="x") + self._lp_debug_title.pack(padx=8, pady=8, fill="x") + self._lp_debug_vars_btn.pack(padx=8, pady=8, fill="x") + self._lp_debug_eval_btn.pack(padx=8, pady=8, fill="x") self._lp_copyright = CTkLabel(self._left_pane, text="© Tapeline 2024") self._lp_copyright.pack(side=BOTTOM) @@ -63,6 +78,14 @@ def _stop_playbook_cmd(self): if messagebox.askyesno(L("UI.ask_title"), L("UI.you_sure_want_exit")): self.destroy() + def _debug_vars_cmd(self): + if self._logger_frame.playbook is not None: + vars_frame.show(self._logger_frame.playbook) + + def _debug_eval_cmd(self): + if self._logger_frame.playbook is not None: + exec_frame.show(self._logger_frame.playbook) + def run(self): self.after(500, self._start_thread) self.mainloop() diff --git a/embark/ui/main.py b/embark/ui/main.py index 3e728c9..33d85df 100644 --- a/embark/ui/main.py +++ b/embark/ui/main.py @@ -2,7 +2,6 @@ Main UI file """ -from embark.commands import cmd_run from embark.ui.playbook_choose_frame.frame import ask_for_playbook from embark.ui.logger_frame.frame import LoggerFrame