diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f1b634836b552abe2fc44b5692cfde71257bca5..40c4b772c478a6fe6ae122a6e0e6e9f24841f5b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,3 @@ # v2.16.1 + +* **bugfix release for 2.16** diff --git a/satella/__init__.py b/satella/__init__.py index 0c7c65f62ffaf561f56c7f5de87231703096cad4..37a2f4fdb778456e40324f2b3c5fcf6b089775bc 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.16.1a1' +__version__ = '2.16.1' diff --git a/satella/configuration/schema/from_json.py b/satella/configuration/schema/from_json.py index fd61daeba79f319c46f2b7132f1f4e07cfc001f2..edcef8f01f3502bb6a9830233cde59048e70722e 100644 --- a/satella/configuration/schema/from_json.py +++ b/satella/configuration/schema/from_json.py @@ -3,13 +3,13 @@ import typing as tp from satella.exceptions import ConfigurationSchemaError from satella.imports import import_class from .base import Descriptor -from .registry import BASE_LOOKUP_TABLE +from .registry import BASE_LOOKUP_TABLE, PLAIN_ENTRIES from .structs import create_key, Dict def _get_descriptor_for_str(key: str, value: str) -> Descriptor: try: - if value in ('int', 'float', 'str', 'ipv4', 'any', 'bool', 'file'): + if value in PLAIN_ENTRIES: return create_key(BASE_LOOKUP_TABLE[value](), key, False, None) except KeyError: diff --git a/satella/configuration/schema/registry.py b/satella/configuration/schema/registry.py index 8d200198df02867ffbb8f26c5622dbc168b5900a..7e98a2b9a667ca787fc324ba72563d2188b30821 100644 --- a/satella/configuration/schema/registry.py +++ b/satella/configuration/schema/registry.py @@ -1,7 +1,8 @@ BASE_LOOKUP_TABLE = {} +PLAIN_ENTRIES = set() -def register_custom_descriptor(name: str): +def register_custom_descriptor(name: str, is_plain: bool = True): """ A decorator used for registering custom descriptors in order to be loadable via descriptor_from_dict @@ -13,9 +14,13 @@ def register_custom_descriptor(name: str): >>> REGEXP = '(([0-9a-f]{1,4}:)' ... :param name: under which it is supposed to be invokable + :param is_plain: is this a nested structure? """ def inner(cls): + global BASE_LOOKUP_TABLE, PLAIN_ENTRIES + if is_plain: + PLAIN_ENTRIES.add(name) BASE_LOOKUP_TABLE[name] = cls return cls diff --git a/satella/configuration/schema/structs.py b/satella/configuration/schema/structs.py index ac6883c4017dd6c268dcf325157864640a66fa7e..7d55518e6eebc8b88f781c9fc59e1d8ecdd5b828 100644 --- a/satella/configuration/schema/structs.py +++ b/satella/configuration/schema/structs.py @@ -7,7 +7,7 @@ from .base import Descriptor, must_be_type, ConfigDictValue from .registry import register_custom_descriptor -@register_custom_descriptor('list') +@register_custom_descriptor('list', is_plain=False) class List(Descriptor): """ This must be a list, made of entries of a descriptor (this is optional) @@ -42,7 +42,7 @@ def create_key(descriptor: Descriptor, name: str, optional: bool = False, return descriptor -@register_custom_descriptor('caster') +@register_custom_descriptor('caster', is_plain=False) class Caster(Descriptor): """ A value must be ran through a function. @@ -62,7 +62,7 @@ class Caster(Descriptor): return self.to_cast(value) -@register_custom_descriptor('dict') +@register_custom_descriptor('dict', is_plain=False) class Dict(Descriptor): """ This entry must be a dict, having at least specified keys. @@ -124,7 +124,7 @@ class Dict(Descriptor): return output -@register_custom_descriptor('union') +@register_custom_descriptor('union', is_plain=False) class Union(Descriptor): """ The type of one of the child descriptors. If posed as such: diff --git a/tests/test_configuration/test_schema.py b/tests/test_configuration/test_schema.py index 4a2d624ec8304f812a5fe022c1c4254ed2a86d79..90755d8bb535eefe2c7c66187bbbe7201e31c5b3 100644 --- a/tests/test_configuration/test_schema.py +++ b/tests/test_configuration/test_schema.py @@ -1,5 +1,6 @@ import enum import os +import shutil import tempfile import unittest @@ -30,6 +31,19 @@ class TestSchema(unittest.TestCase): ps = Caster(Environment) self.assertEqual(ps(0), Environment.PRODUCTION) + def test_directory(self): + schema = { + "directory": "dir" + } + + if os.path.exists('directory'): + shutil.rmtree('directory') + + s = descriptor_from_dict(schema) + self.assertRaises(ConfigurationValidationError, lambda: s({'directory': 'directory'})) + os.mkdir('directory') + self.assertEqual(s({'directory': 'directory'})['directory'], DirectoryObject('directory')) + def test_descriptor_from_schema_caster(self): schema = { "key": {