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)