diff options
author | Anish Athalye <aathalye@me.com> | 2014-03-19 23:07:30 -0400 |
---|---|---|
committer | Anish Athalye <aathalye@me.com> | 2014-03-20 18:57:56 -0400 |
commit | 60a560e97699a1d9a4320b8e787a50b1a9a7734d (patch) | |
tree | 573ce63c2a49a278af49c0cc6542bfe3b89cf572 /dotbot/dispatcher.py |
Initial commit
Diffstat (limited to 'dotbot/dispatcher.py')
-rw-r--r-- | dotbot/dispatcher.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/dotbot/dispatcher.py b/dotbot/dispatcher.py new file mode 100644 index 0000000..35b0889 --- /dev/null +++ b/dotbot/dispatcher.py @@ -0,0 +1,46 @@ +import os +from .executor import * +from .messenger import Messenger + +class Dispatcher(object): + PLUGIN_CLASS = Executor + PLUGIN_DIR = 'dotbot/executor' + + def __init__(self, base_directory): + self._log = Messenger() + self._set_base_directory(base_directory) + self._load_plugins() + + def _set_base_directory(self, base_directory): + path = os.path.abspath(os.path.realpath( + os.path.expanduser(base_directory))) + if os.path.exists(path): + self._base_directory = path + else: + raise DispatchError('Nonexistant base directory') + + def dispatch(self, tasks): + success = True + for task in tasks: + for action in task: + handled = False + for plugin in self._plugins: + if plugin.can_handle(action): + try: + success &= plugin.handle(action, task[action]) + handled = True + except Exception: + self._log.error( + 'An error was encountered while executing action %s' % + action) + if not handled: + success = False + self._log.error('Action %s not handled' % action) + return success + + def _load_plugins(self): + self._plugins = [plugin(self._base_directory) + for plugin in Executor.__subclasses__()] + +class DispatchError(Exception): + pass |