def configure(self, *args, **kwargs): """ Called to manually configure the settings. The 'default_settings' parameter sets where to retrieve any unspecified values from (its argument should be a :class:`dict`). Arguments --------- *args : Passed along to :class:`Settings` **kwargs : Passed along to :class:`Settings` Raises ------ ImproperlyConfigured If settings is already configured, will throw this exception """ if self._wrapped is not None: raise ImproperlyConfigured('Settings already configured.') logger.debug2('Pre settings configure') self._wrapped = Settings(*args, **kwargs) for pattern, settings in global_templates: if nested_in_dict(pattern, self._wrapped): # Not the most efficient way to do this, but insignificant "preupdate" d = {} nested_update(d, settings) nested_update(d, self._wrapped) # Nested update and run patch code self._wrapped.update(d) def read_json(json_file): # In case json_file is an @settings_property function if getattr(json_file, 'settings_property', None): json_file = json_file(settings) return Settings(json_load(json_file)) nested_patch_inplace( self._wrapped, lambda key, value: (isinstance(key, str) and (isinstance(value, str) or getattr( value, 'settings_property', False)) and any(key.endswith(pattern) for pattern in json_include_suffixes)), lambda key, value: read_json(value)) # Importing these here is intentional, it guarantees the signals are # connected so that executor and computes can setup logging if need be import terra.executor # noqa import terra.compute # noqa from terra.core.signals import post_settings_configured post_settings_configured.send(sender=self) logger.debug2('Post settings configure')
def test_nested_update_dervied(self): class FooDict(dict): pass foo = FooDict({'a': 15, 'b': FooDict({'c': 14, 'f': 18})}) bar = {'a': 16, 'd': {'e': 17}, 'b': {'c': 24}} nested_update(foo, bar) ans = {'a': 16, 'd': {'e': 17}, 'b': {'c': 24, 'f': 18}} self.assertEqual(foo, ans) self.assertIsInstance(foo, FooDict) self.assertIsInstance(foo['b'], FooDict) self.assertIsInstance(foo['d'], FooDict)
def test_nested_update(self): x = {"a": 1, "b": {"c": 2, "d": 3}, "d": 4} # Normal update y = {"a": 11, "b": {"c": 22, "e": 33}} ans = {"a": 11, "b": {"c": 22, "d": 3, "e": 33}, "d": 4} z = copy.deepcopy(x) nested_update(z, y) self.assertEqual(z, ans) # Keys not there before + replace dict with int y = {"b": 5, "e": {"c": 22, "e": 33}, "f": 6} ans = {"a": 1, "b": 5, "e": {"c": 22, "e": 33}, "d": 4, "f": 6} z = copy.deepcopy(x) nested_update(z, y) self.assertEqual(z, ans) y = {"a": {"g": 15}} z = copy.deepcopy(x) # No longer a type error # with self.assertRaises(TypeError): nested_update(z, y) ans = {'a': {'g': 15}, 'b': {'c': 2, 'd': 3}, 'd': 4} self.assertEqual(z, ans) z = {'foo': 13, 'bar': 12, 'far': 14, 'boo': {}} z2 = copy.deepcopy(z) nested_update(z2, foo={}, bar={'x': 11}, far={'y': {}}, boo=[11, 22, 33]) ans = { 'foo': {}, 'bar': { 'x': 11 }, 'far': { 'y': {} }, 'boo': [11, 22, 33] } self.assertEqual(z2, ans) nested_update(z2, foo=13, bar=12, far=14, boo={}) self.assertEqual(z, z2)
def test_nested_update(self): x = {"a": 1, "b": {"c": 2, "d": 3}, "d": 4} # Normal update y = {"a": 11, "b": {"c": 22, "e": 33}} ans = {"a": 11, "b": {"c": 22, "d": 3, "e": 33}, "d": 4} z = x.copy() nested_update(z, y) self.assertEqual(z, ans) # Keys not there before + replace dict with int y = {"b": 5, "e": {"c": 22, "e": 33}, "f": 6} ans = {"a": 1, "b": 5, "e": {"c": 22, "e": 33}, "d": 4, "f": 6} z = x.copy() nested_update(z, y) self.assertEqual(z, ans) y = {"a": {"g": 15}} z = x.copy() with self.assertRaises(TypeError): nested_update(z, y)
def update(self, *args, **kwargs): """ Supported """ nested_update(self, *args, **kwargs)