mirror of
1
0
Fork 0

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.
This commit is contained in:
E. Keys 2020-12-03 07:35:27 -05:00
parent 0ba45763d9
commit d6e1e4ad56
3 changed files with 7 additions and 14 deletions

View File

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

View File

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

View File

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