def _call_spec_handlers(self, new_config): flattened_config = yapconf.flatten(new_config, self.spec._separator) flattened_current = yapconf.flatten(self.current_config, self.spec._separator) for key, value in flattened_config.items(): if value != flattened_current.get(key): item = self.spec.find_item(key) if item and item.watch_target: item.watch_target(flattened_current[key], value)
def test_dict_get_config_value(dict_item): value = dict_item.get_config_value([ ('label1', yapconf.flatten({'foo_dict': { 'foo': 'bar' }})) ]) assert value == {'foo': 'bar'}
def test_dict_get_config_value_ignore_environment(dict_item): dict_item.env_name = 'FOO_DICT' value = dict_item.get_config_value([ ('ENVIRONMENT', {'FOO_DICT': {'foo': 'bar'}}), ('label1', yapconf.flatten({'foo_dict': {'foo': 'baz'}})) ]) assert value == {'foo': 'baz'}
def _generate_overrides(self, *args): # An override is a tuple of label, dictionary # we cannot use a dictionary because we need to preserve # the order of the arguments passed in as they are significant overrides = [] for index, override in enumerate(args): source = self._extract_source(index, override) overrides.append(source.generate_override(self._separator)) # We manually generate defaults here so that it is easy to find out # if there are defaults for fallbacks that should be applied. overrides.append( ("__defaults__", yapconf.flatten(self.defaults, separator=self._separator),) ) return overrides
def test_basic_dict_get_config_value_from_env(db_item): value = db_item.get_config_value([('ENVIRONMENT', { 'ALT_NAME': 'db_name', 'DB_PORT': '1234', 'DB_VERBOSE': 'True' }), ('dict1', yapconf.flatten({ 'db': { 'users': ['user1', 'user2'], 'log': { 'level': 'INFO', 'file': '/some/file' } } }))]) assert value['name'] == 'db_name' assert value['port'] == 1234
def generate_override(self, separator="."): """Generate an override. Uses ``get_data`` which is expected to be implemented by each child class. Returns: A tuple of label, dict Raises: YapconfLoadError: If a known error occurs. """ data = self.get_data() if not isinstance(data, dict): raise YapconfLoadError( "Invalid source (%s). The data was loaded successfully, but " "the result was not a dictionary. Got %s" % (self.label, type(data))) return self.label, yapconf.flatten(data, separator=separator)
'foos': ['d'] }, ), (YapconfListItem( "foos", choices=[['a', 'a'], ['a', 'b'], ['c']], children={'foo': YapconfItem('foo', choices=['a', 'b', 'c'])}), { 'foos': ['a'] }), (YapconfDictItem("foo_dict", children={ "foo": YapconfItem( 'foo', choices=['bar', 'baz'], prefix='foo_dict') }), yapconf.flatten({'foo_dict': { 'foo': 'invalid' }})), ]) def test_choices(item, config): with pytest.raises(YapconfValueError): item.get_config_value([('test', config)]) def test_from_spec_list_fq_names(): spec = { 'list1': { 'type': 'list', 'required': True, 'items': { 'list_item': { 'type': 'str',
def test_flatten(original, expected): assert yapconf.flatten(original) == expected