1
0
Fork 0
mirror of synced 2024-06-15 13:51:08 -04:00
dotbot/dotbot/dispatcher.py
Andreas Schmidt e07ed9374a add plugins module to handle plugins in configuration file
Signed-off-by: Andreas Schmidt <mail@schmidt-andreas.de>
2018-12-08 13:57:55 +01:00

53 lines
2 KiB
Python

import os
from .plugin import Plugin
from .messenger import Messenger
from .context import Context
class Dispatcher(object):
def __init__(self, base_directory):
self._log = Messenger()
self._setup_context(base_directory)
self._load_plugins()
def _setup_context(self, base_directory):
path = os.path.abspath(os.path.realpath(
os.path.expanduser(base_directory)))
if not os.path.exists(path):
raise DispatchError('Nonexistent base directory')
self._context = Context(path)
def dispatch(self, tasks):
success = True
for task in tasks:
for action in task:
handled = False
if action == 'defaults':
self._context.set_defaults(task[action]) # replace, not update
handled = True
# keep going, let other plugins handle this if they want
for plugin in self._plugins:
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(
'An error was encountered while executing action %s' %
action)
self._log.debug(err)
if not handled:
success = False
self._log.error('Action %s not handled' % action)
return success
def _load_plugins(self):
self._plugins = [plugin(self._context)
for plugin in Plugin.__subclasses__()]
class DispatchError(Exception):
pass