mirror of
1
0
Fork 0
dotbot/dotbot/dispatcher.py

79 lines
2.8 KiB
Python
Raw Normal View History

2014-03-19 23:07:30 -04:00
import os
from argparse import Namespace
from .context import Context
from .messenger import Messenger
from .plugin import Plugin
2014-03-19 23:07:30 -04:00
2022-01-30 18:48:30 -05:00
class Dispatcher:
2022-01-30 18:48:30 -05:00
def __init__(
self,
base_directory,
only=None,
skip=None,
exit_on_failure=False,
options=Namespace(),
plugins=None,
2022-01-30 18:48:30 -05:00
):
2014-03-19 23:07:30 -04:00
self._log = Messenger()
self._setup_context(base_directory, options)
plugins = plugins or []
self._plugins = [plugin(self._context) for plugin in plugins]
self._only = only
self._skip = skip
2021-09-12 20:32:25 -04:00
self._exit = exit_on_failure
2014-03-19 23:07:30 -04:00
def _setup_context(self, base_directory, options):
2022-01-30 18:48:30 -05:00
path = os.path.abspath(os.path.expanduser(base_directory))
if not os.path.exists(path):
2022-01-30 18:48:30 -05:00
raise DispatchError("Nonexistent base directory")
self._context = Context(path, options)
2014-03-19 23:07:30 -04:00
def dispatch(self, tasks):
success = True
for task in tasks:
for action in task:
2022-01-30 18:48:30 -05:00
if (
self._only is not None
and action not in self._only
or self._skip is not None
and action in self._skip
) and action != "defaults":
self._log.info("Skipping action %s" % action)
continue
2014-03-19 23:07:30 -04:00
handled = False
2022-01-30 18:48:30 -05:00
if action == "defaults":
self._context.set_defaults(task[action]) # replace, not update
handled = True
# keep going, let other plugins handle this if they want
2014-03-19 23:07:30 -04:00
for plugin in self._plugins:
if plugin.can_handle(action):
try:
2021-09-12 20:32:25 -04:00
local_success = plugin.handle(action, task[action])
if not local_success and self._exit:
# The action has failed exit
2022-01-30 18:48:30 -05:00
self._log.error("Action %s failed" % action)
2021-09-12 20:32:25 -04:00
return False
success &= local_success
2014-03-19 23:07:30 -04:00
handled = True
2018-01-27 02:27:44 -05:00
except Exception as err:
2014-03-19 23:07:30 -04:00
self._log.error(
2022-01-30 18:48:30 -05:00
"An error was encountered while executing action %s" % action
)
2018-01-27 02:27:44 -05:00
self._log.debug(err)
2021-09-12 20:32:25 -04:00
if self._exit:
# There was an execption exit
return False
2014-03-19 23:07:30 -04:00
if not handled:
success = False
2022-01-30 18:48:30 -05:00
self._log.error("Action %s not handled" % action)
2021-09-12 20:32:25 -04:00
if self._exit:
# Invalid action exit
return False
2014-03-19 23:07:30 -04:00
return success
class DispatchError(Exception):
pass