1
0
Fork 0
mirror of synced 2024-12-22 14:11: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): class Dispatcher(object):
def __init__(self, base_directory): def __init__(self, base_directory):
self._log = Messenger() self._log = Messenger()
self._defaults = {}
self._set_base_directory(base_directory) self._set_base_directory(base_directory)
self._load_plugins() self._load_plugins()
@ -20,11 +21,16 @@ class Dispatcher(object):
success = True success = True
for task in tasks: for task in tasks:
for action in task: for action in task:
if action == 'defaults':
self._defaults = task[action]
self._log.info('Set defaults')
continue
handled = False handled = False
for plugin in self._plugins: for plugin in self._plugins:
if plugin.can_handle(action): if plugin.can_handle(action):
try: try:
success &= plugin.handle(action, task[action]) success &= plugin.handle(action, task[action],
self._defaults.get(action, {}))
handled = True handled = True
except Exception: except Exception:
self._log.error( self._log.error(
@ -36,8 +42,7 @@ class Dispatcher(object):
return success return success
def _load_plugins(self): def _load_plugins(self):
self._plugins = [plugin(self._base_directory) self._plugins = [plugin(self._base_directory) for plugin in Plugin.__subclasses__()]
for plugin in Plugin.__subclasses__()]
class DispatchError(Exception): class DispatchError(Exception):
pass pass

View file

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

View file

@ -10,7 +10,7 @@ class Clean(dotbot.Plugin):
def can_handle(self, directive): def can_handle(self, directive):
return directive == self._directive return directive == self._directive
def handle(self, directive, data): def handle(self, directive, data, defaults):
if directive != self._directive: if directive != self._directive:
raise ValueError('Clean cannot handle directive %s' % directive) raise ValueError('Clean cannot handle directive %s' % directive)
return self._process_clean(data) return self._process_clean(data)

View file

@ -10,12 +10,12 @@ class Link(dotbot.Plugin):
def can_handle(self, directive): def can_handle(self, directive):
return directive == self._directive return directive == self._directive
def handle(self, directive, data): def handle(self, directive, data, defaults):
if directive != self._directive: if directive != self._directive:
raise ValueError('Link cannot handle directive %s' % 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 success = True
for destination, source in links.items(): for destination, source in links.items():
source = os.path.expandvars(source) source = os.path.expandvars(source)
@ -23,10 +23,10 @@ class Link(dotbot.Plugin):
if isinstance(source, dict): if isinstance(source, dict):
# extended config # extended config
path = source['path'] path = source['path']
relative = source.get('relative', False) relative = source.get('relative', defaults.get('relative', False))
force = source.get('force', False) force = source.get('force', defaults.get('force', False))
relink = source.get('relink', False) relink = source.get('relink', defaults.get('relink', False))
create = source.get('create', False) create = source.get('create', defaults.get('create', False))
if create: if create:
success &= self._create(destination) success &= self._create(destination)
if force: if force:

View file

@ -10,13 +10,12 @@ class Shell(dotbot.Plugin):
def can_handle(self, directive): def can_handle(self, directive):
return directive == self._directive return directive == self._directive
def handle(self, directive, data): def handle(self, directive, data, defaults):
if directive != self._directive: if directive != self._directive:
raise ValueError('Shell cannot handle directive %s' % raise ValueError('Shell cannot handle directive %s' % directive)
directive) return self._process_commands(data, defaults)
return self._process_commands(data)
def _process_commands(self, data): def _process_commands(self, data, defaults):
success = True success = True
with open(os.devnull, 'w') as devnull: with open(os.devnull, 'w') as devnull:
for item in data: for item in data:
@ -24,11 +23,11 @@ class Shell(dotbot.Plugin):
if isinstance(item, dict): if isinstance(item, dict):
cmd = item['command'] cmd = item['command']
msg = item.get('description', None) msg = item.get('description', None)
if item.get('stdin', False) is True: if item.get('stdin', defaults.get('stdin', False)) is True:
stdin = None stdin = None
if item.get('stdout', False) is True: if item.get('stdout', defaults.get('stdout', False)) is True:
stdout = None stdout = None
if item.get('stderr', False) is True: if item.get('stderr', defaults.get('stderr', False)) is True:
stderr = None stderr = None
elif isinstance(item, list): elif isinstance(item, list):
cmd = item[0] cmd = item[0]
@ -41,7 +40,7 @@ class Shell(dotbot.Plugin):
else: else:
self._log.lowinfo('%s [%s]' % (msg, cmd)) self._log.lowinfo('%s [%s]' % (msg, cmd))
ret = subprocess.call(cmd, shell=True, stdin=stdin, stdout=stdout, 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: if ret != 0:
success = False success = False
self._log.warning('Command [%s] failed' % cmd) self._log.warning('Command [%s] failed' % cmd)