From e07ed9374a06da1c0f3ad08c961103458c7bcf10 Mon Sep 17 00:00:00 2001 From: Andreas Schmidt Date: Sat, 8 Dec 2018 13:44:59 +0100 Subject: [PATCH] add plugins module to handle plugins in configuration file Signed-off-by: Andreas Schmidt --- dotbot/cli.py | 2 +- dotbot/dispatcher.py | 4 ++++ dotbot/plugins/__init__.py | 1 + dotbot/plugins/plugins.py | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 dotbot/plugins/plugins.py diff --git a/dotbot/cli.py b/dotbot/cli.py index fdc2a13..85f2a3c 100644 --- a/dotbot/cli.py +++ b/dotbot/cli.py @@ -56,7 +56,7 @@ def main(): log.use_color(False) plugin_directories = list(options.plugin_dirs) if not options.disable_built_in_plugins: - from .plugins import Clean, Link, Shell + from .plugins import Clean, Link, Shell, Plugins plugin_paths = [] for directory in plugin_directories: for plugin_path in glob.glob(os.path.join(directory, '*.py')): diff --git a/dotbot/dispatcher.py b/dotbot/dispatcher.py index d1a4f95..68d0d88 100644 --- a/dotbot/dispatcher.py +++ b/dotbot/dispatcher.py @@ -29,6 +29,10 @@ class Dispatcher(object): if plugin.can_handle(action): try: success &= plugin.handle(action, task[action]) + if action == 'plugins': + self._load_plugins() + self._log.lowinfo('Plugin list have been update') + self._log.info('New plugin list: %s' % [type(plugin).__name__ for plugin in self._plugins]) handled = True except Exception as err: self._log.error( diff --git a/dotbot/plugins/__init__.py b/dotbot/plugins/__init__.py index 93bd981..2d3e204 100644 --- a/dotbot/plugins/__init__.py +++ b/dotbot/plugins/__init__.py @@ -1,3 +1,4 @@ from .clean import Clean from .link import Link from .shell import Shell +from .plugins import Plugins diff --git a/dotbot/plugins/plugins.py b/dotbot/plugins/plugins.py new file mode 100644 index 0000000..fa2c896 --- /dev/null +++ b/dotbot/plugins/plugins.py @@ -0,0 +1,37 @@ +import os +import glob +import dotbot +from ..util import module + +class Plugins(dotbot.Plugin): + ''' + Plugins configuration + ''' + + _directive = 'plugins' + + def can_handle(self, directive): + return directive == self._directive + + def handle(self, directive, data): + if directive != self._directive: + raise ValueError('Plugins cannot handle directive %s' % directive) + return self._process_plugins(data) + + def _process_plugins(self, plugins): + plugin_dirs = [] + plugin_paths = [] + for plugin in plugins: + if isinstance(plugin, dict): + plugin_dirs.append(plugin.get('dir', '')) + else: + plugin_paths.append(plugin) + for directory in plugin_dirs: + for plugin_path in glob.glob(os.path.join(directory, '*.py')): + plugin_paths.append(plugin_path) + for plugin in plugin_paths: + abspath = os.path.abspath(plugin) + module.load(abspath) + return True + +# vim: ts=4 sw=4 et