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 ..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

View File

@ -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]]]]

View File

@ -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

View File

@ -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

View File

@ -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")

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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]]

View File

@ -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:

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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/

View File

@ -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: