changeset 3370:2157880ba3b4

core: plugins can now be loaded from a directory: A plugin can now be a directory with a name starting by `plugin_`. It will then be loaded as a module. The `__init__.py` file must contain the `PLUGIN_INFO` dict with metadata.
author Goffi <goffi@goffi.org>
date Sun, 20 Sep 2020 14:05:40 +0200
parents 96b2f84a685c
children e8d74ac7c479
files sat/core/sat_main.py
diffstat 1 files changed, 22 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/sat/core/sat_main.py	Sun Sep 20 14:05:40 2020 +0200
+++ b/sat/core/sat_main.py	Sun Sep 20 14:05:40 2020 +0200
@@ -16,7 +16,6 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from glob import glob
 import sys
 import os.path
 import uuid
@@ -273,17 +272,29 @@
         # TODO: do not import all plugins if no needed: component plugins are not needed
         #       if we just use a client, and plugin blacklisting should be possible in
         #       sat.conf
-        plugins_path = os.path.dirname(sat.plugins.__file__)
-        plugin_glob = "plugin*." + C.PLUGIN_EXT
-        plug_lst = [
-            os.path.splitext(plugin)[0]
-            for plugin in map(
-                os.path.basename, glob(os.path.join(plugins_path, plugin_glob))
-            )
-        ]
+        plugins_path = Path(sat.plugins.__file__).parent
         plugins_to_import = {}  # plugins we still have to import
-        for plug in plug_lst:
-            plugin_path = "sat.plugins." + plug
+        for plug_path in plugins_path.glob("plugin_*"):
+            if plug_path.is_dir():
+                init_path = plug_path / f"__init__.{C.PLUGIN_EXT}"
+                if not init_path.exists():
+                    log.warning(
+                        f"{plug_path} doesn't appear to be a package, can't load it")
+                    continue
+                plug_name = plug_path.name
+            elif plug_path.is_file():
+                if plug_path.suffix != f".{C.PLUGIN_EXT}":
+                    continue
+                plug_name = plug_path.stem
+            else:
+                log.warning(
+                    f"{plug_path} is not a file or a dir, ignoring it")
+                continue
+            if not plug_name.isidentifier():
+                log.warning(
+                    f"{plug_name!r} is not a valid name for a plugin, ignoring it")
+                continue
+            plugin_path = f"sat.plugins.{plug_name}"
             try:
                 __import__(plugin_path)
             except exceptions.MissingModule as e: