From 5c0a76908b1eb77a06e4a9d95e377110f20dc05c Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 15 Nov 2020 15:24:24 +0100 Subject: plugin: reduce code duplication Factor out supported directive checking, drop redundant checking in handle(). --- dotbot/plugin.py | 4 +++- dotbot/plugins/clean.py | 10 +--------- dotbot/plugins/create.py | 10 +--------- dotbot/plugins/link.py | 10 +--------- dotbot/plugins/shell.py | 9 --------- 5 files changed, 6 insertions(+), 37 deletions(-) diff --git a/dotbot/plugin.py b/dotbot/plugin.py index 56d4da8..5e2c923 100644 --- a/dotbot/plugin.py +++ b/dotbot/plugin.py @@ -6,6 +6,8 @@ class Plugin(object): Abstract base class for commands that process directives. ''' + _directive = None + def __init__(self, context): self._context = context self._log = Messenger() @@ -14,7 +16,7 @@ class Plugin(object): ''' Returns true if the Plugin can handle the directive. ''' - raise NotImplementedError + return directive == self._directive def handle(self, directive, data): ''' diff --git a/dotbot/plugins/clean.py b/dotbot/plugins/clean.py index e7f1561..c8f3a03 100644 --- a/dotbot/plugins/clean.py +++ b/dotbot/plugins/clean.py @@ -9,15 +9,7 @@ class Clean(dotbot.Plugin): _directive = 'clean' - def can_handle(self, directive): - return directive == self._directive - - def handle(self, directive, data): - if directive != self._directive: - raise ValueError('Clean cannot handle directive %s' % directive) - return self._process_clean(data) - - def _process_clean(self, targets): + def handle(self, directive, targets): success = True defaults = self._context.defaults().get(self._directive, {}) for target in targets: diff --git a/dotbot/plugins/create.py b/dotbot/plugins/create.py index 7e39c1a..2420558 100644 --- a/dotbot/plugins/create.py +++ b/dotbot/plugins/create.py @@ -9,15 +9,7 @@ class Create(dotbot.Plugin): _directive = 'create' - def can_handle(self, directive): - return directive == self._directive - - def handle(self, directive, data): - if directive != self._directive: - raise ValueError('Create cannot handle directive %s' % directive) - return self._process_paths(data) - - def _process_paths(self, paths): + def handle(self, directive, paths): success = True for path in paths: path = os.path.expandvars(os.path.expanduser(path)) diff --git a/dotbot/plugins/link.py b/dotbot/plugins/link.py index 4112fe5..1064bb6 100644 --- a/dotbot/plugins/link.py +++ b/dotbot/plugins/link.py @@ -13,15 +13,7 @@ class Link(dotbot.Plugin): _directive = 'link' - def can_handle(self, directive): - return directive == self._directive - - def handle(self, directive, data): - if directive != self._directive: - raise ValueError('Link cannot handle directive %s' % directive) - return self._process_links(data) - - def _process_links(self, links): + def handle(self, directive, links): success = True defaults = self._context.defaults().get('link', {}) for destination, source in links.items(): diff --git a/dotbot/plugins/shell.py b/dotbot/plugins/shell.py index 6fe79b8..f0121e6 100644 --- a/dotbot/plugins/shell.py +++ b/dotbot/plugins/shell.py @@ -11,16 +11,7 @@ class Shell(dotbot.Plugin): _directive = 'shell' - def can_handle(self, directive): - return directive == self._directive - def handle(self, directive, data): - if directive != self._directive: - raise ValueError('Shell cannot handle directive %s' % - directive) - return self._process_commands(data) - - def _process_commands(self, data): success = True defaults = self._context.defaults().get('shell', {}) for item in data: -- cgit v1.2.3