Skip to content
Snippets Groups Projects
Commit 27a17262 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

v2.16 added Directory

parent 08b5abca
No related branches found
No related tags found
No related merge requests found
# v2.16 # v2.16
* changed semantics of `wrap_future` * changed semantics of `wrap_future`
* added `Directory` to schema configuration
...@@ -17,6 +17,14 @@ you should instantiate a Descriptor. Descriptor reflects how your config is nest ...@@ -17,6 +17,14 @@ you should instantiate a Descriptor. Descriptor reflects how your config is nest
.. autoclass:: satella.configuration.schema.File .. autoclass:: satella.configuration.schema.File
.. autoclass:: satella.configuration.schema.FileObject
:members:
.. autoclass:: satella.configuration.schema.Directory
.. autoclass:: satella.configuration.schema.DirectoryObject
:members:
.. autoclass:: satella.configuration.schema.basic.FileObject .. autoclass:: satella.configuration.schema.basic.FileObject
.. autoclass:: satella.configuration.schema.IPv4 .. autoclass:: satella.configuration.schema.IPv4
...@@ -71,15 +79,17 @@ Note that providing a short-hand, string type is impossible for descriptors that ...@@ -71,15 +79,17 @@ Note that providing a short-hand, string type is impossible for descriptors that
Available string types are: Available string types are:
* **int** - Integer * **int** - :class:`~satella.configuration.schema.Integer`
* **str** - String * **str** - :class:`~satella.configuration.schema.String`
* **list** - List * **list** - :class:`~satella.configuration.schema.List`
* **dict** - Dict * **dict** - :class:`~satella.configuration.schema.Dict`
* **ipv4** - IPv4 * **ipv4** - :class:`~satella.configuration.schema.IPv4`
* **any** - Descriptor * **any** - :class:`~satella.configuration.schema.Descriptor`
* **bool** - Boolean * **bool** - :class:`~satella.configuration.schema.Boolean`
* **union** - Union * **union** - :class:`~satella.configuration.schema.Union`
* **caster** - Caster * **caster** - :class:`~satella.configuration.schema.Caster`
* **file** - :class:`~satella.configuration.schema.File`
* **dir** - :class:`~satella.configuration.schema.Directory`
Lists you define as following Lists you define as following
......
__version__ = '2.16a1' __version__ = '2.16'
from .base import CheckerCondition, Descriptor from .base import CheckerCondition, Descriptor
from .basic import IPv4, Integer, String, Float, Boolean from .basic import IPv4, Integer, String, Float, Boolean, File, Directory, FileObject, DirectoryObject
from .from_json import descriptor_from_dict from .from_json import descriptor_from_dict
from .registry import register_custom_descriptor from .registry import register_custom_descriptor
from .structs import Union, List, Dict, Caster, create_key from .structs import Union, List, Dict, Caster, create_key
__all__ = ['CheckerCondition', 'Descriptor', 'descriptor_from_dict', 'IPv4', 'Integer', __all__ = ['CheckerCondition', 'Descriptor', 'descriptor_from_dict', 'IPv4', 'Integer',
'String', 'Float', 'Boolean', 'Union', 'List', 'Dict', 'Caster', 'String', 'Float', 'Boolean', 'Union', 'List', 'Dict', 'Caster',
'File', 'FileObject', 'DirectoryObject', 'Directory',
'register_custom_descriptor', 'create_key'] 'register_custom_descriptor', 'create_key']
...@@ -71,7 +71,7 @@ class FileObject: ...@@ -71,7 +71,7 @@ class FileObject:
return self.path return self.path
def __eq__(self, other) -> bool: def __eq__(self, other) -> bool:
return self.path == str(other) return self.path == str(other) and isinstance(other, FileObject)
def __hash__(self) -> int: def __hash__(self) -> int:
return hash(self.path) return hash(self.path)
...@@ -95,6 +95,37 @@ class FileObject: ...@@ -95,6 +95,37 @@ class FileObject:
return open(self.path, mode) return open(self.path, mode)
class DirectoryObject:
"""
What you get for values in schema of :class:`~satella.configuration.schema.Directory`.
This object is comparable and hashable, and is equal to the string of it's path
"""
__slots__ = 'path',
def __init__(self, path: str):
self.path = path
def __repr__(self):
return '<Directory object %s>' % (self.path, )
def __str__(self):
return self.path
def __eq__(self, other) -> bool:
return self.path == str(other) and isinstance(other, DirectoryObject)
def __hash__(self) -> int:
return hash(self.path)
def get_files(self) -> tp.Iterable[str]:
"""
Return a list of files inside this directory
:return:
"""
return os.listdir(self.path)
@staticmethod @staticmethod
def _make_file(v: str) -> bool: def _make_file(v: str) -> bool:
...@@ -114,6 +145,25 @@ class File(Descriptor): ...@@ -114,6 +145,25 @@ class File(Descriptor):
BASIC_MAKER = _make_file BASIC_MAKER = _make_file
@staticmethod
def _make_directory(v: str) -> bool:
if not os.path.isdir(v):
raise ConfigurationValidationError('Expected to find a directory under %s'
% (v,))
return DirectoryObject(v)
@register_custom_descriptor('dir')
class Directory(Descriptor):
"""
This value must be a valid path to a file. The value in your schema will be
an instance of :class:`~satella.configuration.schema.basic.FileObject`
"""
BASIC_MAKER = _make_directory
class Regexp(String): class Regexp(String):
""" """
Base class for declaring regexp-based descriptors. Overload it's attribute REGEXP. Use as Base class for declaring regexp-based descriptors. Overload it's attribute REGEXP. Use as
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment