From 05fb8d73b14553f9e163157808f3c27ae7775d44 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Tue, 25 May 2021 17:49:22 +0200
Subject: [PATCH] added `strip_afterwards` to config schema file_contents,
 v2.16.4

---
 CHANGELOG.md                              |  1 +
 docs/configuration/schema.rst             |  7 ++++++-
 satella/__init__.py                       |  2 +-
 satella/configuration/schema/basic.py     | 11 ++++++++---
 satella/configuration/schema/from_json.py |  2 +-
 tests/test_configuration/test_schema.py   |  7 +++++--
 6 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9b084f01..fdfd92f1 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 5fda5cfe..9437cb3c 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 e8803eec..99b05791 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 26388f3e..03464e8a 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 63d31b7f..e3b318a3 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 adf466d8..e1cdb81b 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']
-- 
GitLab