From 74637f0ca06b2921dec90cf310fb60b866f96f9e Mon Sep 17 00:00:00 2001 From: Aleks Kamko Date: Fri, 26 Feb 2016 21:43:11 -0800 Subject: [PATCH 1/2] Add default options --- dotbot/dispatcher.py | 11 ++++++++--- dotbot/plugin.py | 2 +- plugins/clean.py | 2 +- plugins/link.py | 14 +++++++------- plugins/shell.py | 17 ++++++++--------- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/dotbot/dispatcher.py b/dotbot/dispatcher.py index 79231a0..f61c802 100644 --- a/dotbot/dispatcher.py +++ b/dotbot/dispatcher.py @@ -5,6 +5,7 @@ from .messenger import Messenger class Dispatcher(object): def __init__(self, base_directory): self._log = Messenger() + self._defaults = {} self._set_base_directory(base_directory) self._load_plugins() @@ -20,11 +21,16 @@ class Dispatcher(object): success = True for task in tasks: for action in task: + if action == 'defaults': + self._defaults = task[action] + self._log.info('Set defaults') + continue handled = False for plugin in self._plugins: if plugin.can_handle(action): try: - success &= plugin.handle(action, task[action]) + success &= plugin.handle(action, task[action], + self._defaults.get(action, {})) handled = True except Exception: self._log.error( @@ -36,8 +42,7 @@ class Dispatcher(object): return success def _load_plugins(self): - self._plugins = [plugin(self._base_directory) - for plugin in Plugin.__subclasses__()] + self._plugins = [plugin(self._base_directory) for plugin in Plugin.__subclasses__()] class DispatchError(Exception): pass diff --git a/dotbot/plugin.py b/dotbot/plugin.py index a79639e..47951ce 100644 --- a/dotbot/plugin.py +++ b/dotbot/plugin.py @@ -15,7 +15,7 @@ class Plugin(object): ''' raise NotImplementedError - def handle(self, directive, data): + def handle(self, directive, data, defaults): ''' Executes the directive. diff --git a/plugins/clean.py b/plugins/clean.py index 22ec450..ac1f95f 100644 --- a/plugins/clean.py +++ b/plugins/clean.py @@ -10,7 +10,7 @@ class Clean(dotbot.Plugin): def can_handle(self, directive): return directive == self._directive - def handle(self, directive, data): + def handle(self, directive, data, defaults): if directive != self._directive: raise ValueError('Clean cannot handle directive %s' % directive) return self._process_clean(data) diff --git a/plugins/link.py b/plugins/link.py index 3bb5686..1ebea6f 100644 --- a/plugins/link.py +++ b/plugins/link.py @@ -10,12 +10,12 @@ class Link(dotbot.Plugin): def can_handle(self, directive): return directive == self._directive - def handle(self, directive, data): + def handle(self, directive, data, defaults): if directive != self._directive: raise ValueError('Link cannot handle directive %s' % directive) - return self._process_links(data) + return self._process_links(data, defaults) - def _process_links(self, links): + def _process_links(self, links, defaults): success = True for destination, source in links.items(): source = os.path.expandvars(source) @@ -23,10 +23,10 @@ class Link(dotbot.Plugin): if isinstance(source, dict): # extended config path = source['path'] - relative = source.get('relative', False) - force = source.get('force', False) - relink = source.get('relink', False) - create = source.get('create', False) + relative = source.get('relative', defaults.get('relative', False)) + force = source.get('force', defaults.get('force', False)) + relink = source.get('relink', defaults.get('relink', False)) + create = source.get('create', defaults.get('create', False)) if create: success &= self._create(destination) if force: diff --git a/plugins/shell.py b/plugins/shell.py index a2d9c1a..d1000cb 100644 --- a/plugins/shell.py +++ b/plugins/shell.py @@ -10,13 +10,12 @@ class Shell(dotbot.Plugin): def can_handle(self, directive): return directive == self._directive - def handle(self, directive, data): + def handle(self, directive, data, defaults): if directive != self._directive: - raise ValueError('Shell cannot handle directive %s' % - directive) - return self._process_commands(data) + raise ValueError('Shell cannot handle directive %s' % directive) + return self._process_commands(data, defaults) - def _process_commands(self, data): + def _process_commands(self, data, defaults): success = True with open(os.devnull, 'w') as devnull: for item in data: @@ -24,11 +23,11 @@ class Shell(dotbot.Plugin): if isinstance(item, dict): cmd = item['command'] msg = item.get('description', None) - if item.get('stdin', False) is True: + if item.get('stdin', defaults.get('stdin', False)) is True: stdin = None - if item.get('stdout', False) is True: + if item.get('stdout', defaults.get('stdout', False)) is True: stdout = None - if item.get('stderr', False) is True: + if item.get('stderr', defaults.get('stderr', False)) is True: stderr = None elif isinstance(item, list): cmd = item[0] @@ -41,7 +40,7 @@ class Shell(dotbot.Plugin): else: self._log.lowinfo('%s [%s]' % (msg, cmd)) ret = subprocess.call(cmd, shell=True, stdin=stdin, stdout=stdout, - stderr=stderr, cwd=self._base_directory) + stderr=stderr, cwd=self._base_directory) if ret != 0: success = False self._log.warning('Command [%s] failed' % cmd) From 717176ad18604cca5341d7a6ae2d28ccb4a084c2 Mon Sep 17 00:00:00 2001 From: Aleks Kamko Date: Fri, 26 Feb 2016 22:05:39 -0800 Subject: [PATCH 2/2] Update plugin tests for defaults feature --- test/tests/plugin-dir.bash | 2 +- test/tests/plugin.bash | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/tests/plugin-dir.bash b/test/tests/plugin-dir.bash index 299f144..a53f3ea 100644 --- a/test/tests/plugin-dir.bash +++ b/test/tests/plugin-dir.bash @@ -11,7 +11,7 @@ class Test(dotbot.Plugin): def can_handle(self, directive): return directive == "test" - def handle(self, directive, data): + def handle(self, directive, data, defaults): with open(os.path.expanduser("~/flag"), "w") as f: f.write("it works") return True diff --git a/test/tests/plugin.bash b/test/tests/plugin.bash index 960e9ce..bc46f75 100644 --- a/test/tests/plugin.bash +++ b/test/tests/plugin.bash @@ -10,7 +10,7 @@ class Test(dotbot.Plugin): def can_handle(self, directive): return directive == "test" - def handle(self, directive, data): + def handle(self, directive, data, defaults): with open(os.path.expanduser("~/flag"), "w") as f: f.write("it works") return True