0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 11:41:49 +02:00

refactor: cleanup MR

This commit is contained in:
2025-03-13 15:58:43 +01:00
committed by wyzula-jan
parent 08f5a1cceb
commit 4f7babb757
7 changed files with 11 additions and 45 deletions

View File

@ -9,11 +9,9 @@ import os
import select import select
import subprocess import subprocess
import threading import threading
import time
from contextlib import contextmanager from contextlib import contextmanager
from typing import TYPE_CHECKING from typing import TYPE_CHECKING
from bec_lib.endpoints import MessageEndpoints
from bec_lib.logger import bec_logger from bec_lib.logger import bec_logger
from bec_lib.utils.import_utils import lazy_import, lazy_import_from from bec_lib.utils.import_utils import lazy_import, lazy_import_from
from rich.console import Console from rich.console import Console
@ -30,7 +28,6 @@ if TYPE_CHECKING:
from bec_lib.redis_connector import StreamMessage from bec_lib.redis_connector import StreamMessage
else: else:
messages = lazy_import("bec_lib.messages") messages = lazy_import("bec_lib.messages")
# from bec_lib.connector import MessageObject
MessageObject = lazy_import_from("bec_lib.connector", ("MessageObject",)) MessageObject = lazy_import_from("bec_lib.connector", ("MessageObject",))
StreamMessage = lazy_import_from("bec_lib.redis_connector", ("StreamMessage",)) StreamMessage = lazy_import_from("bec_lib.redis_connector", ("StreamMessage",))
@ -74,7 +71,7 @@ def _get_output(process, logger) -> None:
def _start_plot_process( def _start_plot_process(
gui_id: str, gui_class: type, gui_class_id: str, config: dict | str, logger=None 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. Start the plot in a new process.
@ -207,7 +204,7 @@ class BECDockArea(client.BECDockArea):
class BECGuiClient(RPCBase): class BECGuiClient(RPCBase):
"""BEC GUI client class. Container for GUI applications within Python.""" """BEC GUI client class. Container for GUI applications within Python."""
_top_level = {} _top_level: dict[str, BECDockArea] = {}
def __init__(self, **kwargs) -> None: def __init__(self, **kwargs) -> None:
super().__init__(**kwargs) super().__init__(**kwargs)
@ -223,7 +220,7 @@ class BECGuiClient(RPCBase):
@property @property
def windows(self) -> dict: def windows(self) -> dict:
"""Dictionary with dock ares in the GUI.""" """Dictionary with dock areas in the GUI."""
return self._top_level return self._top_level
@property @property
@ -457,7 +454,7 @@ class BECGuiClient(RPCBase):
self._process = None self._process = None
if __name__ == "__main__": if __name__ == "__main__": # pragma: no cover
from bec_lib.client import BECClient from bec_lib.client import BECClient
from bec_lib.service_config import ServiceConfig from bec_lib.service_config import ServiceConfig

View File

@ -8,7 +8,7 @@ from weakref import WeakValueDictionary
from bec_lib.logger import bec_logger from bec_lib.logger import bec_logger
from qtpy.QtCore import QObject 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_connector import BECConnector
from bec_widgets.utils.bec_widget import BECWidget from bec_widgets.utils.bec_widget import BECWidget
from bec_widgets.widgets.containers.dock.dock import BECDock from bec_widgets.widgets.containers.dock.dock import BECDock
@ -73,20 +73,6 @@ class RPCRegister:
rpc_object = self._rpc_register.get(gui_id, None) rpc_object = self._rpc_register.get(gui_id, None)
return rpc_object 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: def list_all_connections(self) -> dict:
""" """
List all the registered RPC objects. List all the registered RPC objects.

View File

@ -13,7 +13,7 @@ class RPCWidgetHandler:
self._widget_classes = None self._widget_classes = None
@property @property
def widget_classes(self) -> dict[str, Any]: def widget_classes(self) -> dict[str, type[BECWidget]]:
""" """
Get the available widget classes. Get the available widget classes.
@ -50,9 +50,7 @@ class RPCWidgetHandler:
Returns: Returns:
widget(BECWidget): The created widget. widget(BECWidget): The created widget.
""" """
if self._widget_classes is None: widget_class = self.widget_classes.get(widget_type) # type: ignore
self.update_available_widgets()
widget_class = self._widget_classes.get(widget_type) # type: ignore
if widget_class: if widget_class:
return widget_class(name=name, **kwargs) return widget_class(name=name, **kwargs)
raise ValueError(f"Unknown widget type: {widget_type}") raise ValueError(f"Unknown widget type: {widget_type}")

View File

@ -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.colors import set_theme
from bec_widgets.utils.container_utils import WidgetContainerUtils 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 from bec_widgets.widgets.containers.dock import BECDock
logger = bec_logger.logger logger = bec_logger.logger
@ -55,13 +55,7 @@ class BECWidget(BECConnector):
""" """
if not isinstance(self, QWidget): if not isinstance(self, QWidget):
raise RuntimeError(f"{repr(self)} is not a subclass of 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) super().__init__(client=client, config=config, gui_id=gui_id, name=name)
self._parent_dock = parent_dock self._parent_dock = parent_dock
app = QApplication.instance() app = QApplication.instance()
@ -104,8 +98,7 @@ class BECWidget(BECConnector):
def cleanup(self): def cleanup(self):
"""Cleanup the widget.""" """Cleanup the widget."""
# needed here instead of closeEvent, to be checked why # All widgets need to call super().cleanup() in their cleanup method
# However, all widgets need to call super().cleanup() in their cleanup method
self.rpc_register.remove_rpc(self) self.rpc_register.remove_rpc(self)
def closeEvent(self, event): def closeEvent(self, event):

View File

@ -434,7 +434,7 @@ class BECDock(BECWidget, Dock):
super().close() super().close()
if __name__ == "__main__": if __name__ == "__main__": # pragma: no cover
import sys import sys
from qtpy.QtWidgets import QApplication from qtpy.QtWidgets import QApplication

View File

@ -71,5 +71,4 @@ class BECMainWindow(BECWidget, QMainWindow):
return dock_area return dock_area
def cleanup(self): def cleanup(self):
# TODO
super().close() super().close()

View File

@ -18,7 +18,6 @@ def connected_figure(connected_client_gui_obj):
def test_rpc_waveform1d_custom_curve(connected_figure): def test_rpc_waveform1d_custom_curve(connected_figure):
fig = connected_figure fig = connected_figure
# fig = BECFigure(connected_client_figure)
ax = fig.plot() ax = fig.plot()
curve = ax.plot(x=[1, 2, 3], y=[1, 2, 3]) 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): def test_rpc_plotting_shortcuts_init_configs(connected_figure, qtbot):
fig = connected_figure fig = connected_figure
# fig = BECFigure(connected_client_figure)
plt = fig.plot(x_name="samx", y_name="bpm4i") plt = fig.plot(x_name="samx", y_name="bpm4i")
im = fig.image("eiger") 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): def test_rpc_waveform_scan(qtbot, connected_figure, bec_client_lib):
# fig = BECFigure(connected_client_figure)
fig = connected_figure fig = connected_figure
# add 3 different curves to track # add 3 different curves to track
plt = fig.plot(x_name="samx", y_name="bpm4i") 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): def test_rpc_image(connected_figure, bec_client_lib):
# fig = BECFigure(connected_client_figure)
fig = connected_figure fig = connected_figure
im = fig.image("eiger") 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): def test_rpc_motor_map(connected_figure, bec_client_lib):
# fig = BECFigure(connected_client_figure)
fig = connected_figure fig = connected_figure
motor_map = fig.motor_map("samx", "samy") 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): def test_dap_rpc(connected_figure, bec_client_lib, qtbot):
fig = connected_figure fig = connected_figure
# fig = BECFigure(connected_client_figure)
plt = fig.plot(x_name="samx", y_name="bpm4i", dap="GaussianModel") plt = fig.plot(x_name="samx", y_name="bpm4i", dap="GaussianModel")
client = bec_client_lib 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): def test_removing_subplots(connected_figure, bec_client_lib):
# fig = BECFigure(connected_client_figure)
fig = connected_figure fig = connected_figure
plt = fig.plot(x_name="samx", y_name="bpm4i", dap="GaussianModel") plt = fig.plot(x_name="samx", y_name="bpm4i", dap="GaussianModel")
im = fig.image(monitor="eiger") im = fig.image(monitor="eiger")