mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-03-04 16:02:51 +01:00
fix: factor out device name function and add test
This commit is contained in:
@@ -25,6 +25,20 @@ else:
|
||||
# pylint: disable=protected-access
|
||||
|
||||
|
||||
def _name_arg(arg):
|
||||
if isinstance(arg, DeviceBaseWithConfig):
|
||||
# if dev.<device> is passed to GUI, it passes full_name
|
||||
if hasattr(arg, "full_name"):
|
||||
return arg.full_name
|
||||
elif hasattr(arg, "name"):
|
||||
return arg.name
|
||||
return arg
|
||||
|
||||
|
||||
def _transform_args_kwargs(args, kwargs) -> tuple[tuple, dict]:
|
||||
return tuple(_name_arg(arg) for arg in args), {k: _name_arg(v) for k, v in kwargs.items()}
|
||||
|
||||
|
||||
def rpc_call(func):
|
||||
"""
|
||||
A decorator for calling a function on the server.
|
||||
@@ -48,25 +62,7 @@ def rpc_call(func):
|
||||
return None # func(*args, **kwargs)
|
||||
caller_frame = caller_frame.f_back
|
||||
|
||||
out = []
|
||||
for arg in args:
|
||||
if isinstance(
|
||||
arg, DeviceBaseWithConfig
|
||||
): # if dev.<device> is passed to GUI, it passes full_name
|
||||
if hasattr(arg, "full_name"):
|
||||
arg = arg.full_name
|
||||
elif hasattr(arg, "name"):
|
||||
arg = arg.name
|
||||
out.append(arg)
|
||||
args = tuple(out)
|
||||
for key, val in kwargs.items():
|
||||
if isinstance(
|
||||
val, DeviceBaseWithConfig
|
||||
): # if dev.<device> is passed to GUI, it passes full_name
|
||||
if hasattr(val, "full_name"):
|
||||
kwargs[key] = val.full_name
|
||||
elif hasattr(val, "name"):
|
||||
kwargs[key] = val.name
|
||||
args, kwargs = _transform_args_kwargs(args, kwargs)
|
||||
if not self._root._gui_is_alive():
|
||||
raise RuntimeError("GUI is not alive")
|
||||
return self._run_rpc(func.__name__, *args, **kwargs)
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
import pytest
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
from bec_widgets.cli.rpc.rpc_base import DeletedWidgetError, RPCBase, RPCReference
|
||||
import pytest
|
||||
from bec_lib.device import DeviceBaseWithConfig, Signal
|
||||
|
||||
from bec_widgets.cli.rpc.rpc_base import (
|
||||
DeletedWidgetError,
|
||||
RPCBase,
|
||||
RPCReference,
|
||||
_transform_args_kwargs,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -26,3 +34,20 @@ def test_rpc_base(rpc_base):
|
||||
|
||||
with pytest.raises(DeletedWidgetError):
|
||||
ref._root # Object no longer referenced in registry
|
||||
|
||||
|
||||
def test_transform_args_kwargs():
|
||||
device_mock = MagicMock(spec=DeviceBaseWithConfig)
|
||||
device_mock.full_name = "full name"
|
||||
fallthrough_device_mock = MagicMock()
|
||||
fallthrough_device_mock.name = "short name"
|
||||
string_arg = "string_arg"
|
||||
signal_mock = MagicMock(spec=Signal)
|
||||
signal_mock.full_name = "full name"
|
||||
|
||||
args, kwargs = _transform_args_kwargs(
|
||||
(device_mock, fallthrough_device_mock, string_arg, signal_mock),
|
||||
{"a": device_mock, "b": fallthrough_device_mock, "c": string_arg, "d": signal_mock},
|
||||
)
|
||||
assert args == ("full name", "short name", "string_arg", "full name")
|
||||
assert kwargs == {"a": "full name", "b": "short name", "c": "string_arg", "d": "full name"}
|
||||
|
||||
Reference in New Issue
Block a user