refactor: convert klippy_connection into a component

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Eric Callahan 2024-01-13 12:02:33 -05:00
parent a88468eb79
commit ddd735feba
No known key found for this signature in database
GPG Key ID: 5A1EB336DFB4C71B
16 changed files with 41 additions and 40 deletions

View File

@ -54,7 +54,7 @@ if TYPE_CHECKING:
from ..server import Server from ..server import Server
from ..eventloop import EventLoop from ..eventloop import EventLoop
from ..confighelper import ConfigHelper from ..confighelper import ConfigHelper
from ..klippy_connection import KlippyConnection as Klippy from .klippy_connection import KlippyConnection as Klippy
from ..utils import IPAddress from ..utils import IPAddress
from .websockets import WebsocketManager from .websockets import WebsocketManager
from .file_manager.file_manager import FileManager from .file_manager.file_manager import FileManager

View File

@ -22,7 +22,7 @@ from typing import (
if TYPE_CHECKING: if TYPE_CHECKING:
from ..confighelper import ConfigHelper from ..confighelper import ConfigHelper
from ..common import WebRequest from ..common import WebRequest
from ..klippy_connection import KlippyConnection from .klippy_connection import KlippyConnection
from .klippy_apis import KlippyAPI as APIComp from .klippy_apis import KlippyAPI as APIComp
GCQueue = Deque[Dict[str, Any]] GCQueue = Deque[Dict[str, Any]]
TempStore = Dict[str, Dict[str, Deque[Optional[float]]]] TempStore = Dict[str, Dict[str, Deque[Optional[float]]]]

View File

@ -24,7 +24,7 @@ if TYPE_CHECKING:
from ..server import Server from ..server import Server
from ..confighelper import ConfigHelper from ..confighelper import ConfigHelper
from ..common import WebRequest from ..common import WebRequest
from ..klippy_connection import KlippyConnection as Klippy from .klippy_connection import KlippyConnection as Klippy
UNIX_BUFFER_LIMIT = 20 * 1024 * 1024 UNIX_BUFFER_LIMIT = 20 * 1024 * 1024

View File

@ -44,7 +44,7 @@ if TYPE_CHECKING:
from inotify_simple import Event as InotifyEvent from inotify_simple import Event as InotifyEvent
from ...confighelper import ConfigHelper from ...confighelper import ConfigHelper
from ...common import WebRequest from ...common import WebRequest
from ...klippy_connection import KlippyConnection from ..klippy_connection import KlippyConnection
from .. import database from .. import database
from .. import klippy_apis from .. import klippy_apis
from .. import shell_command from .. import shell_command

View File

@ -23,7 +23,7 @@ from typing import (
) )
if TYPE_CHECKING: if TYPE_CHECKING:
from ..confighelper import ConfigHelper from ..confighelper import ConfigHelper
from ..klippy_connection import KlippyConnection as Klippy from .klippy_connection import KlippyConnection as Klippy
Subscription = Dict[str, Optional[List[Any]]] Subscription = Dict[str, Optional[List[Any]]]
SubCallback = Callable[[Dict[str, Dict[str, Any]], float], Optional[Coroutine]] SubCallback = Callable[[Dict[str, Dict[str, Any]], float], Optional[Coroutine]]
_T = TypeVar("_T") _T = TypeVar("_T")

View File

@ -12,9 +12,9 @@ import logging
import getpass import getpass
import asyncio import asyncio
import pathlib import pathlib
from .utils import ServerError, get_unix_peer_credentials from ..utils import ServerError, get_unix_peer_credentials
from .utils import json_wrapper as jsonw from ..utils import json_wrapper as jsonw
from .common import KlippyState, RequestType from ..common import KlippyState, RequestType
# Annotation imports # Annotation imports
from typing import ( from typing import (
@ -31,14 +31,13 @@ from typing import (
Union Union
) )
if TYPE_CHECKING: if TYPE_CHECKING:
from .server import Server from ..common import WebRequest, APITransport, BaseRemoteConnection
from .common import WebRequest, APITransport, BaseRemoteConnection from ..confighelper import ConfigHelper
from .confighelper import ConfigHelper from .klippy_apis import KlippyAPI
from .components.klippy_apis import KlippyAPI from .file_manager.file_manager import FileManager
from .components.file_manager.file_manager import FileManager from .machine import Machine
from .components.machine import Machine from .job_state import JobState
from .components.job_state import JobState from .database import MoonrakerDatabase as Database
from .components.database import MoonrakerDatabase as Database
FlexCallback = Callable[..., Optional[Coroutine]] FlexCallback = Callable[..., Optional[Coroutine]]
Subscription = Dict[str, Optional[List[str]]] Subscription = Dict[str, Optional[List[str]]]
@ -57,9 +56,11 @@ UNIX_BUFFER_LIMIT = 20 * 1024 * 1024
SVC_INFO_KEY = "klippy_connection.service_info" SVC_INFO_KEY = "klippy_connection.service_info"
class KlippyConnection: class KlippyConnection:
def __init__(self, server: Server) -> None: def __init__(self, config: ConfigHelper) -> None:
self.server = server self.server = config.get_server()
self.uds_address = pathlib.Path("/tmp/klippy_uds") self.uds_address = config.getpath(
"klippy_uds_address", pathlib.Path("/tmp/klippy_uds")
)
self.writer: Optional[asyncio.StreamWriter] = None self.writer: Optional[asyncio.StreamWriter] = None
self.connection_mutex: asyncio.Lock = asyncio.Lock() self.connection_mutex: asyncio.Lock = asyncio.Lock()
self.event_loop = self.server.get_event_loop() self.event_loop = self.server.get_event_loop()
@ -94,12 +95,6 @@ class KlippyConnection:
self.register_remote_method( self.register_remote_method(
'process_status_update', self._process_status_update, 'process_status_update', self._process_status_update,
need_klippy_reg=False) 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 @property
def klippy_apis(self) -> KlippyAPI: def klippy_apis(self) -> KlippyAPI:
@ -789,3 +784,6 @@ class KlippyRequest:
'method': self.rpc_method, 'method': self.rpc_method,
'params': self.params 'params': self.params
} }
def load_component(config: ConfigHelper) -> KlippyConnection:
return KlippyConnection(config)

View File

@ -43,7 +43,7 @@ if TYPE_CHECKING:
from ..confighelper import ConfigHelper from ..confighelper import ConfigHelper
from ..common import WebRequest from ..common import WebRequest
from .application import MoonrakerApp from .application import MoonrakerApp
from ..klippy_connection import KlippyConnection from .klippy_connection import KlippyConnection
from .shell_command import ShellCommandFactory as SCMDComp from .shell_command import ShellCommandFactory as SCMDComp
from .database import MoonrakerDatabase from .database import MoonrakerDatabase
from .file_manager.file_manager import FileManager from .file_manager.file_manager import FileManager

View File

@ -16,7 +16,7 @@ from typing import (
List, List,
) )
if TYPE_CHECKING: if TYPE_CHECKING:
from ..klippy_connection import KlippyConnection from .klippy_connection import KlippyConnection
from ..confighelper import ConfigHelper from ..confighelper import ConfigHelper
from ..common import WebRequest from ..common import WebRequest
from .klippy_apis import KlippyAPI as APIComp from .klippy_apis import KlippyAPI as APIComp

View File

@ -29,7 +29,7 @@ from typing import (
) )
if TYPE_CHECKING: if TYPE_CHECKING:
from ..confighelper import ConfigHelper from ..confighelper import ConfigHelper
from ..klippy_connection import KlippyConnection from .klippy_connection import KlippyConnection
from .klippy_apis import KlippyAPI as APIComp from .klippy_apis import KlippyAPI as APIComp
from .file_manager.file_manager import FileManager as FMComp from .file_manager.file_manager import FileManager as FMComp
FlexCallback = Callable[..., Optional[Coroutine]] FlexCallback = Callable[..., Optional[Coroutine]]

View File

@ -35,7 +35,7 @@ if TYPE_CHECKING:
from .mqtt import MQTTClient from .mqtt import MQTTClient
from .template import JinjaTemplate from .template import JinjaTemplate
from .http_client import HttpClient from .http_client import HttpClient
from klippy_connection import KlippyConnection from .klippy_connection import KlippyConnection
class PrinterPower: class PrinterPower:
def __init__(self, config: ConfigHelper) -> None: def __init__(self, config: ConfigHelper) -> None:

View File

@ -45,7 +45,7 @@ if TYPE_CHECKING:
from .power import PrinterPower from .power import PrinterPower
from .announcements import Announcements from .announcements import Announcements
from .webcam import WebcamManager, WebCam from .webcam import WebcamManager, WebCam
from ..klippy_connection import KlippyConnection from .klippy_connection import KlippyConnection
COMPONENT_VERSION = "0.0.1" COMPONENT_VERSION = "0.0.1"
SP_VERSION = "0.1" SP_VERSION = "0.1"

View File

@ -30,7 +30,7 @@ from typing import (
) )
if TYPE_CHECKING: if TYPE_CHECKING:
from ...confighelper import ConfigHelper from ...confighelper import ConfigHelper
from ...klippy_connection import KlippyConnection as Klippy from ..klippy_connection import KlippyConnection as Klippy
from .update_manager import CommandHelper from .update_manager import CommandHelper
from ..machine import Machine from ..machine import Machine
from ..file_manager.file_manager import FileManager from ..file_manager.file_manager import FileManager

