From d6e1e4ad5612628d30df7846f2e2d03ac2e4764b Mon Sep 17 00:00:00 2001 From: "E. Keys" Date: Thu, 3 Dec 2020 07:35:27 -0500 Subject: [PATCH] Context._options defaults to argparse.Namespace Updated default value of `Context._options` from `None` to `Namespace`. This makes member access operate the same regardless of where used and to remove the need for None checking. It also provides a hint to plugin developers for how to deserialize options from a dict if they had to. Opted for `argparse.Namespace` instead of `types.SimpleNamespace` to support Python 2.7. --- dotbot/context.py | 7 +++---- dotbot/dispatcher.py | 11 ++++------- test/tests/plugin.bash | 3 --- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/dotbot/context.py b/dotbot/context.py index 9820757..cb2b5df 100644 --- a/dotbot/context.py +++ b/dotbot/context.py @@ -1,12 +1,13 @@ import copy import os +from argparse import Namespace class Context(object): ''' Contextual data and information for plugins. ''' - def __init__(self, base_directory, options): + def __init__(self, base_directory, options=Namespace()): self._base_directory = base_directory self._defaults = {} self._options = options @@ -28,6 +29,4 @@ class Context(object): return copy.deepcopy(self._defaults) def options(self): - if self._options is not None: - return copy.deepcopy(self._options) - return None + return copy.deepcopy(self._options) diff --git a/dotbot/dispatcher.py b/dotbot/dispatcher.py index fa38092..c5a21ac 100644 --- a/dotbot/dispatcher.py +++ b/dotbot/dispatcher.py @@ -1,19 +1,16 @@ import os +from argparse import Namespace from .plugin import Plugin from .messenger import Messenger from .context import Context class Dispatcher(object): - def __init__(self, base_directory, only=None, skip=None, options=None): + def __init__(self, base_directory, only=None, skip=None, options=Namespace()): self._log = Messenger() self._setup_context(base_directory, options) self._load_plugins() - if options is not None: - self._only = options.only - self._skip = options.skip - else: - self._only = only - self._skip = skip + self._only = options.only or only + self._skip = options.skip or skip def _setup_context(self, base_directory, options): path = os.path.abspath( diff --git a/test/tests/plugin.bash b/test/tests/plugin.bash index 47e348d..1e113b7 100644 --- a/test/tests/plugin.bash +++ b/test/tests/plugin.bash @@ -40,9 +40,6 @@ class Test(dotbot.Plugin): def handle(self, directive, data): self._log.debug("Attempting to get options from Context") options = self._context.options() - if options is None: - self._log.debug("Context.options is None, expected not None") - return False if len(options.plugins) != 1: self._log.debug("Context.options.plugins length is %i, expected 1" % len(options.plugins)) return False