From 6a7f5326da004d7a0d78fe5a6d91f9630d832f51 Mon Sep 17 00:00:00 2001 From: mathieui Date: Fri, 27 Dec 2019 16:58:20 +0100 Subject: Plugins: use import_module for module in poezio_plugins And do not rely on the "module name" to index the plugins, use the intended plugin name instead. --- poezio/plugin.py | 11 ++++++----- poezio/plugin_manager.py | 30 ++++++++++++++---------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/poezio/plugin.py b/poezio/plugin.py index 1995d7cd..c3d10b38 100644 --- a/poezio/plugin.py +++ b/poezio/plugin.py @@ -395,17 +395,18 @@ class BasePlugin(object, metaclass=SafetyMetaclass): default_config = None - def __init__(self, plugin_api, core, plugins_conf_dir): + def __init__(self, name, plugin_api, core, plugins_conf_dir): + self.__name = name self.core = core # More hack; luckily we'll never have more than one core object SafetyMetaclass.core = core - conf = plugins_conf_dir / (self.__module__ + '.cfg') + conf = plugins_conf_dir / (self.__name + '.cfg') try: self.config = PluginConfig( - conf, self.__module__, default=self.default_config) + conf, self.__name, default=self.default_config) except Exception: log.debug('Error while creating the plugin config', exc_info=True) - self.config = PluginConfig(conf, self.__module__) + self.config = PluginConfig(conf, self.__name) self._api = plugin_api[self.name] self.init() @@ -414,7 +415,7 @@ class BasePlugin(object, metaclass=SafetyMetaclass): """ Get the name (module name) of the plugin. """ - return self.__module__ + return self.__name @property def api(self): diff --git a/poezio/plugin_manager.py b/poezio/plugin_manager.py index 8275e6f9..2b7b7374 100644 --- a/poezio/plugin_manager.py +++ b/poezio/plugin_manager.py @@ -5,10 +5,11 @@ the API together. Defines also a bunch of variables related to the plugin env. """ +import logging import os -from os import path +from importlib import import_module, machinery from pathlib import Path -import logging +from os import path from poezio import tabs, xdg from poezio.core.structs import Command, Completion @@ -44,7 +45,6 @@ class PluginManager: self.tab_keys = {} self.roster_elements = {} - from importlib import machinery self.finder = machinery.PathFinder() self.initial_set_plugins_dir() @@ -57,7 +57,7 @@ class PluginManager: for plugin in set(self.plugins.keys()): self.unload(plugin, notify=False) - def load(self, name, notify=True): + def load(self, name: str, notify=True): """ Load a plugin. """ @@ -67,11 +67,17 @@ class PluginManager: try: module = None loader = self.finder.find_module(name, self.load_path) - if not loader: + if loader: + module = loader.load_module() + else: + try: + module = import_module('poezio_plugins.%s' % name) + except ModuleNotFoundError: + pass + if not module: self.core.information('Could not find plugin: %s' % name, 'Error') return - module = loader.load_module() except Exception as e: log.debug("Could not load plugin %s", name, exc_info=True) self.core.information("Could not load plugin %s: %s" % (name, e), @@ -88,7 +94,7 @@ class PluginManager: self.event_handlers[name] = [] try: self.plugins[name] = None - self.plugins[name] = module.Plugin(self.plugin_api, self.core, + self.plugins[name] = module.Plugin(name, self.plugin_api, self.core, self.plugins_conf_dir) except Exception as e: log.error('Error while loading the plugin %s', name, exc_info=True) @@ -100,7 +106,7 @@ class PluginManager: if notify: self.core.information('Plugin %s loaded' % name, 'Info') - def unload(self, name, notify=True): + def unload(self, name: str, notify=True): if name in self.plugins: try: for command in self.commands[name].keys(): @@ -387,11 +393,3 @@ class PluginManager: if os.access(str(self.plugins_dir), os.R_OK | os.X_OK): self.load_path.append(str(self.plugins_dir)) - - try: - import poezio_plugins - except: - pass - else: - if poezio_plugins.__path__: - self.load_path.append(list(poezio_plugins.__path__)[0]) -- cgit v1.2.3