]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
manager: compat: dataclasses removed
authorAleš Mrázek <ales.mrazek@nic.cz>
Thu, 25 Jul 2024 11:56:08 +0000 (13:56 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Mon, 26 Aug 2024 12:28:10 +0000 (14:28 +0200)
manager/knot_resolver_manager/compat/__init__.py
manager/knot_resolver_manager/compat/dataclasses.py [deleted file]
manager/knot_resolver_manager/kresd_controller/supervisord/config_file.py
tests/manager/utils/test_dataclasses.py [deleted file]

index 410074cda76e5903d76f577733427bf0c0e6a113..53993f6cedc914e11f79246e858328af52e74ea9 100644 (file)
@@ -1,3 +1,3 @@
-from . import asyncio, dataclasses
+from . import asyncio
 
-__all__ = ["asyncio", "dataclasses"]
+__all__ = ["asyncio"]
diff --git a/manager/knot_resolver_manager/compat/dataclasses.py b/manager/knot_resolver_manager/compat/dataclasses.py
deleted file mode 100644 (file)
index f420b03..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
-"""
-This module contains rather simplistic reimplementation of dataclasses due to them being unsupported on Python 3.6
-"""
-
-from typing import Any, Dict, Set, Type
-
-dataclasses_import_success = False
-try:
-    import dataclasses
-
-    dataclasses_import_success = True
-except ImportError:
-    pass
-
-
-_CUSTOM_DATACLASS_MARKER = "_CUSTOM_DATACLASS_MARKER"
-
-
-def dataclass(cls: Any) -> Any:
-    if dataclasses_import_success:
-        return dataclasses.dataclass(cls)
-
-    anot: Dict[str, Type[Any]] = cls.__dict__.get("__annotations__", {})
-
-    def ninit(slf: Any, *args: Any, **kwargs: Any) -> None:
-        nonlocal anot
-
-        ianot = iter(anot.keys())
-        used: Set[str] = set()
-
-        # set normal arguments
-        for arg in args:
-            name = next(ianot)
-            setattr(slf, name, arg)
-            used.add(name)
-
-        # set keyd arguments
-        for key, val in kwargs.items():
-            assert key in anot, (
-                f"Constructing dataclass with an argument '{key}' which is not defined with a type"
-                f" annotation in class {cls.__name__}"
-            )
-            setattr(slf, key, val)
-            used.add(key)
-
-        # set default values
-        for key in anot:
-            if key in used:
-                continue
-            assert hasattr(
-                cls, key
-            ), f"Field '{key}' does not have default value and was not defined in the constructor"
-            dfl = getattr(cls, key)
-            setattr(slf, key, dfl)
-
-    setattr(cls, "__init__", ninit)
-    setattr(cls, _CUSTOM_DATACLASS_MARKER, ...)
-    return cls
-
-
-def is_dataclass(obj: Any) -> bool:
-    if dataclasses_import_success:
-        return dataclasses.is_dataclass(obj)
-
-    return hasattr(obj, _CUSTOM_DATACLASS_MARKER)
-
-
-__all__ = ["dataclass", "is_dataclass"]
index 15a81ba7e700a556883d2c60832d294e05b51946..2fb7a99b5fbcbfb4884ff38757ecbf2ddd3e36c2 100644 (file)
@@ -1,10 +1,11 @@
 import logging
 import os
 
+from dataclasses import dataclass
+from pathlib import Path
 from jinja2 import Template
 from typing_extensions import Literal
 
-from knot_resolver_manager.compat.dataclasses import dataclass
 from knot_resolver_manager.constants import (
     kres_gc_executable,
     kresd_cache_dir,
@@ -83,7 +84,7 @@ class ProcessTypeConfig:
     Data structure holding data for supervisord config template
     """
 
-    logfile: str
+    logfile: Path
     workdir: str
     command: str
     environment: str
@@ -139,16 +140,16 @@ class ProcessTypeConfig:
             workdir=user_constants().working_directory_on_startup,
             command=cmd,
             environment="X-SUPERVISORD-TYPE=notify",
-            logfile="",  # this will be ignored
+            logfile=Path(""),  # this will be ignored
         )
 
 
 @dataclass
 class SupervisordConfig:
-    unix_http_server: str
-    pid_file: str
+    unix_http_server: Path
+    pid_file: Path
     workdir: str
-    logfile: str
+    logfile: Path
     loglevel: Literal["critical", "error", "warn", "info", "debug", "trace", "blather"]
     target: LogTargetEnum
 
@@ -166,14 +167,13 @@ class SupervisordConfig:
                 "info": "info",
                 "debug": "debug",
             }[config.logging.level]
-
         cwd = str(os.getcwd())
         return SupervisordConfig(  # type: ignore[call-arg]
             unix_http_server=supervisord_sock_file(config),
             pid_file=supervisord_pid_file(config),
             workdir=cwd,
-            logfile="syslog" if config.logging.target == "syslog" else "/dev/null",
-            loglevel=loglevel,
+            logfile=Path("syslog" if config.logging.target == "syslog" else "/dev/null"),
+            loglevel=loglevel,  # type: ignore[arg-type]
             target=config.logging.target,
         )
 
diff --git a/tests/manager/utils/test_dataclasses.py b/tests/manager/utils/test_dataclasses.py
deleted file mode 100644 (file)
index c402c09..0000000
+++ /dev/null
@@ -1,15 +0,0 @@
-from knot_resolver_manager.compat.dataclasses import dataclass, is_dataclass
-
-
-def test_dataclass():
-    @dataclass
-    class A:
-        b: int = 5
-
-    val = A(6)
-    assert val.b == 6
-
-    val = A(b=7)
-    assert val.b == 7
-
-    assert is_dataclass(A)