0
0
mirror of https://github.com/bec-project/bec_widgets.git synced 2025-07-14 03:31:50 +02:00

feat: rpc decorator to add methods to USER_ACCESS

This commit is contained in:
wyzula-jan
2024-02-14 18:32:33 +01:00
parent 7768e594b5
commit b676877242
4 changed files with 37 additions and 14 deletions

View File

@ -4,3 +4,4 @@ from .validator_delegate import DoubleValidationDelegate
from .bec_table import BECTable
from .bec_connector import BECConnector, ConnectionConfig
from .bec_dispatcher import BECDispatcher
from .rpc_decorator import rpc_public, register_rpc_methods

View File

@ -0,0 +1,15 @@
def rpc_public(func):
func.rpc_public = True # Mark the function for later processing by the class decorator
return func
def register_rpc_methods(cls):
"""
Class decorator to scan for rpc_public methods and add them to USER_ACCESS.
"""
if not hasattr(cls, "USER_ACCESS"):
cls.USER_ACCESS = set()
for name, method in cls.__dict__.items():
if getattr(method, "rpc_public", False):
cls.USER_ACCESS.add(name)
return cls

View File

@ -10,7 +10,13 @@ from pydantic import Field
from pyqtgraph.Qt import uic
from qtpy.QtWidgets import QApplication, QWidget
from bec_widgets.utils import BECDispatcher, BECConnector, ConnectionConfig
from bec_widgets.utils import (
BECDispatcher,
BECConnector,
ConnectionConfig,
register_rpc_methods,
rpc_public,
)
from bec_widgets.widgets.plots import WidgetConfig, BECPlotBase
@ -24,6 +30,7 @@ class FigureConfig(ConnectionConfig):
)
@register_rpc_methods
class BECFigure(BECConnector, pg.GraphicsLayoutWidget):
def __init__(
self,
@ -56,6 +63,7 @@ class BECFigure(BECConnector, pg.GraphicsLayoutWidget):
# if hasattr(self, "window"):
# self.window.close()
@rpc_public
def add_widget(self, widget_id: str = None, row: int = None, col: int = None, **kwargs):
# Generate unique widget_id if not provided
if not widget_id:

View File

@ -5,7 +5,7 @@ import numpy as np
from pydantic import BaseModel, Field
from qtpy.QtWidgets import QWidget
from bec_widgets.utils import BECConnector, ConnectionConfig
from bec_widgets.utils import BECConnector, ConnectionConfig, register_rpc_methods, rpc_public
class AxisConfig(BaseModel):
@ -33,19 +33,8 @@ class WidgetConfig(ConnectionConfig):
)
@register_rpc_methods
class BECPlotBase(BECConnector, pg.PlotItem):
USER_ACCESS = [
"set",
"set_title",
"set_x_label",
"set_y_label",
"set_x_scale",
"set_y_scale",
"set_x_lim",
"set_y_lim",
"plot",
]
def __init__(
self,
parent: Optional[QWidget] = None, # TODO decide if needed for this class
@ -58,6 +47,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
super().__init__(client=client, config=config, gui_id=gui_id)
pg.PlotItem.__init__(self, parent)
@rpc_public
def set(self, **kwargs) -> None:
"""
Set the properties of the plot widget.
@ -101,6 +91,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.set(**{k: v for k, v in config_mappings.items() if v is not None})
@rpc_public
def set_title(self, title: str):
"""
Set the title of the plot widget.
@ -110,6 +101,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.plt.setTitle(title)
self.config.axis.title = title
@rpc_public
def set_x_label(self, label: str):
"""
Set the label of the x-axis.
@ -119,6 +111,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.plt.setLabel("bottom", label)
self.config.axis.x_label = label
@rpc_public
def set_y_label(self, label: str):
"""
Set the label of the y-axis.
@ -128,6 +121,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.plt.setLabel("left", label)
self.config.axis.y_label = label
@rpc_public
def set_x_scale(self, scale: Literal["linear", "log"] = "linear"):
"""
Set the scale of the x-axis.
@ -137,6 +131,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.plt.setLogMode(x=(scale == "log"))
self.config.axis.x_scale = scale
@rpc_public
def set_y_scale(self, scale: Literal["linear", "log"] = "linear"):
"""
Set the scale of the y-axis.
@ -146,6 +141,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.plt.setLogMode(y=(scale == "log"))
self.config.axis.y_scale = scale
@rpc_public
def set_x_lim(self, x_lim: tuple) -> None:
"""
Set the limits of the x-axis.
@ -155,6 +151,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.plt.setXRange(x_lim[0], x_lim[1])
self.config.axis.x_lim = x_lim
@rpc_public
def set_y_lim(self, y_lim: tuple) -> None:
"""
Set the limits of the y-axis.
@ -164,6 +161,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.plt.setYRange(y_lim[0], y_lim[1])
self.config.axis.y_lim = y_lim
@rpc_public
def set_grid(self, x: bool = False, y: bool = False):
"""
Set the grid of the plot widget.
@ -175,6 +173,7 @@ class BECPlotBase(BECConnector, pg.PlotItem):
self.config.axis.x_grid = x
self.config.axis.y_grid = y
@rpc_public
def plot_data(self, data_x: list | np.ndarray, data_y: list | np.ndarray, label: str = None):
"""
Plot custom data on the plot widget. These data are not saved in config.