Config module no longer remember cli args.

- The .set_cli method is dropped;
 - .load method now accept an 'args' argument instead;
 - no cli args is read by default when 'args' is None.

Implements: blueprint config-module

Change-Id: Ic41904a584495170d8ca26c93c0d68e708469cc6
This commit is contained in:
Zhihao Yuan 2013-04-05 14:22:01 -04:00
parent 948476edf9
commit fe23abd2b8
5 changed files with 22 additions and 36 deletions

View File

@ -25,7 +25,7 @@ def fail(returncode, e):
def run():
try:
server = bootstrap.Bootstrap()
server = bootstrap.Bootstrap(cli_args=sys.argv[1:])
server.run()
except KeyboardInterrupt:
fail(1, '... terminating marconi')

View File

@ -32,8 +32,8 @@ class Bootstrap(object):
lifetimes.
"""
def __init__(self, config_file=None):
cfg_handle.load(config_file)
def __init__(self, config_file=None, cli_args=None):
cfg_handle.load(filename=config_file, args=cli_args)
self.storage_module = import_driver(cfg.storage)
self.transport_module = import_driver(cfg.transport)

View File

@ -45,22 +45,19 @@ sections named by their associated namespaces.
To load the configurations from a file:
cfg_handle = config.project('marconi')
cfg_handle.load("/path/to/example.conf")
cfg_handle.load(filename="/path/to/example.conf")
A call to `.load` without an argument looks up for the default ones:
A call to `.load` without a filename looks up for the default ones:
~/.marconi/marconi.conf
/etc/marconi/marconi.conf
Global config variables, if any, will be read from the command line using
sys.argv[:1]. If needed, this can be overwritten by calling `set_cli`
before calling `.load`
Global config variables, if any, can also be read from the command line
arguments:
cfg_handle.set_cli([])
cfg_handle.load(filename="example.conf", args=sys.argv[1:])
"""
import sys
from oslo.config import cfg
@ -76,7 +73,6 @@ def _init():
__setattr__ = dict.__setitem__
conf = cfg.ConfigOpts()
my = Obj(args=sys.argv[1:])
def namespace(name, title=None):
"""
@ -132,17 +128,7 @@ def _init():
return from_class(opaque_type_of(ConfigProxy, name))()
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):
def load(filename=None, args=None):
"""Load the configurations from a config file.
If the file name is not supplied, look for
@ -153,15 +139,18 @@ def _init():
/etc/%project/%project.conf
:param config_file: the name of an alternative config file
:param filename: the name of an alternative config file
:param args: command line arguments
"""
if config_file is None:
conf(args=my.args, project=name, prog=name)
else:
conf(args=my.args, default_config_files=[config_file])
args = [] if args is None else args
return Obj(from_options=from_options, set_cli=set_cli, load=load)
if filename is None:
conf(args=args, project=name, prog=name)
else:
conf(args=args, default_config_files=[filename])
return Obj(from_options=from_options, load=load)
def opaque_type_of(base, postfix):
return type('%s of %s' % (base.__name__, postfix), (base,), {})

View File

@ -27,11 +27,10 @@ class TestConfig(testing.TestBase):
def test_cli(self):
args = ['--with_help', 'sense']
cfg_handle.set_cli(args)
cfg_handle.load(self.conf_path('wsgi_sqlite.conf'))
cfg_handle.load(self.conf_path('wsgi_sqlite.conf'), args)
self.assertEquals(cfg.with_help, 'sense')
cfg_handle.set_cli([])
cfg_handle.load()
cfg_handle.load(args=[])
self.assertEquals(cfg.with_help, None)
def test_wrong_type(self):

View File

@ -51,9 +51,7 @@ class TestBase(testtools.TestCase):
:returns: Project's config object.
"""
# Reset CLI
cfg.set_cli([])
cfg.load(self.conf_path(filename))
cfg.load(filename=self.conf_path(filename))
return cfg
def _my_dir(self):