diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b084f01c704801957fa473294859ac045ee955a..fdfd92f149e55282a7b2cbf6f23d58735c559f49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,3 +2,4 @@ * added embedding configuration documentation in schema JSON * this functionality does not require updating satella +* added `strip_afterwards` to config schema file_contents diff --git a/docs/configuration/schema.rst b/docs/configuration/schema.rst index 5fda5cfece6778028da32e317868c21db57423f3..9437cb3cd2008dee2a9d7d39d114a362b5ba8c4d 100644 --- a/docs/configuration/schema.rst +++ b/docs/configuration/schema.rst @@ -182,10 +182,15 @@ You can also provide a commentary for your entries: "contents": { "type": "file_contents", "encoding": "utf-8, - "description": "Encryption key (private key) + "description": "Encryption key (private key)", + "strip_afterwards": True }, "max_workers": { "type": "int", "description": "Maximum parallel instances of service" } } + + +:code:`strip_afterwards` (default is False) strips the content of loaded file of trailing and +leading whitespace. diff --git a/satella/__init__.py b/satella/__init__.py index e8803eec7538a60cf636fc655b72ce8b00989fef..99b05791422a7b27507c102d5453e83d58ba63b4 100644 --- a/satella/__init__.py +++ b/satella/__init__.py @@ -1 +1 @@ -__version__ = '2.16.4a2' +__version__ = '2.16.4' diff --git a/satella/configuration/schema/basic.py b/satella/configuration/schema/basic.py index 26388f3e495880037365903cf203253aa44747df..03464e8a93da95961f0ff70b893e9293b3c08837 100644 --- a/satella/configuration/schema/basic.py +++ b/satella/configuration/schema/basic.py @@ -158,17 +158,22 @@ class FileContents(Descriptor): the contents of this file, applied with encoding (if given). By default, bytes will be read in """ - def __init__(self, encoding: tp.Optional[str] = None): + def __init__(self, encoding: tp.Optional[str] = None, strip_afterwards: bool = False): super().__init__() self.encoding = encoding + self.strip_afterwards = strip_afterwards def BASIC_MAKER(self, c: str): if not self.encoding: with open(c, 'rb') as f_in: - return f_in.read() + y = f_in.read() else: with codecs.open(c, 'r', encoding=self.encoding) as f_in: - return f_in.read() + y = f_in.read() + + if self.strip_afterwards: + y = y.strip() + return y @staticmethod diff --git a/satella/configuration/schema/from_json.py b/satella/configuration/schema/from_json.py index 63d31b7f16d6029586ca223067df5e32e85d51cd..e3b318a3d4f3534d7f588d37af75fa192cc7121c 100644 --- a/satella/configuration/schema/from_json.py +++ b/satella/configuration/schema/from_json.py @@ -47,7 +47,7 @@ def _get_descriptor_for_dict(key: str, value: dict) -> Descriptor: else: args = y, elif type_ == 'file_contents': - args = value.get('encoding', None), + args = value.get('encoding', None), value.get('strip_afterwards', False) elif type_ == 'union': args = [_get_descriptor_for('', x) for x in value.get('of', [])] optional, default = False, None diff --git a/tests/test_configuration/test_schema.py b/tests/test_configuration/test_schema.py index adf466d8d9cfcc8645dd21f589225b55ce734c76..e1cdb81b23a7b0d2c8627f712cb74214eaaaf188 100644 --- a/tests/test_configuration/test_schema.py +++ b/tests/test_configuration/test_schema.py @@ -44,10 +44,13 @@ class TestSchema(unittest.TestCase): def test_file_contents_2(self): schema = { - "key": "file_contents" + "key": { + "type": "file_contents", + "strip_afterwards": True + } } with open('test', 'wb') as f_out: - f_out.write(b'test') + f_out.write(b'test\n') s = descriptor_from_dict(schema) fo = s({'key': "test"})['key']