1
0
Fork 0
mirror of synced 2024-12-22 06:01:07 -05:00

Add default options

This commit is contained in:
Aleks Kamko 2016-02-26 21:43:11 -08:00
parent daf8d82e02
commit 74637f0ca0
5 changed files with 25 additions and 21 deletions

View file

@ -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

View file

@ -15,7 +15,7 @@ class Plugin(object):
'''
raise NotImplementedError
def handle(self, directive, data):
def handle(self, directive, data, defaults):
'''
Executes the directive.

View file

@ -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)

View file

@ -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:

View file

@ -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)