mirror of
https://github.com/bec-project/bec_widgets.git
synced 2026-06-06 05:18:40 +02:00
feat(generate_cli): added support for property and qproperty setter
This commit is contained in:
@@ -8,6 +8,7 @@ import sys
|
|||||||
|
|
||||||
import black
|
import black
|
||||||
import isort
|
import isort
|
||||||
|
from qtpy.QtCore import Property as QtProperty
|
||||||
|
|
||||||
from bec_widgets.utils.generate_designer_plugin import DesignerPluginGenerator
|
from bec_widgets.utils.generate_designer_plugin import DesignerPluginGenerator
|
||||||
from bec_widgets.utils.plugin_utils import BECClassContainer, get_rpc_classes
|
from bec_widgets.utils.plugin_utils import BECClassContainer, get_rpc_classes
|
||||||
@@ -90,11 +91,27 @@ class {class_name}(RPCBase):"""
|
|||||||
self.content += """...
|
self.content += """...
|
||||||
"""
|
"""
|
||||||
for method in cls.USER_ACCESS:
|
for method in cls.USER_ACCESS:
|
||||||
obj = getattr(cls, method)
|
is_property_setter = False
|
||||||
if isinstance(obj, property):
|
obj = getattr(cls, method, None)
|
||||||
self.content += """
|
if obj is None:
|
||||||
|
obj = getattr(cls, method.split(".setter")[0], None)
|
||||||
|
is_property_setter = True
|
||||||
|
method = method.split(".setter")[0]
|
||||||
|
if obj is None:
|
||||||
|
raise AttributeError(
|
||||||
|
f"Method {method} not found in class {cls.__name__}. Please check the USER_ACCESS list."
|
||||||
|
)
|
||||||
|
if isinstance(obj, (property, QtProperty)):
|
||||||
|
# for the cli, we can map qt properties to regular properties
|
||||||
|
if is_property_setter:
|
||||||
|
self.content += f"""
|
||||||
|
@{method}.setter
|
||||||
|
@rpc_call"""
|
||||||
|
else:
|
||||||
|
self.content += """
|
||||||
@property
|
@property
|
||||||
@rpc_call"""
|
@rpc_call"""
|
||||||
|
|
||||||
sig = str(inspect.signature(obj.fget))
|
sig = str(inspect.signature(obj.fget))
|
||||||
doc = inspect.getdoc(obj.fget)
|
doc = inspect.getdoc(obj.fget)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -86,10 +86,15 @@ class BECWidgetsCLIServer:
|
|||||||
return obj
|
return obj
|
||||||
|
|
||||||
def run_rpc(self, obj, method, args, kwargs):
|
def run_rpc(self, obj, method, args, kwargs):
|
||||||
|
logger.debug(f"Running RPC instruction: {method} with args: {args}, kwargs: {kwargs}")
|
||||||
method_obj = getattr(obj, method)
|
method_obj = getattr(obj, method)
|
||||||
# check if the method accepts args and kwargs
|
# check if the method accepts args and kwargs
|
||||||
if not callable(method_obj):
|
if not callable(method_obj):
|
||||||
res = method_obj
|
if not args:
|
||||||
|
res = method_obj
|
||||||
|
else:
|
||||||
|
setattr(obj, method, args[0])
|
||||||
|
res = None
|
||||||
else:
|
else:
|
||||||
sig = inspect.signature(method_obj)
|
sig = inspect.signature(method_obj)
|
||||||
if sig.parameters:
|
if sig.parameters:
|
||||||
@@ -245,5 +250,5 @@ def main():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__": # pragma: no cover
|
if __name__ == "__main__": # pragma: no cover
|
||||||
sys.argv = ["bec_widgets.cli.server", "--id", "test", "--gui_class", "BECDockArea"]
|
sys.argv = ["bec_widgets.cli.server", "--id", "e2860", "--gui_class", "BECDockArea"]
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user