From f52bbd1eec0d6fb103c5fcbb75c03ca223fffdb7 Mon Sep 17 00:00:00 2001 From: Anish Athalye Date: Wed, 2 Mar 2016 20:53:19 -0500 Subject: Add default options This feature was implemented with feedback from Aleks Kamko and Casey Rodarmor . --- dotbot/context.py | 23 +++++++++++++++++++++++ dotbot/dispatcher.py | 16 ++++++++++------ dotbot/plugin.py | 5 +++-- 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 dotbot/context.py (limited to 'dotbot') diff --git a/dotbot/context.py b/dotbot/context.py new file mode 100644 index 0000000..b2dbd6c --- /dev/null +++ b/dotbot/context.py @@ -0,0 +1,23 @@ +import copy + +class Context(object): + ''' + Contextual data and information for plugins. + ''' + + def __init__(self, base_directory): + self._base_directory = base_directory + self._defaults = {} + pass + + def set_base_directory(self, base_directory): + self._base_directory = base_directory + + def base_directory(self): + return self._base_directory + + def set_defaults(self, defaults): + self._defaults = defaults + + def defaults(self): + return copy.deepcopy(self._defaults) diff --git a/dotbot/dispatcher.py b/dotbot/dispatcher.py index 79231a0..cc07435 100644 --- a/dotbot/dispatcher.py +++ b/dotbot/dispatcher.py @@ -1,26 +1,30 @@ 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._set_base_directory(base_directory) + self._setup_context(base_directory) self._load_plugins() - def _set_base_directory(self, base_directory): + def _setup_context(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: + 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: @@ -36,7 +40,7 @@ class Dispatcher(object): return success def _load_plugins(self): - self._plugins = [plugin(self._base_directory) + self._plugins = [plugin(self._context) for plugin in Plugin.__subclasses__()] class DispatchError(Exception): diff --git a/dotbot/plugin.py b/dotbot/plugin.py index a79639e..56d4da8 100644 --- a/dotbot/plugin.py +++ b/dotbot/plugin.py @@ -1,12 +1,13 @@ from .messenger import Messenger +from .context import Context class Plugin(object): ''' Abstract base class for commands that process directives. ''' - def __init__(self, base_directory): - self._base_directory = base_directory + def __init__(self, context): + self._context = context self._log = Messenger() def can_handle(self, directive): -- cgit v1.2.3