]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
python: client: minor modules and code refactoring
authorAleš Mrázek <ales.mrazek@nic.cz>
Fri, 16 Aug 2024 09:06:23 +0000 (11:06 +0200)
committerAleš Mrázek <ales.mrazek@nic.cz>
Fri, 6 Sep 2024 22:28:31 +0000 (00:28 +0200)
python/knot_resolver_manager/client/client.py [moved from python/knot_resolver_manager/client/kresctl.py with 94% similarity]
python/knot_resolver_manager/client/command.py
python/knot_resolver_manager/client/main.py

similarity index 94%
rename from python/knot_resolver_manager/client/kresctl.py
rename to python/knot_resolver_manager/client/client.py
index 1d52564d8438e1fd668c56274ba7abd4aa39a1c0..d97ca65549e3ef0ec89fd9886e7b2f0211dfd9fc 100644 (file)
@@ -2,13 +2,15 @@ import argparse
 
 from knot_resolver_manager.client.command import CommandArgs
 
+KRES_CLIENT_NAME = "kresctl"
 
-class Kresctl:
+
+class KresClient:
     def __init__(
         self,
         namespace: argparse.Namespace,
         parser: argparse.ArgumentParser,
-        prompt: str = "kresctl",
+        prompt: str = KRES_CLIENT_NAME,
     ) -> None:
         self.path = None
         self.prompt = prompt
index 18cf4a2241e955faa175b7c239a224910aab9d3d..6adb0dd9f43a6a546c2c929629682331a522530c 100644 (file)
@@ -66,7 +66,7 @@ def get_socket_from_config(config: Path, optional_file: bool) -> Optional[Socket
 
 
 def determine_socket(namespace: argparse.Namespace) -> SocketDesc:
-    # 1) socket from 'kresctl --socket' argument
+    # 1) socket from '--socket' argument
     if len(namespace.socket) > 0:
         return SocketDesc(namespace.socket[0], "--socket argument")
 
@@ -74,7 +74,7 @@ def determine_socket(namespace: argparse.Namespace) -> SocketDesc:
     socket_env = os.getenv(API_SOCK_ENV_VAR)
 
     socket: Optional[SocketDesc] = None
-    # 2) socket from config file ('kresctl --config' argument)
+    # 2) socket from config file ('--config' argument)
     if len(namespace.config) > 0:
         socket = get_socket_from_config(namespace.config[0], False)
     # 3) socket from config file (environment variable)
index 50e08203fd18b846578842e213f6ef8b9189ccf2..933da54d29a600be8d0c6aae742888e799071b55 100644 (file)
@@ -2,12 +2,12 @@ import argparse
 import importlib
 import os
 
-from knot_resolver_manager.client.command import install_commands_parsers
-from knot_resolver_manager.client.kresctl import Kresctl
+from .command import install_commands_parsers
+from .client import KresClient, KRES_CLIENT_NAME
 
 
-def autoimport_commands() -> None:
-    prefix = "knot_resolver_manager.client.commands."
+def auto_import_commands() -> None:
+    prefix = f"{'.'.join(__name__.split('.')[:-1])}.commands."
     for module_name in os.listdir(os.path.dirname(__file__) + "/commands"):
         if module_name[-3:] != ".py":
             continue
@@ -16,15 +16,15 @@ def autoimport_commands() -> None:
 
 def create_main_argument_parser() -> argparse.ArgumentParser:
     parser = argparse.ArgumentParser(
-        "kresctl",
-        description="Command-line utility that helps communicate with Knot Resolver's management API."
-        "It also provides tooling to work with declarative configuration (validate, convert).",
+        KRES_CLIENT_NAME,
+        description="Knot Resolver command-line utility that serves as a client for communicating with the Knot Resolver management API."
+        " The utility also provides tools to work with the resolver's declarative configuration (validate, convert, ...).",
     )
     # parser.add_argument(
     #     "-i",
     #     "--interactive",
     #     action="store_true",
-    #     help="Interactive mode of kresctl utility",
+    #     help="Use the utility in interactive mode.",
     #     default=False,
     #     required=False,
     # )
@@ -34,8 +34,8 @@ def create_main_argument_parser() -> argparse.ArgumentParser:
         "--socket",
         action="store",
         type=str,
-        help="Optional, path to Unix-domain socket or network interface of the management API. "
-        "Cannot be used together with '--config'.",
+        help="Optional, path to the resolver's management API, unix-domain socket, or network interface."
+        " Cannot be used together with '--config'.",
         default=[],
         nargs=1,
         required=False,
@@ -45,8 +45,8 @@ def create_main_argument_parser() -> argparse.ArgumentParser:
         "--config",
         action="store",
         type=str,
-        help="Optional, path to Knot Resolver declarative configuration to retrieve Unix-domain socket or "
-        "network interface of the management API from. Cannot be used together with '--socket'.",
+        help="Optional, path to the resolver's declarative configuration to retrieve the management API configuration."
+        " Cannot be used together with '--socket'.",
         default=[],
         nargs=1,
         required=False,
@@ -55,15 +55,15 @@ def create_main_argument_parser() -> argparse.ArgumentParser:
 
 
 def main() -> None:
-    autoimport_commands()
+    auto_import_commands()
     parser = create_main_argument_parser()
     install_commands_parsers(parser)
 
     namespace = parser.parse_args()
-    kresctl = Kresctl(namespace, parser)
-    kresctl.execute()
+    client = KresClient(namespace, parser)
+    client.execute()
 
     # if namespace.interactive or len(vars(namespace)) == 2:
-    #     kresctl.interactive()
+    #     client.interactive()
     # else:
-    #     kresctl.execute()
+    #     client.execute()