##// END OF EJS Templates
make Config.merge a public method...
MinRK -
Show More
@@ -379,7 +379,7 b' class Application(SingletonConfigurable):'
379 379 # Save a copy of the current config.
380 380 newconfig = deepcopy(self.config)
381 381 # Merge the new config into the current one.
382 newconfig._merge(config)
382 newconfig.merge(config)
383 383 # Save the combined config as self.config, which triggers the traits
384 384 # events.
385 385 self.config = newconfig
@@ -148,7 +148,7 b' class Configurable(HasTraits):'
148 148 # Save a copy of the current config.
149 149 newconfig = deepcopy(self.config)
150 150 # Merge the new config into the current one.
151 newconfig._merge(config)
151 newconfig.merge(config)
152 152 # Save the combined config as self.config, which triggers the traits
153 153 # events.
154 154 self.config = newconfig
@@ -99,14 +99,19 b' class Config(dict):'
99 99 dict.__setattr__(self, key, Config(obj))
100 100
101 101 def _merge(self, other):
102 """deprecated alias, use Config.merge()"""
103 self.merge(other)
104
105 def merge(self, other):
106 """merge another config object into this one"""
102 107 to_update = {}
103 108 for k, v in other.iteritems():
104 109 if k not in self:
105 110 to_update[k] = v
106 111 else: # I have this key
107 if isinstance(v, Config):
112 if isinstance(v, Config) and isinstance(self[k], Config):
108 113 # Recursively merge common sub Configs
109 self[k]._merge(v)
114 self[k].merge(v)
110 115 else:
111 116 # Plain updates for non-Configs
112 117 to_update[k] = v
@@ -328,7 +333,7 b' class PyFileConfigLoader(FileConfigLoader):'
328 333 # when a user s using a profile, but not the default config.
329 334 pass
330 335 else:
331 self.config._merge(sub_config)
336 self.config.merge(sub_config)
332 337
333 338 # Again, this needs to be a closure and should be used in config
334 339 # files to get the config being loaded.
@@ -691,7 +696,7 b' class KVArgParseConfigLoader(ArgParseConfigLoader):'
691 696 if self.extra_args:
692 697 sub_parser = KeyValueConfigLoader()
693 698 sub_parser.load_config(self.extra_args)
694 self.config._merge(sub_parser.config)
699 self.config.merge(sub_parser.config)
695 700 self.extra_args = sub_parser.extra_args
696 701
697 702
@@ -715,5 +720,5 b' def load_pyconfig_files(config_files, path):'
715 720 except:
716 721 raise
717 722 else:
718 config._merge(next_config)
723 config.merge(next_config)
719 724 return config
@@ -222,11 +222,11 b' class TestConfig(TestCase):'
222 222 c2 = Config()
223 223 c2.bar = 10
224 224 c2.Foo.bar = 10
225 c1._merge(c2)
225 c1.merge(c2)
226 226 self.assertEqual(c1.Foo.bar, 10)
227 227 self.assertEqual(c1.bar, 10)
228 228 c2.Bar.bar = 10
229 c1._merge(c2)
229 c1.merge(c2)
230 230 self.assertEqual(c1.Bar.bar, 10)
231 231
232 232 def test_merge_exists(self):
@@ -236,12 +236,12 b' class TestConfig(TestCase):'
236 236 c1.Foo.bam = 30
237 237 c2.Foo.bar = 20
238 238 c2.Foo.wow = 40
239 c1._merge(c2)
239 c1.merge(c2)
240 240 self.assertEqual(c1.Foo.bam, 30)
241 241 self.assertEqual(c1.Foo.bar, 20)
242 242 self.assertEqual(c1.Foo.wow, 40)
243 243 c2.Foo.Bam.bam = 10
244 c1._merge(c2)
244 c1.merge(c2)
245 245 self.assertEqual(c1.Foo.Bam.bam, 10)
246 246
247 247 def test_deepcopy(self):
@@ -267,17 +267,17 b' class TestConfig(TestCase):'
267 267 self.assertEqual(c1.Foo.__class__, Config)
268 268 self.assertEqual(c1.Foo.bar, 1)
269 269
270 def test_fromdict_merge(self):
270 def test_fromdictmerge(self):
271 271 c1 = Config()
272 272 c2 = Config({'Foo' : {'bar' : 1}})
273 c1._merge(c2)
273 c1.merge(c2)
274 274 self.assertEqual(c1.Foo.__class__, Config)
275 275 self.assertEqual(c1.Foo.bar, 1)
276 276
277 def test_fromdict_merge2(self):
277 def test_fromdictmerge2(self):
278 278 c1 = Config({'Foo' : {'baz' : 2}})
279 279 c2 = Config({'Foo' : {'bar' : 1}})
280 c1._merge(c2)
280 c1.merge(c2)
281 281 self.assertEqual(c1.Foo.__class__, Config)
282 282 self.assertEqual(c1.Foo.bar, 1)
283 283 self.assertEqual(c1.Foo.baz, 2)
General Comments 0
You need to be logged in to leave comments. Login now