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