]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
compat: module expansion and refactoring - included dataclasses
authorVasek Sraier <git@vakabus.cz>
Tue, 23 Mar 2021 18:50:20 +0000 (19:50 +0100)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 8 Apr 2022 14:17:51 +0000 (16:17 +0200)
manager/knot_resolver_manager/compat/__init__.py [new file with mode: 0644]
manager/knot_resolver_manager/compat/asyncio.py [moved from manager/knot_resolver_manager/compat.py with 88% similarity]
manager/knot_resolver_manager/compat/dataclasses.py [new file with mode: 0644]
manager/knot_resolver_manager/datamodel.py
manager/knot_resolver_manager/kresd_manager.py

diff --git a/manager/knot_resolver_manager/compat/__init__.py b/manager/knot_resolver_manager/compat/__init__.py
new file mode 100644 (file)
index 0000000..2f41e53
--- /dev/null
@@ -0,0 +1,4 @@
+from . import asyncio
+
+
+__all__ = ["asyncio"]
similarity index 88%
rename from manager/knot_resolver_manager/compat.py
rename to manager/knot_resolver_manager/compat/asyncio.py
index 3a1482a172063b4a8fe830c503615c2a838d183e..331e180ba3f877242f5436f813f18826fceb8c10 100644 (file)
@@ -7,7 +7,7 @@ import functools
 from typing import Awaitable, Coroutine
 
 
-def asyncio_to_thread(func, *args, **kwargs) -> Awaitable:
+def to_thread(func, *args, **kwargs) -> Awaitable:
     # version 3.9 and higher, call directly
     if sys.version_info.major >= 3 and sys.version_info.minor >= 9:
         return asyncio.to_thread(func, *args, **kwargs)
@@ -19,7 +19,7 @@ def asyncio_to_thread(func, *args, **kwargs) -> Awaitable:
         return loop.run_in_executor(None, pfunc)
 
 
-def asyncio_create_task(coro: Coroutine, name=None) -> Future:
+def create_task(coro: Coroutine, name=None) -> Future:
     # version 3.8 and higher, call directly
     if sys.version_info.major >= 3 and sys.version_info.minor >= 8:
         return asyncio.create_task(coro, name=name)
@@ -33,7 +33,7 @@ def asyncio_create_task(coro: Coroutine, name=None) -> Future:
         return asyncio.ensure_future(coro)
 
 
-def asyncio_run(coro: Coroutine, debug=None) -> Awaitable:
+def run(coro: Coroutine, debug=None) -> Awaitable:
     # ideally copy-paste of this:
     # https://github.com/python/cpython/blob/3.9/Lib/asyncio/runners.py#L8
 
diff --git a/manager/knot_resolver_manager/compat/dataclasses.py b/manager/knot_resolver_manager/compat/dataclasses.py
new file mode 100644 (file)
index 0000000..403141d
--- /dev/null
@@ -0,0 +1,13 @@
+"""
+This is a compat module that we will use with dataclasses
+due to them being unsupported on Python 3.6. However, a proper backport exists.
+This module is simply a reimport of that backported library (or the system one),
+so that if we have to vendor that library or do something similar with it, we have
+the option to do it transparently, without changing anything else.
+"""
+
+
+from dataclasses import dataclass
+
+
+__all__ = ["dataclass"]
index 32cc06f0d11ade91cd3a13af088ac8cb443297da..728dbfd68c46540e733a6d1a15e61b33c6720164 100644 (file)
@@ -1,6 +1,6 @@
-from dataclasses import dataclass
 from typing import Optional
 
+from .compat.dataclasses import dataclass
 from .utils import StrictyamlParser
 
 
index d4c5f9b3a4fd3ea97d57b3cdcd24be7973649aa2..dfcc8b7dcc75fed8225c540c8e47b419a36be276 100644 (file)
@@ -19,13 +19,13 @@ class Kresd:
         raise NotImplementedError()
 
     async def start(self):
-        await compat.asyncio_to_thread(systemd.start_unit, f"kresd@{self._id}.service")
+        await compat.asyncio.to_thread(systemd.start_unit, f"kresd@{self._id}.service")
 
     async def stop(self):
-        await compat.asyncio_to_thread(systemd.stop_unit, f"kresd@{self._id}.service")
+        await compat.asyncio.to_thread(systemd.stop_unit, f"kresd@{self._id}.service")
 
     async def restart(self):
-        await compat.asyncio_to_thread(
+        await compat.asyncio.to_thread(
             systemd.restart_unit, f"kresd@{self._id}.service"
         )
 
@@ -55,7 +55,7 @@ class KresdManager:
         await kresd.stop()
 
     async def _collect_already_running_children(self):
-        units = await compat.asyncio_to_thread(systemd.list_units)
+        units = await compat.asyncio.to_thread(systemd.list_units)
         for unit in units:
             u: str = unit
             if u.startswith("kresd@") and u.endswith(".service"):