Resolves Issue #136

This commit is contained in:
BJ Dierkes 2012-07-16 00:52:28 -05:00
parent 7d346bba1a
commit ba079ad7c1
4 changed files with 67 additions and 19 deletions

View File

@ -44,7 +44,10 @@ Incompatible Changes:
live in the same file (i.e. hooks won't be registered just because
you imported an extension to sub-class a handler, etc).
* cement.utils.test_helper moved to cement.utils.test.
* By default, command line arguments no longer override config settings.
This is now configurable by the CementApp.Meta.arguments_override_config
boolean.
1.9.12 - Thu Jul 05, 2012
------------------------------------------------------------------------------

View File

@ -75,6 +75,13 @@ class CementApp(meta.MetaMixin):
and options.
Default: sys.argv[1:]
arguments_override_config
A boolean to toggle whether command line arguments should
override configuration values if the argument name matches the
config key. I.e. --foo=bar would override config['myapp']['foo'].
Default: False
config_section
The base configuration section for the application.
@ -310,6 +317,7 @@ class CementApp(meta.MetaMixin):
plugin_bootstrap = None
plugin_dir = None
argv = list(sys.argv[1:])
arguments_override_config = False
config_section = None
config_defaults = None
catch_signals = [signal.SIGTERM, signal.SIGINT]
@ -585,19 +593,20 @@ class CementApp(meta.MetaMixin):
def _parse_args(self):
self.args.parse(self.argv)
for member in dir(self.args.parsed_args):
if member and member.startswith('_'):
continue
if self._meta.arguments_override_config is True:
for member in dir(self.args.parsed_args):
if member and member.startswith('_'):
continue
# don't override config values for options that weren't passed
# or in otherwords are None
elif getattr(self.args.parsed_args, member) is None:
continue
# don't override config values for options that weren't passed
# or in otherwords are None
elif getattr(self.args.parsed_args, member) is None:
continue
for section in self.config.get_sections():
if member in self.config.keys(section):
self.config.set(section, member,
getattr(self.args.parsed_args, member))
for section in self.config.get_sections():
if member in self.config.keys(section):
self.config.set(section, member,
getattr(self.args.parsed_args, member))
def _setup_signals(self):
if not self._meta.catch_signals:

View File

@ -32,7 +32,8 @@ is the order in which configurations are discovered:
* Extended by any handler Meta.config_defaults (not overridden)
* Overridden by a config_defaults dict passed to foundation.CementApp()
* Overridden by the configuration files
* Overridden by command line options that match the same key name
* Overridden by command line options that match the same key name (only
if CementApp.Meta.arguments_override_config=True)
Application Default Settings
@ -213,8 +214,9 @@ If no config_files meta data is provided, Cement will set the defaults to:
Overriding Configurations with Command Line Options
---------------------------------------------------
Config settings are automatically overridden if a passed command line option
matches the name. Note that this happens in *all* sections:
Config settings can be automatically overridden by a passed command line
option if the argument name matches a configuration key. Note that this will
happen in *all* config sections:
.. code-block:: python
@ -223,7 +225,10 @@ matches the name. Note that this happens in *all* sections:
defaults = backend.defaults('base')
defaults['base']['foo'] = 'bar'
app = foundation.CementApp('myapp', config_defaults=defaults)
app = foundation.CementApp('myapp',
config_defaults=defaults,
arguments_override_config=True,
)
try:
# First setup the application
app.setup()

View File

@ -27,11 +27,42 @@ class ConfigTestCase(test.CementTestCase):
defaults = dict()
defaults['test'] = dict()
defaults['test']['debug'] = False
self.app = self.make_app(config_defaults=defaults, argv=['--debug'])
defaults['test']['foo'] = 'bar'
# first test that it doesn't override the config with the default
# setting of arguments_override_config=False
self.app = self.make_app(
config_defaults=defaults,
argv=['--foo=not_bar'],
arguments_override_config=False
)
self.app.setup()
self.app.args.add_argument('--foo', action='store')
self.app.run()
self.eq(self.app.config.get('test', 'debug'), True)
self.eq(self.app.config.get('test', 'foo'), 'bar')
# then make sure that it does
self.app = self.make_app(
config_defaults=defaults,
argv=['--foo=not_bar'],
arguments_override_config=True
)
self.app.setup()
self.app.args.add_argument('--foo', action='store')
self.app.run()
self.eq(self.app.config.get('test', 'foo'), 'not_bar')
# one last test just for code coverage
self.app = self.make_app(
config_defaults=defaults,
argv=['--debug'],
arguments_override_config=True
)
self.app.setup()
self.app.args.add_argument('--foo', action='store')
self.app.run()
self.eq(self.app.config.get('test', 'foo'), 'bar')
def test_parse_file_bad_path(self):
self.app._meta.config_files = ['./some_bogus_path']
self.app.setup()