Example #1
0
def test_from_specification_check_fq_names():
    spec = {
        'foo': {
            'type': 'dict',
            'required': True,
            'items': {
                'bar': {
                    'type': 'dict',
                    'required': True,
                    'items': {
                        'baz': {
                            'type': 'str',
                            'required': True,
                        },
                    },
                },
                'bat': {
                    'type': 'bool',
                }
            },
        },
    }
    items = from_specification(spec)
    assert items['foo'].fq_name == 'foo'
    assert items['foo'].children['bar'].fq_name == 'foo.bar'
    assert (
        items['foo'].children['bar'].children['baz'].fq_name == 'foo.bar.baz')
    assert items['foo'].children['bat'].fq_name == 'foo.bat'
Example #2
0
def test_invalid_value():
    def always_invalid(value):
        return False

    item = from_specification({'foo': {'validator': always_invalid}})['foo']
    with pytest.raises(YapconfValueError):
        item.get_config_value([('label', {'foo': 'bar'})])
Example #3
0
    def __init__(
        self,
        specification,
        file_type="json",
        env_prefix=None,
        encoding="utf-8",
        separator=".",
    ):
        self._file_type = file_type
        self._encoding = encoding

        if self._file_type not in yapconf.FILE_TYPES:
            raise YapconfSpecError(
                "Unsupported file type: {0}. Supported file types are: "
                "{1}".format(self._file_type, yapconf.FILE_TYPES)
            )

        self._specification = self._load_specification(specification)
        self._env_prefix = env_prefix
        self._separator = separator
        self._yapconf_items = from_specification(
            self._specification, self._env_prefix, self._separator
        )
        self._logger = logging.getLogger(__name__)
        self._sources = {}
Example #4
0
def test_from_spec_list_fq_names():
    spec = {
        'list1': {
            'type': 'list',
            'required': True,
            'items': {
                'list_item': {
                    'type': 'str',
                    'required': True
                },
            },
        },
        'list2': {
            'type': 'list',
            'required': True,
            'items': {
                'list_dict_item': {
                    'type': 'dict',
                    'required': True,
                    'items': {
                        'foo': {},
                        'bar': {},
                    },
                },
            },
        },
    }
    items = from_specification(spec)
    assert items['list1'].fq_name == 'list1'
    assert items['list1'].children['list1'].fq_name == 'list1'
    assert items['list2'].children['list2'].fq_name == 'list2'
    assert (items['list2'].children['list2'].children['foo'].fq_name ==
            'list2.foo')
    assert (items['list2'].children['list2'].children['bar'].fq_name ==
            'list2.bar')
Example #5
0
def dict_item(simple_item_spec):
    """A YapconfItem for a dictionary."""
    return from_specification({
        'foo_dict': {
            'type': 'dict',
            'items': {'foo': simple_item_spec}
        }
    })['foo_dict']
Example #6
0
def bool_list_item(bool_item_spec):
    """A YapconfItem for a list of boolean values."""
    return from_specification({
        'my_bools': {
            'type': 'list',
            'items': bool_item_spec
        }
    })['my_bools']
Example #7
0
def list_item(simple_item_spec):
    """A YapconfItem for a list value."""
    return from_specification({
        'foos': {
            'type': 'list',
            'items': {
                'simple_item': simple_item_spec
            }
        }
    })['foos']
Example #8
0
def db_item():
    """A YapconfItem for a dictionary that represents a db config."""
    return from_specification(
        {
            'db': {
                'type': 'dict',
                'items': {
                    'name': {
                        'required': True,
                        'alt_env_names': ['ALT_NAME'],
                    },
                    'port': {
                        'required': True,
                        'type': 'int',
                    },
                    'verbose': {
                        'required': True,
                        'type': 'bool',
                    },
                    'users': {
                        'type': 'list',
                        'required': True,
                        'items': {
                            'user': {
                                'required': True,
                                'type': 'str',
                            },
                        },
                    },
                    'log': {
                        'type': 'dict',
                        'required': True,
                        'items': {
                            'level': {
                                'required': True,
                                'type': 'str'
                            },
                            'file': {
                                'required': True,
                                'type': 'str'
                            },
                        },
                    },
                },
            },
        },
    )['db']
Example #9
0
    def __init__(self,
                 specification,
                 file_type='json',
                 env_prefix=None,
                 encoding='utf-8',
                 separator='.'):
        self._file_type = file_type
        self._encoding = encoding

        if self._file_type not in yapconf.FILE_TYPES:
            raise YapconfSpecError('Unsupported file type: {0}.'
                                   'Supported file types are: {1}'.format(
                                       self._file_type, yapconf.FILE_TYPES))

        self._specification = self._load_specification(specification)
        self._env_prefix = env_prefix
        self._separator = separator
        self._yapconf_items = from_specification(self._specification,
                                                 self._env_prefix,
                                                 self._separator)
        self._logger = logging.getLogger(__name__)
Example #10
0
def unformatted_bool_item(unformatted_bool_item_spec):
    """A YapconfItem for an unformatted boolean value."""
    # unformatted_item_spec['type'] = 'bool'
    items = from_specification(unformatted_bool_item_spec)
    return items['weirdName-Cool_stuff lol']
Example #11
0
def unformatted_item(unformatted_item_spec):
    """A YapconfItem for an unformatted string value."""
    items = from_specification(unformatted_item_spec)
    return items['weirdName-Cool_stuff lol']
Example #12
0
def bool_item(bool_item_spec):
    """A YacponfItem for a boolean config value."""
    return from_specification(bool_item_spec)['my_bool']
Example #13
0
def simple_item(simple_item_spec):
    """A YacponfItem for a string config value."""
    return from_specification(simple_item_spec)['foo']