Merge ~jugmac00/lpci:auto-collect-plugins into lpci:main

Proposed by Jürgen Gmach
Status: Merged
Merged at revision: f907f2f9bbfa07b33d7d7d728965d88640350ecc
Proposed branch: ~jugmac00/lpci:auto-collect-plugins
Merge into: lpci:main
Diff against target: 50 lines (+24/-4)
2 files modified
lpcraft/plugins/__init__.py (+22/-4)
lpcraft/plugins/plugins.py (+2/-0)
Reviewer Review Type Date Requested Status
Colin Watson (community) Approve
Review via email: mp+413411@code.launchpad.net

Commit message

Use a decorator to collect all builtin plugins

To post a comment you must log in.
Revision history for this message
Colin Watson (cjwatson) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lpcraft/plugins/__init__.py b/lpcraft/plugins/__init__.py
2index 58966af..0c44aa5 100644
3--- a/lpcraft/plugins/__init__.py
4+++ b/lpcraft/plugins/__init__.py
5@@ -1,5 +1,23 @@
6-from lpcraft.plugins.tox import ToxPlugin
7+from typing import Any, Callable, Type, TypeVar
8
9-# XXX jugmac00 2021-12-16: The plugin mapping should be autogenerated by a
10-# decorator, e.g. @register_plugin(name="<name>")
11-PLUGINS = {"tox": ToxPlugin}
12+PLUGINS = dict()
13+
14+
15+TypeT = TypeVar("TypeT", bound=Type[Any])
16+
17+
18+def register(name: str) -> Callable[[TypeT], TypeT]:
19+ # this function registers all decorated plugin classes
20+ # the result looks like:
21+ #
22+ # PLUGINS = {'tox': <class 'lpcraft.plugins.plugins.ToxPlugin'>}
23+ def inner(cls: TypeT) -> TypeT:
24+ PLUGINS[name] = cls
25+ return cls
26+
27+ return inner
28+
29+
30+# for registration all modules which contain plugins need to be imported
31+# the imports must be at the bottom of the module to avoid circular imports
32+from lpcraft.plugins import plugins # noqa: F401, E402
33diff --git a/lpcraft/plugins/tox.py b/lpcraft/plugins/plugins.py
34similarity index 95%
35rename from lpcraft/plugins/tox.py
36rename to lpcraft/plugins/plugins.py
37index 00ba66c..9aa7a4f 100644
38--- a/lpcraft/plugins/tox.py
39+++ b/lpcraft/plugins/plugins.py
40@@ -5,8 +5,10 @@ from __future__ import annotations
41
42 from lpcraft.config import Job
43 from lpcraft.plugin import hookimpl
44+from lpcraft.plugins import register
45
46
47+@register(name="tox")
48 class ToxPlugin:
49 # XXX jugmac00 2021-12-16: this plugin is not yet fully implemented
50 def __init__(self, config: Job) -> None:

Subscribers

People subscribed via source and target branches