]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: utils: modeling: BaseCustomType renamed to BaseValueType
authorAleš Mrázek <ales.mrazek@nic.cz>
Thu, 14 Jul 2022 11:44:11 +0000 (13:44 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Thu, 14 Jul 2022 11:44:11 +0000 (13:44 +0200)
manager/knot_resolver_manager/datamodel/types/base_types.py
manager/knot_resolver_manager/datamodel/types/types.py
manager/knot_resolver_manager/utils/modeling/README.md
manager/knot_resolver_manager/utils/modeling/__init__.py
manager/knot_resolver_manager/utils/modeling/base_schema.py
manager/knot_resolver_manager/utils/modeling/base_value_type.py [moved from manager/knot_resolver_manager/utils/modeling/base_custom_type.py with 93% similarity]

index d6830c83af17c45b9442d9ab980703386b05cea2..9bf784026b48bdb0e93d26d366fad01faf56f5bc 100644 (file)
@@ -1,10 +1,10 @@
 import re
 from typing import Any, Dict, Pattern, Type
 
-from knot_resolver_manager.utils.modeling import BaseCustomType
+from knot_resolver_manager.utils.modeling import BaseValueType
 
 
-class IntBase(BaseCustomType):
+class IntBase(BaseValueType):
     """
     Base class to work with integer value.
     """
@@ -28,7 +28,7 @@ class IntBase(BaseCustomType):
         return {"type": "integer"}
 
 
-class StrBase(BaseCustomType):
+class StrBase(BaseValueType):
     """
     Base class to work with string value.
     """
index 9d00d175413910b1ee3b2568614f7c61af69703b..32d951b02caa1ab265df79b8bb13c38b716bdbd9 100644 (file)
@@ -4,7 +4,7 @@ from pathlib import Path
 from typing import Any, Dict, Optional, Type, Union
 
 from knot_resolver_manager.datamodel.types.base_types import IntRangeBase, PatternBase, StrBase, UnitBase
-from knot_resolver_manager.utils.modeling import BaseCustomType
+from knot_resolver_manager.utils.modeling import BaseValueType
 
 
 class IntNonNegative(IntRangeBase):
@@ -230,7 +230,7 @@ class IPAddressOptionalPort(StrBase):
             )
 
 
-class IPv4Address(BaseCustomType):
+class IPv4Address(BaseValueType):
     _value: ipaddress.IPv4Address
 
     def __init__(self, source_value: Any, object_path: str = "/") -> None:
@@ -272,7 +272,7 @@ class IPv4Address(BaseCustomType):
         }
 
 
-class IPv6Address(BaseCustomType):
+class IPv6Address(BaseValueType):
     _value: ipaddress.IPv6Address
 
     def __init__(self, source_value: Any, object_path: str = "/") -> None:
@@ -317,7 +317,7 @@ class IPv6Address(BaseCustomType):
 IPAddress = Union[IPv4Address, IPv6Address]
 
 
-class IPNetwork(BaseCustomType):
+class IPNetwork(BaseValueType):
     _value: Union[ipaddress.IPv4Network, ipaddress.IPv6Network]
 
     def __init__(self, source_value: Any, object_path: str = "/") -> None:
@@ -352,7 +352,7 @@ class IPNetwork(BaseCustomType):
         }
 
 
-class IPv6Network96(BaseCustomType):
+class IPv6Network96(BaseValueType):
     _value: ipaddress.IPv6Network
 
     def __init__(self, source_value: Any, object_path: str = "/") -> None:
@@ -401,7 +401,7 @@ class IPv6Network96(BaseCustomType):
         return {"type": "string"}
 
 
