Move .set_cli and .load to the project() object.
The API changes from config.project().from_options().load() to config.project().load(); the intermediate project() object now serves as the config handle. As a 'side-effect', config vars can be named 'set_cli' and 'load' now. Change-Id: Ic676f3cb3dabd03a67bc523e1de9a1e1d60a9431
This commit is contained in:
parent
a23643ec3c
commit
541d2b5731
@ -100,46 +100,42 @@ def _init():
|
||||
for k, v in opts.items():
|
||||
conf.register_cli_opt(_make_opt(k, v))
|
||||
|
||||
@staticmethod
|
||||
def set_cli(args):
|
||||
"""
|
||||
Save the CLI arguments.
|
||||
|
||||
:param args: a list of CLI arguments in strings
|
||||
"""
|
||||
|
||||
my.args = []
|
||||
my.args.extend(args)
|
||||
|
||||
@staticmethod
|
||||
def load(config_file=None):
|
||||
"""Load the configurations from a config file.
|
||||
|
||||
If the file name is not supplied, look for
|
||||
|
||||
/etc/%project/%project.conf
|
||||
|
||||
and
|
||||
|
||||
~/.%project/%project.conf
|
||||
|
||||
:param config_file: the name of an alternative config file
|
||||
"""
|
||||
|
||||
if config_file is None:
|
||||
conf(args=my.args, project=name, prog=name)
|
||||
else:
|
||||
conf(args=my.args, default_config_files=[config_file])
|
||||
|
||||
def from_class(cls):
|
||||
grant_access_to_class(conf, cls)
|
||||
cls.set_cli = set_cli
|
||||
cls.load = load
|
||||
return cls
|
||||
|
||||
return from_class(opaque_type_of(ConfigProxy, name))()
|
||||
|
||||
return Obj(from_options=from_options)
|
||||
def set_cli(args):
|
||||
"""
|
||||
Save the CLI arguments.
|
||||
|
||||
:param args: a list of CLI arguments in strings
|
||||
"""
|
||||
|
||||
my.args = []
|
||||
my.args.extend(args)
|
||||
|
||||
def load(config_file=None):
|
||||
"""Load the configurations from a config file.
|
||||
|
||||
If the file name is not supplied, look for
|
||||
|
||||
/etc/%project/%project.conf
|
||||
|
||||
and
|
||||
|
||||
~/.%project/%project.conf
|
||||
|
||||
:param config_file: the name of an alternative config file
|
||||
"""
|
||||
|
||||
if config_file is None:
|
||||
conf(args=my.args, project=name, prog=name)
|
||||
else:
|
||||
conf(args=my.args, default_config_files=[config_file])
|
||||
|
||||
return Obj(from_options=from_options, set_cli=set_cli, load=load)
|
||||
|
||||
def opaque_type_of(base, postfix):
|
||||
return type('%s of %s' % (base.__name__, postfix), (base,), {})
|
||||
|
@ -18,7 +18,7 @@ from marconi.storage import reference as storage
|
||||
from marconi.transport.wsgi import driver as wsgi
|
||||
|
||||
|
||||
cfg = config.project('marconi').from_options()
|
||||
cfg_handle = config.project('marconi')
|
||||
|
||||
|
||||
class Kernel(object):
|
||||
@ -31,7 +31,7 @@ class Kernel(object):
|
||||
|
||||
def __init__(self, config_file=None):
|
||||
#TODO(kgriffs): Error handling
|
||||
cfg.load(config_file)
|
||||
cfg_handle.load(config_file)
|
||||
|
||||
#TODO(kgriffs): Determine driver types from cfg
|
||||
self.storage = storage.Driver()
|
||||
|
@ -19,7 +19,8 @@ from marconi.common import config
|
||||
from marconi.tests.util import suite
|
||||
|
||||
|
||||
cfg = config.project().from_options(
|
||||
cfg_handle = config.project()
|
||||
cfg = cfg_handle.from_options(
|
||||
without_help=3,
|
||||
with_help=(None, "nonsense"))
|
||||
|
||||
@ -28,11 +29,11 @@ class TestConfig(suite.TestSuite):
|
||||
|
||||
def test_cli(self):
|
||||
args = ['--with_help', 'sense']
|
||||
cfg.set_cli(args)
|
||||
cfg.load(self.conf_path('wsgi_reference.conf'))
|
||||
cfg_handle.set_cli(args)
|
||||
cfg_handle.load(self.conf_path('wsgi_reference.conf'))
|
||||
self.assertEquals(cfg.with_help, 'sense')
|
||||
cfg.set_cli([])
|
||||
cfg.load()
|
||||
cfg_handle.set_cli([])
|
||||
cfg_handle.load()
|
||||
self.assertEquals(cfg.with_help, None)
|
||||
|
||||
def test_wrong_type(self):
|
||||
|
Loading…
Reference in New Issue
Block a user