Permalink
Browse files

prevent flags from clobbering entire config sections

previously failing test included
  • Loading branch information...
1 parent bd7e164 commit 465241ee1d9fa6e74564605812abd7028ad4eaa9 @minrk committed May 29, 2011
@@ -355,8 +355,6 @@ def boolean_flag(name, configurable, set_help='', unset_help=''):
cls,trait = configurable.split('.')
- setter = Config()
- setter[cls][trait] = True
- unsetter = Config()
- unsetter[cls][trait] = False
+ setter = {cls : {trait : True}}
+ unsetter = {cls : {trait : False}}
return {name : (setter, set_help), 'no-'+name : (unsetter, unset_help)}
View
@@ -408,8 +408,10 @@ def load_config(self, argv=None, aliases=None, flags=None):
if cfg is None:
raise ArgumentError("Unrecognized flag: %r"%item)
elif isinstance(cfg, (dict, Config)):
- # update self.config with Config:
- self.config.update(cfg)
+ # don't clobber whole config sections, update
+ # each section from config:
+ for sec,c in cfg.iteritems():
+ self.config[sec].update(c)
else:
raise ValueError("Invalid flag: %r"%flag)
else:
@@ -42,6 +42,7 @@ class Foo(Configurable):
class Bar(Configurable):
+ b = Int(0, config=True, help="The integer b.")
enabled = Bool(True, config=True, help="Enable bar.")
@@ -94,7 +95,7 @@ def test_config_propagation(self):
self.assertEquals(app.foo.j, 10)
self.assertEquals(app.bar.enabled, False)
- def test_alias(self):
+ def test_flags(self):
app = MyApp()
app.parse_command_line(["--disable"])
app.init_bar()
@@ -103,3 +104,26 @@ def test_alias(self):
app.init_bar()
self.assertEquals(app.bar.enabled, True)
+ def test_aliases(self):
+ app = MyApp()
+ app.parse_command_line(["i=5", "j=10"])
+ app.init_foo()
+ self.assertEquals(app.foo.i, 5)
+ app.init_foo()
+ self.assertEquals(app.foo.j, 10)
+
+ def test_flag_clobber(self):
+ """test that setting flags doesn't clobber existing settings"""
+ app = MyApp()
+ app.parse_command_line(["Bar.b=5", "--disable"])
+ print app.config
+ app.init_bar()
+ self.assertEquals(app.bar.enabled, False)
+ self.assertEquals(app.bar.b, 5)
+ app.parse_command_line(["--enable", "Bar.b=10"])
+ print app.config
+ app.init_bar()
+ self.assertEquals(app.bar.enabled, True)
+ self.assertEquals(app.bar.b, 10)
+
+
@@ -233,13 +233,13 @@ def find_profile_dir(cls, profile_dir, config=None):
)
base_flags = dict(
- debug = ({'Application' : Config({'log_level' : logging.DEBUG})},
+ debug = ({'Application' : {'log_level' : logging.DEBUG}},
"set log level to logging.DEBUG (maximize logging output)"),
- quiet = ({'Application' : Config({'log_level' : logging.CRITICAL})},
+ quiet = ({'Application' : {'log_level' : logging.CRITICAL}},
"set log level to logging.CRITICAL (minimize logging output)"),
- init = ({'BaseIPythonApplication' : Config({
+ init = ({'BaseIPythonApplication' : {
'copy_config_files' : True,
- 'auto_create' : True})
+ 'auto_create' : True}
}, "Initialize profile with default config files")
)

0 comments on commit 465241e

Please sign in to comment.