← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~jugmac00/lpcraft:auto-collect-plugins into lpcraft:main

 

Jürgen Gmach has proposed merging ~jugmac00/lpcraft:auto-collect-plugins into lpcraft:main.

Commit message:
Use a decorator to collect all builtin plugins

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~jugmac00/lpcraft/+git/lpcraft/+merge/413411
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~jugmac00/lpcraft:auto-collect-plugins into lpcraft:main.
diff --git a/lpcraft/plugins/__init__.py b/lpcraft/plugins/__init__.py
index 58966af..0c44aa5 100644
--- a/lpcraft/plugins/__init__.py
+++ b/lpcraft/plugins/__init__.py
@@ -1,5 +1,23 @@
-from lpcraft.plugins.tox import ToxPlugin
+from typing import Any, Callable, Type, TypeVar
 
-# XXX jugmac00 2021-12-16: The plugin mapping should be autogenerated by a
-# decorator, e.g. @register_plugin(name="<name>")
-PLUGINS = {"tox": ToxPlugin}
+PLUGINS = dict()
+
+
+TypeT = TypeVar("TypeT", bound=Type[Any])
+
+
+def register(name: str) -> Callable[[TypeT], TypeT]:
+    # this function registers all decorated plugin classes
+    # the result looks like:
+    #
+    # PLUGINS = {'tox': <class 'lpcraft.plugins.plugins.ToxPlugin'>}
+    def inner(cls: TypeT) -> TypeT:
+        PLUGINS[name] = cls
+        return cls
+
+    return inner
+
+
+# for registration all modules which contain plugins need to be imported
+# the imports must be at the bottom of the module to avoid circular imports
+from lpcraft.plugins import plugins  # noqa: F401, E402
diff --git a/lpcraft/plugins/collector.py b/lpcraft/plugins/collector.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/lpcraft/plugins/collector.py
diff --git a/lpcraft/plugins/tox.py b/lpcraft/plugins/plugins.py
similarity index 95%
rename from lpcraft/plugins/tox.py
rename to lpcraft/plugins/plugins.py
index 00ba66c..9aa7a4f 100644
--- a/lpcraft/plugins/tox.py
+++ b/lpcraft/plugins/plugins.py
@@ -5,8 +5,10 @@ from __future__ import annotations
 
 from lpcraft.config import Job
 from lpcraft.plugin import hookimpl
+from lpcraft.plugins import register
 
 
+@register(name="tox")
 class ToxPlugin:
     # XXX jugmac00 2021-12-16: this plugin is not yet fully implemented
     def __init__(self, config: Job) -> None: