From ddd735feba70b38f5064b558456bd3ae2eda7e73 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Sat, 13 Jan 2024 12:02:33 -0500 Subject: [PATCH] refactor: convert klippy_connection into a component Signed-off-by: Eric Callahan --- moonraker/components/application.py | 2 +- moonraker/components/data_store.py | 2 +- moonraker/components/extensions.py | 2 +- .../components/file_manager/file_manager.py | 2 +- moonraker/components/klippy_apis.py | 2 +- .../{ => components}/klippy_connection.py | 38 +++++++++---------- moonraker/components/machine.py | 2 +- moonraker/components/octoprint_compat.py | 2 +- moonraker/components/paneldue.py | 2 +- moonraker/components/power.py | 2 +- moonraker/components/simplyprint.py | 2 +- .../components/update_manager/app_deploy.py | 2 +- .../update_manager/update_manager.py | 2 +- moonraker/components/websockets.py | 2 +- moonraker/loghelper.py | 2 +- moonraker/server.py | 15 +++++--- 16 files changed, 41 insertions(+), 40 deletions(-) rename moonraker/{ => components}/klippy_connection.py (97%) diff --git a/moonraker/components/application.py b/moonraker/components/application.py index 4fd93c2..9c45d68 100644 --- a/moonraker/components/application.py +++ b/moonraker/components/application.py @@ -54,7 +54,7 @@ if TYPE_CHECKING: from ..server import Server from ..eventloop import EventLoop from ..confighelper import ConfigHelper - from ..klippy_connection import KlippyConnection as Klippy + from .klippy_connection import KlippyConnection as Klippy from ..utils import IPAddress from .websockets import WebsocketManager from .file_manager.file_manager import FileManager diff --git a/moonraker/components/data_store.py b/moonraker/components/data_store.py index b45f162..739cf38 100644 --- a/moonraker/components/data_store.py +++ b/moonraker/components/data_store.py @@ -22,7 +22,7 @@ from typing import ( if TYPE_CHECKING: from ..confighelper import ConfigHelper from ..common import WebRequest - from ..klippy_connection import KlippyConnection + from .klippy_connection import KlippyConnection from .klippy_apis import KlippyAPI as APIComp GCQueue = Deque[Dict[str, Any]] TempStore = Dict[str, Dict[str, Deque[Optional[float]]]] diff --git a/moonraker/components/extensions.py b/moonraker/components/extensions.py index 3031c79..cd74a9a 100644 --- a/moonraker/components/extensions.py +++ b/moonraker/components/extensions.py @@ -24,7 +24,7 @@ if TYPE_CHECKING: from ..server import Server from ..confighelper import ConfigHelper from ..common import WebRequest - from ..klippy_connection import KlippyConnection as Klippy + from .klippy_connection import KlippyConnection as Klippy UNIX_BUFFER_LIMIT = 20 * 1024 * 1024 diff --git a/moonraker/components/file_manager/file_manager.py b/moonraker/components/file_manager/file_manager.py index 5d8161c..cfebac7 100644 --- a/moonraker/components/file_manager/file_manager.py +++ b/moonraker/components/file_manager/file_manager.py @@ -44,7 +44,7 @@ if TYPE_CHECKING: from inotify_simple import Event as InotifyEvent from ...confighelper import ConfigHelper from ...common import WebRequest - from ...klippy_connection import KlippyConnection + from ..klippy_connection import KlippyConnection from .. import database from .. import klippy_apis from .. import shell_command diff --git a/moonraker/components/klippy_apis.py b/moonraker/components/klippy_apis.py index 88cd4ad..7836944 100644 --- a/moonraker/components/klippy_apis.py +++ b/moonraker/components/klippy_apis.py @@ -23,7 +23,7 @@ from typing import ( ) if TYPE_CHECKING: from ..confighelper import ConfigHelper - from ..klippy_connection import KlippyConnection as Klippy + from .klippy_connection import KlippyConnection as Klippy Subscription = Dict[str, Optional[List[Any]]] SubCallback = Callable[[Dict[str, Dict[str, Any]], float], Optional[Coroutine]] _T = TypeVar("_T") diff --git a/moonraker/klippy_connection.py b/moonraker/components/klippy_connection.py similarity index 97% rename from moonraker/klippy_connection.py rename to moonraker/components/klippy_connection.py index 93737c1..9867730 100644 --- a/moonraker/klippy_connection.py +++ b/moonraker/components/klippy_connection.py @@ -12,9 +12,9 @@ import logging import getpass import asyncio import pathlib -from .utils import ServerError, get_unix_peer_credentials -from .utils import json_wrapper as jsonw -from .common import KlippyState, RequestType +from ..utils import ServerError, get_unix_peer_credentials +from ..utils import json_wrapper as jsonw +from ..common import KlippyState, RequestType # Annotation imports from typing import ( @@ -31,14 +31,13 @@ from typing import ( Union ) if TYPE_CHECKING: - from .server import Server - from .common import WebRequest, APITransport, BaseRemoteConnection - from .confighelper import ConfigHelper - from .components.klippy_apis import KlippyAPI - from .components.file_manager.file_manager import FileManager - from .components.machine import Machine - from .components.job_state import JobState - from .components.database import MoonrakerDatabase as Database + from ..common import WebRequest, APITransport, BaseRemoteConnection + from ..confighelper import ConfigHelper + from .klippy_apis import KlippyAPI + from .file_manager.file_manager import FileManager + from .machine import Machine + from .job_state import JobState + from .database import MoonrakerDatabase as Database FlexCallback = Callable[..., Optional[Coroutine]] Subscription = Dict[str, Optional[List[str]]] @@ -57,9 +56,11 @@ UNIX_BUFFER_LIMIT = 20 * 1024 * 1024 SVC_INFO_KEY = "klippy_connection.service_info" class KlippyConnection: - def __init__(self, server: Server) -> None: - self.server = server - self.uds_address = pathlib.Path("/tmp/klippy_uds") + def __init__(self, config: ConfigHelper) -> None: + self.server = config.get_server() + self.uds_address = config.getpath( + "klippy_uds_address", pathlib.Path("/tmp/klippy_uds") + ) self.writer: Optional[asyncio.StreamWriter] = None self.connection_mutex: asyncio.Lock = asyncio.Lock() self.event_loop = self.server.get_event_loop() @@ -94,12 +95,6 @@ class KlippyConnection: self.register_remote_method( 'process_status_update', self._process_status_update, need_klippy_reg=False) - self.server.register_component("klippy_connection", self) - - def configure(self, config: ConfigHelper): - self.uds_address = config.getpath( - "klippy_uds_address", self.uds_address - ) @property def klippy_apis(self) -> KlippyAPI: @@ -789,3 +784,6 @@ class KlippyRequest: 'method': self.rpc_method, 'params': self.params } + +def load_component(config: ConfigHelper) -> KlippyConnection: + return KlippyConnection(config) diff --git a/moonraker/components/machine.py b/moonraker/components/machine.py index 8596bae..967be3d 100644 --- a/moonraker/components/machine.py +++ b/moonraker/components/machine.py @@ -43,7 +43,7 @@ if TYPE_CHECKING: from ..confighelper import ConfigHelper from ..common import WebRequest from .application import MoonrakerApp - from ..klippy_connection import KlippyConnection + from .klippy_connection import KlippyConnection from .shell_command import ShellCommandFactory as SCMDComp from .database import MoonrakerDatabase from .file_manager.file_manager import FileManager diff --git a/moonraker/components/octoprint_compat.py b/moonraker/components/octoprint_compat.py index 8d77dd1..fca83c7 100644 --- a/moonraker/components/octoprint_compat.py +++ b/moonraker/components/octoprint_compat.py @@ -16,7 +16,7 @@ from typing import ( List, ) if TYPE_CHECKING: - from ..klippy_connection import KlippyConnection + from .klippy_connection import KlippyConnection from ..confighelper import ConfigHelper from ..common import WebRequest from .klippy_apis import KlippyAPI as APIComp diff --git a/moonraker/components/paneldue.py b/moonraker/components/paneldue.py index 662511f..d28d4c5 100644 --- a/moonraker/components/paneldue.py +++ b/moonraker/components/paneldue.py @@ -29,7 +29,7 @@ from typing import ( ) if TYPE_CHECKING: from ..confighelper import ConfigHelper - from ..klippy_connection import KlippyConnection + from .klippy_connection import KlippyConnection from .klippy_apis import KlippyAPI as APIComp from .file_manager.file_manager import FileManager as FMComp FlexCallback = Callable[..., Optional[Coroutine]] diff --git a/moonraker/components/power.py b/moonraker/components/power.py index ad9c42e..ee53b9a 100644 --- a/moonraker/components/power.py +++ b/moonraker/components/power.py @@ -35,7 +35,7 @@ if TYPE_CHECKING: from .mqtt import MQTTClient from .template import JinjaTemplate from .http_client import HttpClient - from klippy_connection import KlippyConnection + from .klippy_connection import KlippyConnection class PrinterPower: def __init__(self, config: ConfigHelper) -> None: diff --git a/moonraker/components/simplyprint.py b/moonraker/components/simplyprint.py index f9862f2..58fd91f 100644 --- a/moonraker/components/simplyprint.py +++ b/moonraker/components/simplyprint.py @@ -45,7 +45,7 @@ if TYPE_CHECKING: from .power import PrinterPower from .announcements import Announcements from .webcam import WebcamManager, WebCam - from ..klippy_connection import KlippyConnection + from .klippy_connection import KlippyConnection COMPONENT_VERSION = "0.0.1" SP_VERSION = "0.1" diff --git a/moonraker/components/update_manager/app_deploy.py b/moonraker/components/update_manager/app_deploy.py index 0e45224..5b8c341 100644 --- a/moonraker/components/update_manager/app_deploy.py +++ b/moonraker/components/update_manager/app_deploy.py @@ -30,7 +30,7 @@ from typing import ( ) if TYPE_CHECKING: from ...confighelper import ConfigHelper - from ...klippy_connection import KlippyConnection as Klippy + from ..klippy_connection import KlippyConnection as Klippy from .update_manager import CommandHelper from ..machine import Machine from ..file_manager.file_manager import FileManager diff --git a/moonraker/components/update_manager/update_manager.py b/moonraker/components/update_manager/update_manager.py index 559b5d8..3be9ed9 100644 --- a/moonraker/components/update_manager/update_manager.py +++ b/moonraker/components/update_manager/update_manager.py @@ -37,7 +37,7 @@ if TYPE_CHECKING: from ...server import Server from ...confighelper import ConfigHelper from ...common import WebRequest - from ...klippy_connection import KlippyConnection + from ..klippy_connection import KlippyConnection from ..shell_command import ShellCommandFactory as SCMDComp from ..database import MoonrakerDatabase as DBComp from ..database import NamespaceWrapper diff --git a/moonraker/components/websockets.py b/moonraker/components/websockets.py index 9d61a22..f83a467 100644 --- a/moonraker/components/websockets.py +++ b/moonraker/components/websockets.py @@ -32,7 +32,7 @@ from typing import ( if TYPE_CHECKING: from ..server import Server - from ..klippy_connection import KlippyConnection as Klippy + from .klippy_connection import KlippyConnection as Klippy from ..confighelper import ConfigHelper from .extensions import ExtensionManager from .authorization import Authorization diff --git a/moonraker/loghelper.py b/moonraker/loghelper.py index fd93656..c2809a0 100644 --- a/moonraker/loghelper.py +++ b/moonraker/loghelper.py @@ -27,7 +27,7 @@ from typing import ( if TYPE_CHECKING: from .server import Server from .common import WebRequest - from .klippy_connection import KlippyConnection + from .components.klippy_connection import KlippyConnection # Coroutine friendly QueueHandler courtesy of Martjin Pieters: # https://www.zopatista.com/python/2019/05/11/asyncio-logging/ diff --git a/moonraker/server.py b/moonraker/server.py index 89f8c29..7f911fe 100755 --- a/moonraker/server.py +++ b/moonraker/server.py @@ -21,7 +21,6 @@ import uuid import traceback from . import confighelper from .eventloop import EventLoop -from .klippy_connection import KlippyConnection from .utils import ( ServerError, Sentinel, @@ -50,6 +49,7 @@ if TYPE_CHECKING: from .common import WebRequest from .components.application import MoonrakerApp from .components.websockets import WebsocketManager + from .components.klippy_connection import KlippyConnection from .components.file_manager.file_manager import FileManager from .components.machine import Machine from .components.extensions import ExtensionManager @@ -57,6 +57,7 @@ if TYPE_CHECKING: _T = TypeVar("_T", Sentinel, Any) API_VERSION = (1, 4, 0) +SERVER_COMPONENTS = ['application', 'websockets', 'klippy_connection'] CORE_COMPONENTS = [ 'dbus_manager', 'database', 'file_manager', 'klippy_apis', 'machine', 'data_store', 'shell_command', 'proc_stats', @@ -96,7 +97,8 @@ class Server: log_level = logging.DEBUG if args["verbose"] else logging.INFO logging.getLogger().setLevel(log_level) self.event_loop.set_debug(args["asyncio_debug"]) - self.klippy_connection = KlippyConnection(self) + self.klippy_connection: KlippyConnection + self.klippy_connection = self.load_component(config, "klippy_connection") # Tornado Application/Server self.moonraker_app: MoonrakerApp = self.load_component(config, "application") @@ -258,7 +260,6 @@ class Server: for section in cfg_sections: self.load_component(config, section, None) - self.klippy_connection.configure(config) config.validate_config() self._is_configured = True @@ -281,9 +282,11 @@ class Server: try: full_name = f"moonraker.components.{component_name}" module = importlib.import_module(full_name) - is_core = component_name in CORE_COMPONENTS - fallback: Optional[str] = "server" if is_core else None - config = config.getsection(component_name, fallback) + # Server components use the [server] section for configuration + if component_name not in SERVER_COMPONENTS: + is_core = component_name in CORE_COMPONENTS + fallback: Optional[str] = "server" if is_core else None + config = config.getsection(component_name, fallback) load_func = getattr(module, "load_component") component = load_func(config) except Exception as e: