From 4f7babb757a494033d1a52a41ced18fa90dc5e49 Mon Sep 17 00:00:00 2001 From: appel_c Date: Thu, 13 Mar 2025 15:58:43 +0100 Subject: [PATCH] refactor: cleanup MR --- bec_widgets/cli/client_utils.py | 11 ++++------- bec_widgets/cli/rpc/rpc_register.py | 16 +--------------- bec_widgets/cli/rpc/rpc_widget_handler.py | 6 ++---- bec_widgets/utils/bec_widget.py | 13 +++---------- bec_widgets/widgets/containers/dock/dock.py | 2 +- .../containers/main_window/main_window.py | 1 - tests/end-2-end/test_bec_figure_rpc_e2e.py | 7 ------- 7 files changed, 11 insertions(+), 45 deletions(-) diff --git a/bec_widgets/cli/client_utils.py b/bec_widgets/cli/client_utils.py index f69c60a6..42ca3ff9 100644 --- a/bec_widgets/cli/client_utils.py +++ b/bec_widgets/cli/client_utils.py @@ -9,11 +9,9 @@ import os import select import subprocess import threading -import time from contextlib import contextmanager from typing import TYPE_CHECKING -from bec_lib.endpoints import MessageEndpoints from bec_lib.logger import bec_logger from bec_lib.utils.import_utils import lazy_import, lazy_import_from from rich.console import Console @@ -30,7 +28,6 @@ if TYPE_CHECKING: from bec_lib.redis_connector import StreamMessage else: messages = lazy_import("bec_lib.messages") - # from bec_lib.connector import MessageObject MessageObject = lazy_import_from("bec_lib.connector", ("MessageObject",)) StreamMessage = lazy_import_from("bec_lib.redis_connector", ("StreamMessage",)) @@ -74,7 +71,7 @@ def _get_output(process, logger) -> None: def _start_plot_process( gui_id: str, gui_class: type, gui_class_id: str, config: dict | str, logger=None -) -> None: +) -> tuple[subprocess.Popen[str], threading.Thread | None]: """ Start the plot in a new process. @@ -207,7 +204,7 @@ class BECDockArea(client.BECDockArea): class BECGuiClient(RPCBase): """BEC GUI client class. Container for GUI applications within Python.""" - _top_level = {} + _top_level: dict[str, BECDockArea] = {} def __init__(self, **kwargs) -> None: super().__init__(**kwargs) @@ -223,7 +220,7 @@ class BECGuiClient(RPCBase): @property def windows(self) -> dict: - """Dictionary with dock ares in the GUI.""" + """Dictionary with dock areas in the GUI.""" return self._top_level @property @@ -457,7 +454,7 @@ class BECGuiClient(RPCBase): self._process = None -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover from bec_lib.client import BECClient from bec_lib.service_config import ServiceConfig diff --git a/bec_widgets/cli/rpc/rpc_register.py b/bec_widgets/cli/rpc/rpc_register.py index 91f03308..a84a5c41 100644 --- a/bec_widgets/cli/rpc/rpc_register.py +++ b/bec_widgets/cli/rpc/rpc_register.py @@ -8,7 +8,7 @@ from weakref import WeakValueDictionary from bec_lib.logger import bec_logger from qtpy.QtCore import QObject -if TYPE_CHECKING: +if TYPE_CHECKING: # pragma: no cover from bec_widgets.utils.bec_connector import BECConnector from bec_widgets.utils.bec_widget import BECWidget from bec_widgets.widgets.containers.dock.dock import BECDock @@ -73,20 +73,6 @@ class RPCRegister: rpc_object = self._rpc_register.get(gui_id, None) return rpc_object - def get_rpc_by_name(self, name: str) -> QObject | None: - """ - Get an RPC object by its name. - - Args: - name(str): The name of the RPC object to be retrieved. - - Returns: - QObject | None: The RPC object with the given name. - """ - rpc_object = [rpc for rpc in self._rpc_register if rpc._name == name] - rpc_object = rpc_object[0] if len(rpc_object) > 0 else None - return rpc_object - def list_all_connections(self) -> dict: """ List all the registered RPC objects. diff --git a/bec_widgets/cli/rpc/rpc_widget_handler.py b/bec_widgets/cli/rpc/rpc_widget_handler.py index cf15ba60..ace92ed5 100644 --- a/bec_widgets/cli/rpc/rpc_widget_handler.py +++ b/bec_widgets/cli/rpc/rpc_widget_handler.py @@ -13,7 +13,7 @@ class RPCWidgetHandler: self._widget_classes = None @property - def widget_classes(self) -> dict[str, Any]: + def widget_classes(self) -> dict[str, type[BECWidget]]: """ Get the available widget classes. @@ -50,9 +50,7 @@ class RPCWidgetHandler: Returns: widget(BECWidget): The created widget. """ - if self._widget_classes is None: - self.update_available_widgets() - widget_class = self._widget_classes.get(widget_type) # type: ignore + widget_class = self.widget_classes.get(widget_type) # type: ignore if widget_class: return widget_class(name=name, **kwargs) raise ValueError(f"Unknown widget type: {widget_type}") diff --git a/bec_widgets/utils/bec_widget.py b/bec_widgets/utils/bec_widget.py index 00d6deb5..3337ea6a 100644 --- a/bec_widgets/utils/bec_widget.py +++ b/bec_widgets/utils/bec_widget.py @@ -11,7 +11,7 @@ from bec_widgets.utils.bec_connector import BECConnector, ConnectionConfig from bec_widgets.utils.colors import set_theme from bec_widgets.utils.container_utils import WidgetContainerUtils -if TYPE_CHECKING: +if TYPE_CHECKING: # pragma: no cover from bec_widgets.widgets.containers.dock import BECDock logger = bec_logger.logger @@ -55,13 +55,7 @@ class BECWidget(BECConnector): """ if not isinstance(self, QWidget): raise RuntimeError(f"{repr(self)} is not a subclass of QWidget") - # Create a default name if None is provided - if name is None: - name = "bec_widget_init_without_name" - # name = self.__class__.__name__ - # Check for invalid chars in the name - if not WidgetContainerUtils.has_name_valid_chars(name): - raise ValueError(f"Name {name} contains invalid characters.") + super().__init__(client=client, config=config, gui_id=gui_id, name=name) self._parent_dock = parent_dock app = QApplication.instance() @@ -104,8 +98,7 @@ class BECWidget(BECConnector): def cleanup(self): """Cleanup the widget.""" - # needed here instead of closeEvent, to be checked why - # However, all widgets need to call super().cleanup() in their cleanup method + # All widgets need to call super().cleanup() in their cleanup method self.rpc_register.remove_rpc(self) def closeEvent(self, event): diff --git a/bec_widgets/widgets/containers/dock/dock.py b/bec_widgets/widgets/containers/dock/dock.py index e5857978..e100b19b 100644 --- a/bec_widgets/widgets/containers/dock/dock.py +++ b/bec_widgets/widgets/containers/dock/dock.py @@ -434,7 +434,7 @@ class BECDock(BECWidget, Dock): super().close() -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover import sys from qtpy.QtWidgets import QApplication diff --git a/bec_widgets/widgets/containers/main_window/main_window.py b/bec_widgets/widgets/containers/main_window/main_window.py index 2ef9241c..76b689b4 100644 --- a/bec_widgets/widgets/containers/main_window/main_window.py +++ b/bec_widgets/widgets/containers/main_window/main_window.py @@ -71,5 +71,4 @@ class BECMainWindow(BECWidget, QMainWindow): return dock_area def cleanup(self): - # TODO super().close() diff --git a/tests/end-2-end/test_bec_figure_rpc_e2e.py b/tests/end-2-end/test_bec_figure_rpc_e2e.py index 81f26a57..dac1535a 100644 --- a/tests/end-2-end/test_bec_figure_rpc_e2e.py +++ b/tests/end-2-end/test_bec_figure_rpc_e2e.py @@ -18,7 +18,6 @@ def connected_figure(connected_client_gui_obj): def test_rpc_waveform1d_custom_curve(connected_figure): fig = connected_figure - # fig = BECFigure(connected_client_figure) ax = fig.plot() curve = ax.plot(x=[1, 2, 3], y=[1, 2, 3]) @@ -32,7 +31,6 @@ def test_rpc_waveform1d_custom_curve(connected_figure): def test_rpc_plotting_shortcuts_init_configs(connected_figure, qtbot): fig = connected_figure - # fig = BECFigure(connected_client_figure) plt = fig.plot(x_name="samx", y_name="bpm4i") im = fig.image("eiger") @@ -90,7 +88,6 @@ def test_rpc_plotting_shortcuts_init_configs(connected_figure, qtbot): def test_rpc_waveform_scan(qtbot, connected_figure, bec_client_lib): - # fig = BECFigure(connected_client_figure) fig = connected_figure # add 3 different curves to track plt = fig.plot(x_name="samx", y_name="bpm4i") @@ -126,7 +123,6 @@ def test_rpc_waveform_scan(qtbot, connected_figure, bec_client_lib): def test_rpc_image(connected_figure, bec_client_lib): - # fig = BECFigure(connected_client_figure) fig = connected_figure im = fig.image("eiger") @@ -148,7 +144,6 @@ def test_rpc_image(connected_figure, bec_client_lib): def test_rpc_motor_map(connected_figure, bec_client_lib): - # fig = BECFigure(connected_client_figure) fig = connected_figure motor_map = fig.motor_map("samx", "samy") @@ -180,7 +175,6 @@ def test_rpc_motor_map(connected_figure, bec_client_lib): def test_dap_rpc(connected_figure, bec_client_lib, qtbot): fig = connected_figure - # fig = BECFigure(connected_client_figure) plt = fig.plot(x_name="samx", y_name="bpm4i", dap="GaussianModel") client = bec_client_lib @@ -219,7 +213,6 @@ def test_dap_rpc(connected_figure, bec_client_lib, qtbot): def test_removing_subplots(connected_figure, bec_client_lib): - # fig = BECFigure(connected_client_figure) fig = connected_figure plt = fig.plot(x_name="samx", y_name="bpm4i", dap="GaussianModel") im = fig.image(monitor="eiger")