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

fix(plugin_utils): plugin utils are able to detect classes for plugin creation based on class attribute rather than if it is top level widget

This commit is contained in:
2024-11-13 13:44:40 +01:00
parent 245ebb444e
commit 7a1b8748a4
6 changed files with 25 additions and 20 deletions

View File

@ -11,7 +11,7 @@ import isort
from qtpy.QtCore import Property as QtProperty 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_custom_classes
if sys.version_info >= (3, 11): if sys.version_info >= (3, 11):
from typing import get_overloads from typing import get_overloads
@ -175,7 +175,7 @@ def main():
current_path = os.path.dirname(__file__) current_path = os.path.dirname(__file__)
client_path = os.path.join(current_path, "client.py") client_path = os.path.join(current_path, "client.py")
rpc_classes = get_rpc_classes("bec_widgets") rpc_classes = get_custom_classes("bec_widgets")
generator = ClientGenerator() generator = ClientGenerator()
generator.generate_client(rpc_classes) generator.generate_client(rpc_classes)

View File

@ -28,10 +28,10 @@ class RPCWidgetHandler:
Returns: Returns:
None None
""" """
from bec_widgets.utils.plugin_utils import get_rpc_classes from bec_widgets.utils.plugin_utils import get_custom_classes
clss = get_rpc_classes("bec_widgets") clss = get_custom_classes("bec_widgets")
self._widget_classes = {cls.__name__: cls for cls in clss.top_level_classes} self._widget_classes = {cls.__name__: cls for cls in clss.widgets}
def create_widget(self, widget_type, **kwargs) -> BECConnector: def create_widget(self, widget_type, **kwargs) -> BECConnector:
""" """

View File

@ -53,7 +53,7 @@ class BECClassInfo:
obj: type obj: type
is_connector: bool = False is_connector: bool = False
is_widget: bool = False is_widget: bool = False
is_top_level: bool = False is_plugin: bool = False
class BECClassContainer: class BECClassContainer:
@ -88,14 +88,14 @@ class BECClassContainer:
""" """
Get all top-level classes. Get all top-level classes.
""" """
return [info.obj for info in self.collection if info.is_top_level] return [info.obj for info in self.collection if info.is_plugin]
@property @property
def plugins(self): def plugins(self):
""" """
Get all plugins. These are all classes that are on the top level and are widgets. Get all plugins. These are all classes that are on the top level and are widgets.
""" """
return [info.obj for info in self.collection if info.is_widget and info.is_top_level] return [info.obj for info in self.collection if info.is_widget and info.is_plugin]
@property @property
def widgets(self): def widgets(self):
@ -109,10 +109,17 @@ class BECClassContainer:
""" """
Get all top-level classes that are RPC-enabled. These are all classes that users can choose from. Get all top-level classes that are RPC-enabled. These are all classes that users can choose from.
""" """
return [info.obj for info in self.collection if info.is_top_level and info.is_connector] return [info.obj for info in self.collection if info.is_plugin and info.is_connector]
@property
def classes(self):
"""
Get all classes.
"""
return [info.obj for info in self.collection]
def get_rpc_classes(repo_name: str) -> BECClassContainer: def get_custom_classes(repo_name: str) -> BECClassContainer:
""" """
Get all RPC-enabled classes in the specified repository. Get all RPC-enabled classes in the specified repository.
@ -149,10 +156,8 @@ def get_rpc_classes(repo_name: str) -> BECClassContainer:
class_info.is_connector = True class_info.is_connector = True
if issubclass(obj, BECWidget): if issubclass(obj, BECWidget):
class_info.is_widget = True class_info.is_widget = True
if len(subs) == 1 and ( if hasattr(obj, "PLUGIN") and obj.PLUGIN:
issubclass(obj, QWidget) or issubclass(obj, QGraphicsWidget) class_info.is_plugin = True
):
class_info.is_top_level = True
collection.add_class(class_info) collection.add_class(class_info)
return collection return collection

View File

@ -4,7 +4,7 @@ from qtpy import PYQT6, PYSIDE6, QT_VERSION
from qtpy.QtCore import QFile, QIODevice from qtpy.QtCore import QFile, QIODevice
from bec_widgets.utils.generate_designer_plugin import DesignerPluginInfo from bec_widgets.utils.generate_designer_plugin import DesignerPluginInfo
from bec_widgets.utils.plugin_utils import get_rpc_classes from bec_widgets.utils.plugin_utils import get_custom_classes
if PYSIDE6: if PYSIDE6:
from PySide6.QtUiTools import QUiLoader from PySide6.QtUiTools import QUiLoader
@ -30,7 +30,7 @@ class UILoader:
def __init__(self, parent=None): def __init__(self, parent=None):
self.parent = parent self.parent = parent
widgets = get_rpc_classes("bec_widgets").top_level_classes widgets = get_custom_classes("bec_widgets").classes
self.custom_widgets = {widget.__name__: widget for widget in widgets} self.custom_widgets = {widget.__name__: widget for widget in widgets}

View File

@ -43,7 +43,7 @@ def test_client_generator_with_black_formatting():
obj=MockBECWaveform1D, obj=MockBECWaveform1D,
is_connector=True, is_connector=True,
is_widget=True, is_widget=True,
is_top_level=False, is_plugin=False,
) )
) )
container.add_class( container.add_class(
@ -54,7 +54,7 @@ def test_client_generator_with_black_formatting():
obj=MockBECFigure, obj=MockBECFigure,
is_connector=True, is_connector=True,
is_widget=True, is_widget=True,
is_top_level=True, is_plugin=True,
) )
) )

View File

@ -1,8 +1,8 @@
from bec_widgets.utils.plugin_utils import get_rpc_classes from bec_widgets.utils.plugin_utils import get_custom_classes
def test_client_generator_classes(): def test_client_generator_classes():
out = get_rpc_classes("bec_widgets") out = get_custom_classes("bec_widgets")
connector_cls_names = [cls.__name__ for cls in out.connector_classes] connector_cls_names = [cls.__name__ for cls in out.connector_classes]
top_level_cls_names = [cls.__name__ for cls in out.top_level_classes] top_level_cls_names = [cls.__name__ for cls in out.top_level_classes]