summaryrefslogtreecommitdiff
path: root/dotbot
diff options
context:
space:
mode:
authorAnish Athalye <me@anishathalye.com>2016-03-02 20:53:19 -0500
committerAnish Athalye <me@anishathalye.com>2016-03-03 09:42:50 -0500
commitf52bbd1eec0d6fb103c5fcbb75c03ca223fffdb7 (patch)
tree8578282f6ce6cdb02eab51a9bcd18c590f7a127a /dotbot
parentdaf8d82e02c2bcc476932790fa25076631c4c312 (diff)
Add default options
This feature was implemented with feedback from Aleks Kamko <aykamko@gmail.com> and Casey Rodarmor <casey@rodarmor.com>.
Diffstat (limited to 'dotbot')
-rw-r--r--dotbot/context.py23
-rw-r--r--dotbot/dispatcher.py16
-rw-r--r--dotbot/plugin.py5
3 files changed, 36 insertions, 8 deletions
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):