Commit d7d4e30e authored by mathieui's avatar mathieui

Merge branch 'fix-plugin-names' into 'master'

Plugins: use import_module for module in poezio_plugins

See merge request !52
parents 12a9528d 6a7f5326
......@@ -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):
......
......@@ -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])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment