예제 #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'
예제 #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'})])
예제 #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 = {}
예제 #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')
예제 #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']
예제 #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']
예제 #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']
예제 #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']
예제 #9
0
파일: spec.py 프로젝트: marcinpohl/yapconf
    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__)
예제 #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']
예제 #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']
예제 #12
0
def bool_item(bool_item_spec):
    """A YacponfItem for a boolean config value."""
    return from_specification(bool_item_spec)['my_bool']
예제 #13
0
def simple_item(simple_item_spec):
    """A YacponfItem for a string config value."""
    return from_specification(simple_item_spec)['foo']