Skip to content

Commit

Permalink
Merge branch 'feature/ghi-#5-dispatcher' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
arcticicestudio committed Jan 7, 2017
2 parents 695f1fd + 0ff4c80 commit 5bb0873
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions snowsaw/dispatcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os
from .plugin import Plugin
from .logging import Logger
from .context import Context


class Dispatcher(object):
"""
Dispatches tasks to loaded plugins.
"""
def __init__(self, snowblock_dir):
self._log = Logger()
self._setup_context(snowblock_dir)
self._load_plugins()

def _setup_context(self, snowblock_dir):
"""
Sets up the plugin context for the specified snowblock.
:param snowblock_dir: The directory of the snowblock
:return: The plugin context
"""
path = os.path.abspath(os.path.realpath(os.path.expanduser(snowblock_dir)))
if not os.path.exists(path):
raise DispatchError("Nonexistent snowblock directory")
self._context = Context(path)

def dispatch(self, tasks):
"""
Dispatches the specified tasks to the loaded plugins.
:param tasks: The tasks to dispatch
:return: True if all tasks have been handled by the loaded plugins successfully, False otherwise
"""
success = True
for task in tasks:
for action in task:
handled = False
if action == "defaults":
self._context.set_defaults(task[action])
handled = True
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 \"{}\"".format(action))
if not handled:
success = False
self._log.error("Action \"{}\" not handled".format(action))
return success

def _load_plugins(self):
"""
Loads all found plugins.
:return: None
"""
self._plugins = [plugin(self._context) for plugin in Plugin.__subclasses__()]


class DispatchError(Exception):
pass

0 comments on commit 5bb0873

Please sign in to comment.