Repository: markusressel/container-app-conf

Issue:
Regex config entry
Add config entry type for specifying regexes.

Discussion:

Patch:
diff --git a/container_app_conf/entry/float.py b/container_app_conf/entry/float.py
index ee85223..6ddb2ca 100644
--- a/container_app_conf/entry/float.py
+++ b/container_app_conf/entry/float.py
@@ -17,7 +17,6 @@
 #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 #  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 #  SOFTWARE.
-from py_range_parse import Range
 
 from container_app_conf import ConfigEntry
 
@@ -25,11 +24,6 @@ from container_app_conf import ConfigEntry
 class FloatConfigEntry(ConfigEntry):
     _example = "3.1415926535"
 
-    def __init__(self, key_path: [str], example: any = None, description: str or None = None, default: any = None,
-                 none_allowed: bool = None, range: Range = None):
-        self.range = range
-        super().__init__(key_path, example, description, default, none_allowed)
-
     def _value_to_type(self, value: any) -> float or None:
         """
         Tries to permissively convert the given value to a float.
@@ -37,13 +31,9 @@ class FloatConfigEntry(ConfigEntry):
         :return: the parsed float value
         """
         if isinstance(value, str) and '%' == value[-1]:
-            parsed_value = float(value[:-1]) / 100.0
+            return float(value[:-1]) / 100.0
         else:
-            parsed_value = float(value)
-
-        if self.range is not None and parsed_value not in self.range:
-            self._raise_invalid_value(value)
-        return parsed_value
+            return float(value)
 
     def _type_to_value(self, type: any) -> any:
         return float(type)
diff --git a/container_app_conf/entry/int.py b/container_app_conf/entry/int.py
index beb6ad1..3e50310 100644
--- a/container_app_conf/entry/int.py
+++ b/container_app_conf/entry/int.py
@@ -17,7 +17,6 @@
 #  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 #  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 #  SOFTWARE.
-from py_range_parse import Range
 
 from container_app_conf import ConfigEntry
 
@@ -25,21 +24,13 @@ from container_app_conf import ConfigEntry
 class IntConfigEntry(ConfigEntry):
     _example = "42"
 
-    def __init__(self, key_path: [str], example: any = None, description: str or None = None, default: any = None,
-                 none_allowed: bool = None, range: Range = None):
-        self.range = range
-        super().__init__(key_path, example, description, default, none_allowed)
-
     def _value_to_type(self, value: any) -> int or None:
         """
         Tries to permissively convert the given value to an int.
         :param value: the value to parse
         :return: the parsed int value
         """
-        parsed_value = int(value)
-        if self.range is not None and parsed_value not in self.range:
-            self._raise_invalid_value(value)
-        return parsed_value
+        return int(value)
 
     def _type_to_value(self, type: any) -> any:
         return int(type)
diff --git a/container_app_conf/entry/regex.py b/container_app_conf/entry/regex.py
new file mode 100644
index 0000000..193178d
--- /dev/null
+++ b/container_app_conf/entry/regex.py
@@ -0,0 +1,41 @@
+#  Copyright (c) 2019 Markus Ressel
+#  .
+#  Permission is hereby granted, free of charge, to any person obtaining a copy
+#  of this software and associated documentation files (the "Software"), to deal
+#  in the Software without restriction, including without limitation the rights
+#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+#  copies of the Software, and to permit persons to whom the Software is
+#  furnished to do so, subject to the following conditions:
+#  .
+#  The above copyright notice and this permission notice shall be included in all
+#  copies or substantial portions of the Software.
+#  .
+#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+#  SOFTWARE.
+
+import copy
+import re
+
+from container_app_conf.entry.string import StringConfigEntry
+
+# workaround for deepcopy bug in python<=3.6
+# see: https://stackoverflow.com/questions/6279305/typeerror-cannot-deepcopy-this-pattern-object/56935186#56935186
+copy._deepcopy_dispatch[type(re.compile(''))] = lambda r, _: r
+
+
+class RegexConfigEntry(StringConfigEntry):
+    _example = r"^[a-zA-z0-9]*$"
+
+    def _value_to_type(self, value: any) -> str or None:
+        s = super()._value_to_type(value)
+        if s is None and self._none_allowed:
+            return None
+        return re.compile(s)
+
+    def _type_to_value(self, type: any) -> str:
+        return type.pattern


