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

changed schema's file to return a file-object

parent d3c5ac6c
No related branches found
No related tags found
No related merge requests found
# v2.15.2
* changed schema's file to return a file-object
......@@ -17,6 +17,8 @@ you should instantiate a Descriptor. Descriptor reflects how your config is nest
.. autoclass:: satella.configuration.schema.File
.. autoclass:: satella.configuration.schema.basic.FileObject
.. autoclass:: satella.configuration.schema.IPv4
.. autoclass:: satella.configuration.schema.List
......
__version__ = '2.15.2a1'
__version__ = '2.15.2a2'
......@@ -5,6 +5,7 @@ import typing as tp
from satella.exceptions import ConfigurationValidationError
from .base import Descriptor, ConfigDictValue
from .registry import register_custom_descriptor
from ...files import read_in_file
@staticmethod
......@@ -53,17 +54,62 @@ class String(Descriptor):
BASIC_MAKER = str
class FileObject:
"""
What you get for values in schema of :class:`~satella.configuration.schema.File`.
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 '<File object %s>' % (self.path, )
def __str__(self):
return self.path
def __eq__(self, other) -> bool:
return self.path == str(other)
def __hash__(self) -> int:
return hash(self.path)
def get_value(self) -> bytes:
"""
Read in the entire file into memory
:return: file contents
"""
return read_in_file(self.path)
def open(self, mode: str):
"""
Open the file in specified mode
:param mode: mode to open the file in
:return: file handle
"""
return open(self.path, mode)
@staticmethod
def _make_file(v: str) -> bool:
if not os.path.isfile(v):
raise ConfigurationValidationError('Expected to find a file under %s'
% (v,))
return FileObject(v)
@register_custom_descriptor('file')
class File(Descriptor):
"""This value must be a valid path to a file"""
"""
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_file
......
......@@ -9,7 +9,7 @@ __all__ = ['read_re_sub_and_write', 'find_files', 'split', 'read_in_file', 'writ
'write_out_file_if_different', 'make_noncolliding_name', 'try_unlink',
'DevNullFilelikeObject', 'read_lines']
from satella.coding import silence_excs
from satella.coding.recast_exceptions import silence_excs
from satella.coding.typing import Predicate
SEPARATORS = {'\\', '/'}
......
......@@ -22,8 +22,9 @@ class TestSchema(unittest.TestCase):
f_out.write(b'test')
s = descriptor_from_dict(schema)
self.assertEqual(s({'key': "test"}), {'key': 'test'})
fo = s({'key': "test"})['key']
self.assertEqual(str(fo), 'test')
self.assertEqual(fo.get_value(), b'test')
def test_caster(self):
ps = Caster(Environment)
......
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