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:
parent
0ba45763d9
commit
d6e1e4ad56
3 changed files with 7 additions and 14 deletions
|
@ -1,12 +1,13 @@
|
||||||
import copy
|
import copy
|
||||||
import os
|
import os
|
||||||
|
from argparse import Namespace
|
||||||
|
|
||||||
class Context(object):
|
class Context(object):
|
||||||
'''
|
'''
|
||||||
Contextual data and information for plugins.
|
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._base_directory = base_directory
|
||||||
self._defaults = {}
|
self._defaults = {}
|
||||||
self._options = options
|
self._options = options
|
||||||
|
@ -28,6 +29,4 @@ class Context(object):
|
||||||
return copy.deepcopy(self._defaults)
|
return copy.deepcopy(self._defaults)
|
||||||
|
|
||||||
def options(self):
|
def options(self):
|
||||||
if self._options is not None:
|
return copy.deepcopy(self._options)
|
||||||
return copy.deepcopy(self._options)
|
|
||||||
return None
|
|
||||||
|
|
|
@ -1,19 +1,16 @@
|
||||||
import os
|
import os
|
||||||
|
from argparse import Namespace
|
||||||
from .plugin import Plugin
|
from .plugin import Plugin
|
||||||
from .messenger import Messenger
|
from .messenger import Messenger
|
||||||
from .context import Context
|
from .context import Context
|
||||||
|
|
||||||
class Dispatcher(object):
|
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._log = Messenger()
|
||||||
self._setup_context(base_directory, options)
|
self._setup_context(base_directory, options)
|
||||||
self._load_plugins()
|
self._load_plugins()
|
||||||
if options is not None:
|
self._only = options.only or only
|
||||||
self._only = options.only
|
self._skip = options.skip or skip
|
||||||
self._skip = options.skip
|
|
||||||
else:
|
|
||||||
self._only = only
|
|
||||||
self._skip = skip
|
|
||||||
|
|
||||||
def _setup_context(self, base_directory, options):
|
def _setup_context(self, base_directory, options):
|
||||||
path = os.path.abspath(
|
path = os.path.abspath(
|
||||||
|
|
|
@ -40,9 +40,6 @@ class Test(dotbot.Plugin):
|
||||||
def handle(self, directive, data):
|
def handle(self, directive, data):
|
||||||
self._log.debug("Attempting to get options from Context")
|
self._log.debug("Attempting to get options from Context")
|
||||||
options = self._context.options()
|
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:
|
if len(options.plugins) != 1:
|
||||||
self._log.debug("Context.options.plugins length is %i, expected 1" % len(options.plugins))
|
self._log.debug("Context.options.plugins length is %i, expected 1" % len(options.plugins))
|
||||||
return False
|
return False
|
||||||
|
|
Loading…
Reference in a new issue