Test patch:
diff --git a/tests/__init__.py b/tests/__init__.py
index 7c63ea1..e75440a 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -31,6 +31,7 @@ from container_app_conf.entry.float import FloatConfigEntry
 from container_app_conf.entry.int import IntConfigEntry
 from container_app_conf.entry.list import ListConfigEntry
 from container_app_conf.entry.range import RangeConfigEntry
+from container_app_conf.entry.regex import RegexConfigEntry
 from container_app_conf.entry.string import StringConfigEntry
 from container_app_conf.entry.timedelta import TimeDeltaConfigEntry
 
@@ -52,6 +53,10 @@ class TestConfigBase(ConfigBase):
         key_path=["test", "string"],
         default="default value"
     )
+    REGEX = RegexConfigEntry(
+        key_path=["test", "regex"],
+        default="^[a-zA-Z0-9]$"
+    )
     INT = IntConfigEntry(
         key_path=["test", "int"],
         default=100
diff --git a/tests/entry_test.py b/tests/entry_test.py
index 456e6d6..bb4d877 100644
--- a/tests/entry_test.py
+++ b/tests/entry_test.py
@@ -28,12 +28,37 @@ from container_app_conf.entry.file import FileConfigEntry, DirectoryConfigEntry
 from container_app_conf.entry.float import FloatConfigEntry
 from container_app_conf.entry.int import IntConfigEntry
 from container_app_conf.entry.range import RangeConfigEntry
+from container_app_conf.entry.regex import RegexConfigEntry
+from container_app_conf.entry.string import StringConfigEntry
 from container_app_conf.entry.timedelta import TimeDeltaConfigEntry
 from tests import EntryTestBase
 
 
 class EntryTest(EntryTestBase):
 
+    def test_string_entry(self):
+        config_entry = StringConfigEntry(key_path=["string"], none_allowed=True)
+
+        input_output = [
+            ("5", "5"),
+            ("hello", "hello"),
+            ("$stuff=)(&/%$§", "$stuff=)(&/%$§"),
+            ("None", None)
+        ]
+
+        self.assert_input_output(config_entry, input_output)
+
+    def test_regex_entry(self):
+        config_entry = RegexConfigEntry(key_path=["regex"], none_allowed=True)
+
+        import re
+        input_output = [
+            ("$[0-9]*", re.compile("$[0-9]*")),
+            ("None", None)
+        ]
+
+        self.assert_input_output(config_entry, input_output)
+
     def test_bool_entry(self):
         config_entry = BoolConfigEntry(key_path=["bool"])
 
@@ -53,19 +78,18 @@ class EntryTest(EntryTestBase):
         self.assert_input_output(config_entry, input_output)
 
     def test_int_entry(self):
-        config_entry = IntConfigEntry(key_path=["int"], range=Range(-5, 5))
+        config_entry = IntConfigEntry(key_path=["int"])
         input_output = [
             ("5", 5),
             (5, 5),
             ("-3", -3),
-            (-3, -3),
-            (-6, ValueError)
+            (-3, -3)
         ]
 
         self.assert_input_output(config_entry, input_output)
 
     def test_float_entry(self):
-        config_entry = FloatConfigEntry(key_path=["float"], range=Range(-10.0, 10))
+        config_entry = FloatConfigEntry(key_path=["float"])
         input_output = [
             ("5", 5.0),
             (5, 5.0),
@@ -73,8 +97,7 @@ class EntryTest(EntryTestBase):
             (-3.0, -3.0),
             (1.2, 1.2),
             ("1.2", 1.2),
-            ("3%", 0.03),
-            (-20.0, ValueError)
+            ("3%", 0.03)
         ]
 
         self.assert_input_output(config_entry, input_output)