Skip to content

util

General utitilies with relevance for plugins.

check_implements_method

check_implements_method(name: str, plugin, base_method)

Check whether plugin overrides a method of its superclass.

Source code in src/metador_core/plugin/util.py
22
23
24
25
26
def check_implements_method(name: str, plugin, base_method):
    """Check whether plugin overrides a method of its superclass."""
    if not implements_method(plugin, base_method):
        msg = f"{name}: {plugin} does not implement {base_method.__name__}!"
        raise TypeError(msg)

check_is_subclass

check_is_subclass(name: str, plugin, base)

Check whether plugin has expected parent class (helper method).

Source code in src/metador_core/plugin/util.py
29
30
31
32
33
def check_is_subclass(name: str, plugin, base):
    """Check whether plugin has expected parent class (helper method)."""
    if not issubclass(plugin, base):
        msg = f"{name}: {plugin} is not subclass of {base}!"
        raise TypeError(msg)

register_in_group

register_in_group(
    pgroup: PluginGroup,
    plugin: Optional[Type[T]] = None,
    *,
    violently: bool = False
)

Register and load a plugin manually, without defining an entry point.

Source code in src/metador_core/plugin/util.py
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
def register_in_group(
    pgroup: PluginGroup,
    plugin: Optional[Type[T]] = None,
    *,
    violently: bool = False,
):
    """Register and load a plugin manually, without defining an entry point."""
    if not violently and not is_notebook():
        raise RuntimeError("This is not supposed to be used outside of notebooks!")

    def manual_register(plugin: Type[T]) -> Type[T]:
        pginfo = plugin.Plugin
        ep_name = to_ep_name(pginfo.name, pginfo.version)
        pg_ref = pgroup.PluginRef(name=pginfo.name, version=pginfo.version)

        pgroup._ENTRY_POINTS[ep_name] = None
        pgroup._LOADED_PLUGINS[pg_ref] = plugin
        if pg_ref.name not in pgroup._VERSIONS:
            pgroup._VERSIONS[pg_ref.name] = []
        pgroup._VERSIONS[pg_ref.name].append(pg_ref)

        pgroup._load_plugin(ep_name, plugin)
        if not violently:
            eprint(
                f"Notebook: Plugin '{pginfo.name}' registered in '{pgroup.name}' group!"
            )  # pragma: no cover
        return plugin

    if not plugin:
        return manual_register  # used as decorator
    else:
        if not is_pluginlike(plugin, check_group=False):
            raise RuntimeError("This class has no inner Plugin class!")

        manual_register(plugin)  # used as normal function