From: Aleš Mrázek Date: Thu, 25 Jul 2024 11:56:08 +0000 (+0200) Subject: manager: compat: dataclasses removed X-Git-Tag: v6.0.9~27^2~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=92bbc27ad0662c71af98281a0d4df915fe599be6;p=thirdparty%2Fknot-resolver.git manager: compat: dataclasses removed --- diff --git a/manager/knot_resolver_manager/compat/__init__.py b/manager/knot_resolver_manager/compat/__init__.py index 410074cda..53993f6ce 100644 --- a/manager/knot_resolver_manager/compat/__init__.py +++ b/manager/knot_resolver_manager/compat/__init__.py @@ -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 index f420b03dc..000000000 --- a/manager/knot_resolver_manager/compat/dataclasses.py +++ /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"] diff --git a/manager/knot_resolver_manager/kresd_controller/supervisord/config_file.py b/manager/knot_resolver_manager/kresd_controller/supervisord/config_file.py index 15a81ba7e..2fb7a99b5 100644 --- a/manager/knot_resolver_manager/kresd_controller/supervisord/config_file.py +++ b/manager/knot_resolver_manager/kresd_controller/supervisord/config_file.py @@ -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 index c402c0923..000000000 --- a/tests/manager/utils/test_dataclasses.py +++ /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)