refactor for simplicity
This commit is contained in:
parent
1daf98c919
commit
328d297ed8
@ -25,42 +25,38 @@ class PluginLoader():
|
|||||||
def __init__(self, api) -> None:
|
def __init__(self, api) -> None:
|
||||||
self.api = api
|
self.api = api
|
||||||
self.plugins = []
|
self.plugins = []
|
||||||
|
self.plugins_dir = os.path.join(os.path.dirname(__file__), 'plugins')
|
||||||
|
self._prefix = 'rsbbs.plugins'
|
||||||
|
self._identifier = 'plugin'
|
||||||
|
|
||||||
def load_plugins(self) -> None:
|
@property
|
||||||
# Path to command plugin directory
|
def _plugin_dirs(self) -> list:
|
||||||
plugins_dir = os.path.join(os.path.dirname(__file__), 'plugins')
|
|
||||||
|
|
||||||
# Discover all subdirectories in the plugins directory
|
# Discover all subdirectories in the plugins directory
|
||||||
plugin_dirs = [d
|
plugin_dirs = [d
|
||||||
for d in os.listdir(plugins_dir)
|
for d in os.listdir(self.plugins_dir)
|
||||||
if os.path.isdir(os.path.join(plugins_dir, d))
|
if os.path.isdir(os.path.join(self.plugins_dir, d))
|
||||||
and not d.startswith('__')]
|
and not d.startswith('__')]
|
||||||
|
return plugin_dirs
|
||||||
|
|
||||||
if self.api.config.debug:
|
def load_plugin(self, plugin_dir) -> None:
|
||||||
print(f"Plugin dirs: {plugin_dirs}")
|
try:
|
||||||
|
# Import the module containing the plugin class
|
||||||
|
plugin_module = importlib.import_module(
|
||||||
|
f"{self._prefix}.{plugin_dir}.{self._identifier}")
|
||||||
|
|
||||||
|
# Get a reference to the plugin class
|
||||||
|
plugin_class = plugin_module.Plugin
|
||||||
|
|
||||||
|
# Initialize an instance of the plugin class, passing api as an
|
||||||
|
# argument
|
||||||
|
plugin = plugin_class(self.api)
|
||||||
|
|
||||||
|
# Add the loaded plugin to the list of plugins
|
||||||
|
self.plugins.append(plugin)
|
||||||
|
except Exception as e:
|
||||||
|
raise
|
||||||
|
|
||||||
|
def load_plugins(self) -> None:
|
||||||
# Loop over each plugin directory
|
# Loop over each plugin directory
|
||||||
for plugin_dir in plugin_dirs:
|
for plugin_dir in self._plugin_dirs:
|
||||||
try:
|
self.load_plugin(plugin_dir)
|
||||||
# Import the module containing the plugin class
|
|
||||||
if self.api.config.debug:
|
|
||||||
print(f"Import rsbbs.plugins.{plugin_dir}.plugin")
|
|
||||||
|
|
||||||
plugin_module = importlib.import_module(
|
|
||||||
f"rsbbs.plugins.{plugin_dir}.plugin")
|
|
||||||
|
|
||||||
# Get a reference to the plugin class
|
|
||||||
plugin_class = plugin_module.Plugin
|
|
||||||
|
|
||||||
# Initialize an instance of the plugin class, passing api as an
|
|
||||||
# argument
|
|
||||||
plugin = plugin_class(self.api)
|
|
||||||
|
|
||||||
# Add the loaded plugin to the list of plugins
|
|
||||||
self.plugins.append(plugin)
|
|
||||||
except Exception as e:
|
|
||||||
if self.api.config.debug:
|
|
||||||
print(f"{e}")
|
|
||||||
raise
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user