-class UncheckedPath(BaseCustomType):
+class UncheckedPath(BaseValueType):
     """
     Wrapper around pathlib.Path object. Can represent pretty much any Path. No checks are
     performed on the value. The value is taken as is.
index 62eed4546f53613f320d5de36ea8784f16f59765..ad8a3106efdbb4478e665ce1a70fa3a3ad4bc6e7 100644 (file)
@@ -92,14 +92,14 @@ json_schema = SimpleSchema.json_schema()
 
 ## Creating custom type
 
-Custom types can be made by extending `BaseCustomType` class which is integrated to parsing and validating process.
+Custom types can be made by extending `BaseValueType` class which is integrated to parsing and validating process.
 Use `DataValidationError` to rase exception during validation. `object_path` is used to track node in more complex/nested schemas and create useful logging message.
 
 ```python
-from .modeling import BaseCustomType
+from .modeling import BaseValueType
 from .modeling.exceptions import DataValidationError
 
-class IntNonNegative(BaseCustomType):
+class IntNonNegative(BaseValueType):
     def __init__(self, source_value: Any, object_path: str = "/") -> None:
         super().__init__(source_value)
         if isinstance(source_value, int) and not isinstance(source_value, bool):
index bf3b322f7b99c08facb3685b55a5cd5b9f3bf579..24545f703992a97fa8f5d485ccf83f315fd7ecad 100644 (file)
@@ -1,9 +1,9 @@
-from .base_custom_type import BaseCustomType
 from .base_schema import BaseSchema
+from .base_value_type import BaseValueType
 from .parsing import ParsedTree, parse, parse_json, parse_yaml
 
 __all__ = [
-    "BaseCustomType",
+    "BaseValueType",
     "BaseSchema",
     "ParsedTree",
     "parse",
index 16bfceb1a4ad39308794326fe7d5f953f0704e4c..15821920a46576e58dad81431362f3a12afafff1 100644 (file)
@@ -6,7 +6,7 @@ import yaml
 
 from knot_resolver_manager.utils.functional import all_matches
 
-from .base_custom_type import BaseCustomType
+from .base_value_type import BaseValueType
 from .exceptions import AggregateDataValidationError, DataDescriptionError, DataValidationError
 from .parsing import ParsedTree
 from .types import (
@@ -52,7 +52,7 @@ class Serializable:
             or is_dict(typ)
             or is_list(typ)
             or (inspect.isclass(typ) and issubclass(typ, Serializable))
-            or (inspect.isclass(typ) and issubclass(typ, BaseCustomType))
+            or (inspect.isclass(typ) and issubclass(typ, BaseValueType))
             or (inspect.isclass(typ) and issubclass(typ, BaseSchema))
             or (is_optional(typ) and Serializable.is_serializable(get_optional_inner_type(typ)))
             or (is_union(typ) and all_matches(Serializable.is_serializable, get_generic_type_arguments(typ)))
@@ -63,7 +63,7 @@ class Serializable:
         if isinstance(obj, Serializable):
             return obj.to_dict()
 
-        elif isinstance(obj, BaseCustomType):
+        elif isinstance(obj, BaseValueType):
             return obj.serialize()
 
         elif isinstance(obj, list):
@@ -141,7 +141,7 @@ def _describe_type(typ: Type[Any]) -> Dict[Any, Any]:
     if inspect.isclass(typ) and issubclass(typ, BaseSchema):
         return typ.json_schema(include_schema_definition=False)
 
-    elif inspect.isclass(typ) and issubclass(typ, BaseCustomType):
+    elif inspect.isclass(typ) and issubclass(typ, BaseValueType):
         return typ.json_schema()
 
     elif is_none_type(typ):
@@ -170,10 +170,10 @@ def _describe_type(typ: Type[Any]) -> Dict[Any, Any]:
     elif is_dict(typ):
         key, val = get_generic_type_arguments(typ)
 
-        if inspect.isclass(key) and issubclass(key, BaseCustomType):
+        if inspect.isclass(key) and issubclass(key, BaseValueType):
             assert (
-                key.__str__ is not BaseCustomType.__str__
-            ), "To support derived 'BaseCustomType', __str__ must be implemented."
+                key.__str__ is not BaseValueType.__str__
+            ), "To support derived 'BaseValueType', __str__ must be implemented."
         else:
             assert key == str, "We currently do not support any other keys then strings"
 
@@ -289,15 +289,15 @@ def _validated_object_type(
     # int
     elif cls == int:
         # we don't want to make an int out of anything else than other int
-        # except for BaseCustomType class instances
-        if is_obj_type(obj, int) or isinstance(obj, BaseCustomType):
+        # except for BaseValueType class instances
+        if is_obj_type(obj, int) or isinstance(obj, BaseValueType):
             return int(obj)
         raise DataValidationError(f"expected int, found {type(obj)}", object_path)
 
     # str
     elif cls == str:
         # we are willing to cast any primitive value to string, but no compound values are allowed
-        if is_obj_type(obj, (str, float, int)) or isinstance(obj, BaseCustomType):
+        if is_obj_type(obj, (str, float, int)) or isinstance(obj, BaseValueType):
             return str(obj)
         elif is_obj_type(obj, bool):
             raise DataValidationError(
@@ -358,8 +358,8 @@ def _validated_object_type(
     elif is_obj_type(obj, cls):
         return obj
 
-    # BaseCustomType subclasses
-    elif inspect.isclass(cls) and issubclass(cls, BaseCustomType):
+    # BaseValueType subclasses
+    elif inspect.isclass(cls) and issubclass(cls, BaseValueType):
         if isinstance(obj, cls):
             # if we already have a custom value type, just pass it through
             return obj
similarity index 93%
rename from manager/knot_resolver_manager/utils/modeling/base_custom_type.py
rename to manager/knot_resolver_manager/utils/modeling/base_value_type.py
index f35b011b7a718905c16fc7fefa122b53a7fb4329..b3249752a817a61423833fd99af155f823cf4962 100644 (file)
@@ -1,7 +1,7 @@
 from typing import Any, Dict, Type
 
 
-class BaseCustomType:
+class BaseValueType:
     """
     Subclasses of this class can be used as type annotations in 'DataParser'. When a value
     is being parsed from a serialized format (e.g. JSON/YAML), an object will be created by
@@ -32,5 +32,5 @@ class BaseCustomType:
         raise NotImplementedError(f"{type(self).__name__}'s' 'serialize()' not implemented.")
 
     @classmethod
-    def json_schema(cls: Type["BaseCustomType"]) -> Dict[Any, Any]:
+    def json_schema(cls: Type["BaseValueType"]) -> Dict[Any, Any]:
         raise NotImplementedError()