mirror of
https://github.com/bec-project/bec_widgets.git
synced 2025-07-13 19:21:50 +02:00
fix: make generate plugin robust to multiline init
instead of str.find, use multiline regex with whitespace
This commit is contained in:
@ -8,6 +8,9 @@ from qtpy.QtCore import QObject
|
|||||||
from bec_widgets.utils.name_utils import pascal_to_snake
|
from bec_widgets.utils.name_utils import pascal_to_snake
|
||||||
|
|
||||||
EXCLUDED_PLUGINS = ["BECConnector", "BECDockArea", "BECDock", "BECFigure"]
|
EXCLUDED_PLUGINS = ["BECConnector", "BECDockArea", "BECDock", "BECFigure"]
|
||||||
|
_PARENT_ARG_REGEX = r".__init__\(\s*(?:parent\)|parent=parent,?|parent,?)"
|
||||||
|
_SELF_PARENT_ARG_REGEX = r".__init__\(\s*self,\s*(?:parent\)|parent=parent,?|parent,?)"
|
||||||
|
SUPER_INIT_REGEX = re.compile(r"super\(\)" + _PARENT_ARG_REGEX, re.MULTILINE)
|
||||||
|
|
||||||
|
|
||||||
class PluginFilenames(NamedTuple):
|
class PluginFilenames(NamedTuple):
|
||||||
@ -90,34 +93,20 @@ class DesignerPluginGenerator:
|
|||||||
|
|
||||||
# Check if the widget class calls the super constructor with parent argument
|
# Check if the widget class calls the super constructor with parent argument
|
||||||
init_source = inspect.getsource(self.widget.__init__)
|
init_source = inspect.getsource(self.widget.__init__)
|
||||||
cls_init_found = (
|
class_re = re.compile(base_cls[0].__name__ + _SELF_PARENT_ARG_REGEX, re.MULTILINE)
|
||||||
bool(init_source.find(f"{base_cls[0].__name__}.__init__(self, parent=parent") > 0)
|
cls_init_found = class_re.search(init_source) is not None
|
||||||
or bool(init_source.find(f"{base_cls[0].__name__}.__init__(self, parent)") > 0)
|
super_self_re = re.compile(
|
||||||
or bool(init_source.find(f"{base_cls[0].__name__}.__init__(self, parent,") > 0)
|
rf"super\({base_cls[0].__name__}, self\)" + _PARENT_ARG_REGEX, re.MULTILINE
|
||||||
)
|
|
||||||
super_init_found = (
|
|
||||||
bool(
|
|
||||||
init_source.find(f"super({base_cls[0].__name__}, self).__init__(parent=parent") > 0
|
|
||||||
)
|
|
||||||
or bool(init_source.find(f"super({base_cls[0].__name__}, self).__init__(parent,") > 0)
|
|
||||||
or bool(init_source.find(f"super({base_cls[0].__name__}, self).__init__(parent)") > 0)
|
|
||||||
)
|
)
|
||||||
|
super_init_found = super_self_re.search(init_source) is not None
|
||||||
if issubclass(self.widget.__bases__[0], QObject) and not super_init_found:
|
if issubclass(self.widget.__bases__[0], QObject) and not super_init_found:
|
||||||
super_init_found = (
|
super_init_found = SUPER_INIT_REGEX.search(init_source) is not None
|
||||||
bool(init_source.find("super().__init__(parent=parent") > 0)
|
|
||||||
or bool(init_source.find("super().__init__(parent,") > 0)
|
|
||||||
or bool(init_source.find("super().__init__(parent)") > 0)
|
|
||||||
)
|
|
||||||
|
|
||||||
# for the new style classes, we only have one super call. We can therefore check if the
|
# for the new style classes, we only have one super call. We can therefore check if the
|
||||||
# number of __init__ calls is 2 (the class itself and the super class)
|
# number of __init__ calls is 2 (the class itself and the super class)
|
||||||
num_inits = re.findall(r"__init__", init_source)
|
num_inits = re.findall(r"__init__", init_source)
|
||||||
if len(num_inits) == 2 and not super_init_found:
|
if len(num_inits) == 2 and not super_init_found:
|
||||||
super_init_found = bool(
|
super_init_found = SUPER_INIT_REGEX.search(init_source) is not None
|
||||||
init_source.find("super().__init__(parent=parent") > 0
|
|
||||||
or init_source.find("super().__init__(parent,") > 0
|
|
||||||
or init_source.find("super().__init__(parent)") > 0
|
|
||||||
)
|
|
||||||
|
|
||||||
if not cls_init_found and not super_init_found:
|
if not cls_init_found and not super_init_found:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
|
@ -48,12 +48,22 @@ class MyWidget(QWidget):
|
|||||||
from qtpy.QtWidgets import QWidget
|
from qtpy.QtWidgets import QWidget
|
||||||
class MyWidget(QWidget):
|
class MyWidget(QWidget):
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super(QWidget, self).__init__(parent)"""
|
super(QWidget, self).__init__(parent)
|
||||||
|
""",
|
||||||
"""
|
"""
|
||||||
from qtpy.QtWidgets import QWidget
|
from qtpy.QtWidgets import QWidget
|
||||||
class MyWidget(QWidget):
|
class MyWidget(QWidget):
|
||||||
def __init__(self, parent=None):
|
def __init__(self, parent=None):
|
||||||
super(QWidget, self).__init__(parent=parent)
|
super(QWidget, self).__init__(parent=parent)
|
||||||
|
""",
|
||||||
|
"""
|
||||||
|
from qtpy.QtWidgets import QWidget
|
||||||
|
class MyWidget(QWidget):
|
||||||
|
def __init__(self, parent=None):
|
||||||
|
super(QWidget, self).__init__(
|
||||||
|
parent=parent,
|
||||||
|
other=arguments,
|
||||||
|
)
|
||||||
""",
|
""",
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user