View File

@ -37,7 +37,7 @@ if TYPE_CHECKING:
from ...server import Server from ...server import Server
from ...confighelper import ConfigHelper from ...confighelper import ConfigHelper
from ...common import WebRequest from ...common import WebRequest
from ...klippy_connection import KlippyConnection from ..klippy_connection import KlippyConnection
from ..shell_command import ShellCommandFactory as SCMDComp from ..shell_command import ShellCommandFactory as SCMDComp
from ..database import MoonrakerDatabase as DBComp from ..database import MoonrakerDatabase as DBComp
from ..database import NamespaceWrapper from ..database import NamespaceWrapper

View File

@ -32,7 +32,7 @@ from typing import (
if TYPE_CHECKING: if TYPE_CHECKING:
from ..server import Server from ..server import Server
from ..klippy_connection import KlippyConnection as Klippy from .klippy_connection import KlippyConnection as Klippy
from ..confighelper import ConfigHelper from ..confighelper import ConfigHelper
from .extensions import ExtensionManager from .extensions import ExtensionManager
from .authorization import Authorization from .authorization import Authorization

View File

@ -27,7 +27,7 @@ from typing import (
if TYPE_CHECKING: if TYPE_CHECKING:
from .server import Server from .server import Server
from .common import WebRequest from .common import WebRequest
from .klippy_connection import KlippyConnection from .components.klippy_connection import KlippyConnection
# Coroutine friendly QueueHandler courtesy of Martjin Pieters: # Coroutine friendly QueueHandler courtesy of Martjin Pieters:
# https://www.zopatista.com/python/2019/05/11/asyncio-logging/ # https://www.zopatista.com/python/2019/05/11/asyncio-logging/

View File

@ -21,7 +21,6 @@ import uuid
import traceback import traceback
from . import confighelper from . import confighelper
from .eventloop import EventLoop from .eventloop import EventLoop
from .klippy_connection import KlippyConnection
from .utils import ( from .utils import (
ServerError, ServerError,
Sentinel, Sentinel,
@ -50,6 +49,7 @@ if TYPE_CHECKING:
from .common import WebRequest from .common import WebRequest
from .components.application import MoonrakerApp from .components.application import MoonrakerApp
from .components.websockets import WebsocketManager from .components.websockets import WebsocketManager
from .components.klippy_connection import KlippyConnection
from .components.file_manager.file_manager import FileManager from .components.file_manager.file_manager import FileManager
from .components.machine import Machine from .components.machine import Machine
from .components.extensions import ExtensionManager from .components.extensions import ExtensionManager
@ -57,6 +57,7 @@ if TYPE_CHECKING:
_T = TypeVar("_T", Sentinel, Any) _T = TypeVar("_T", Sentinel, Any)
API_VERSION = (1, 4, 0) API_VERSION = (1, 4, 0)
SERVER_COMPONENTS = ['application', 'websockets', 'klippy_connection']
CORE_COMPONENTS = [ CORE_COMPONENTS = [
'dbus_manager', 'database', 'file_manager', 'klippy_apis', 'dbus_manager', 'database', 'file_manager', 'klippy_apis',
'machine', 'data_store', 'shell_command', 'proc_stats', 'machine', 'data_store', 'shell_command', 'proc_stats',
@ -96,7 +97,8 @@ class Server:
log_level = logging.DEBUG if args["verbose"] else logging.INFO log_level = logging.DEBUG if args["verbose"] else logging.INFO
logging.getLogger().setLevel(log_level) logging.getLogger().setLevel(log_level)
self.event_loop.set_debug(args["asyncio_debug"]) 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 # Tornado Application/Server
self.moonraker_app: MoonrakerApp = self.load_component(config, "application") self.moonraker_app: MoonrakerApp = self.load_component(config, "application")
@ -258,7 +260,6 @@ class Server:
for section in cfg_sections: for section in cfg_sections:
self.load_component(config, section, None) self.load_component(config, section, None)
self.klippy_connection.configure(config)
config.validate_config() config.validate_config()
self._is_configured = True self._is_configured = True
@ -281,6 +282,8 @@ class Server:
try: try:
full_name = f"moonraker.components.{component_name}" full_name = f"moonraker.components.{component_name}"
module = importlib.import_module(full_name) module = importlib.import_module(full_name)
# Server components use the [server] section for configuration
if component_name not in SERVER_COMPONENTS:
is_core = component_name in CORE_COMPONENTS is_core = component_name in CORE_COMPONENTS
fallback: Optional[str] = "server" if is_core else None fallback: Optional[str] = "server" if is_core else None
config = config.getsection(component_name, fallback) config = config.getsection(component_name, fallback)