From d506c9241fbab94e7a82c81679b285d7a0324892 Mon Sep 17 00:00:00 2001 From: Eric Callahan Date: Sat, 13 Jan 2024 10:44:39 -0500 Subject: [PATCH] refactor: convert application into a component Signed-off-by: Eric Callahan --- .../{app.py => components/application.py} | 32 ++++++++++--------- moonraker/components/button.py | 2 +- moonraker/components/machine.py | 2 +- moonraker/components/simplyprint.py | 2 +- moonraker/components/zeroconf.py | 2 +- moonraker/server.py | 5 +-- 6 files changed, 24 insertions(+), 21 deletions(-) rename moonraker/{app.py => components/application.py} (98%) diff --git a/moonraker/app.py b/moonraker/components/application.py similarity index 98% rename from moonraker/app.py rename to moonraker/components/application.py index 5ef8ae2..2d0cff7 100644 --- a/moonraker/app.py +++ b/moonraker/components/application.py @@ -22,8 +22,8 @@ from tornado.escape import url_unescape, url_escape from tornado.routing import Rule, PathMatches, AnyMatches from tornado.http1connection import HTTP1Connection from tornado.log import access_log -from .utils import ServerError, source_info, parse_ip_address -from .common import ( +from ..utils import ServerError, source_info, parse_ip_address +from ..common import ( JsonRPC, WebRequest, APIDefinition, @@ -32,8 +32,8 @@ from .common import ( RequestType, KlippyState ) -from .utils import json_wrapper as jsonw -from .websockets import ( +from ..utils import json_wrapper as jsonw +from ..websockets import ( WebsocketManager, WebSocket, BridgeSocket @@ -55,17 +55,17 @@ from typing import ( ) if TYPE_CHECKING: from tornado.httpserver import HTTPServer - from .server import Server - from .eventloop import EventLoop - from .confighelper import ConfigHelper - from .klippy_connection import KlippyConnection as Klippy - from .utils import IPAddress - from .components.file_manager.file_manager import FileManager - from .components.announcements import Announcements - from .components.machine import Machine + from ..server import Server + from ..eventloop import EventLoop + from ..confighelper import ConfigHelper + from ..klippy_connection import KlippyConnection as Klippy + from ..utils import IPAddress + from .file_manager.file_manager import FileManager + from .announcements import Announcements + from .machine import Machine from io import BufferedReader - from .components.authorization import Authorization - from .components.template import TemplateFactory, JinjaTemplate + from .authorization import Authorization + from .template import TemplateFactory, JinjaTemplate MessageDelgate = Optional[tornado.httputil.HTTPMessageDelegate] AuthComp = Optional[Authorization] APICallback = Callable[[WebRequest], Coroutine] @@ -217,7 +217,6 @@ class MoonrakerApp: self.register_upload_handler("/server/files/upload") # Register Server Components - self.server.register_component("application", self) self.server.register_component("jsonrpc", self.json_rpc) self.server.register_component("internal_transport", self.internal_transport) @@ -1111,3 +1110,6 @@ class WelcomeHandler(tornado.web.RequestHandler): welcome_template = await app.load_template("welcome.html") ret = await welcome_template.render_async(context) self.finish(ret) + +def load_component(config: ConfigHelper) -> MoonrakerApp: + return MoonrakerApp(config) diff --git a/moonraker/components/button.py b/moonraker/components/button.py index 3f5baee..b244228 100644 --- a/moonraker/components/button.py +++ b/moonraker/components/button.py @@ -14,7 +14,7 @@ from typing import ( ) if TYPE_CHECKING: from ..confighelper import ConfigHelper - from ..app import InternalTransport as ITransport + from .application import InternalTransport as ITransport class ButtonManager: diff --git a/moonraker/components/machine.py b/moonraker/components/machine.py index 352e86c..8596bae 100644 --- a/moonraker/components/machine.py +++ b/moonraker/components/machine.py @@ -42,7 +42,7 @@ from typing import ( if TYPE_CHECKING: from ..confighelper import ConfigHelper from ..common import WebRequest - from ..app import MoonrakerApp + from .application import MoonrakerApp from ..klippy_connection import KlippyConnection from .shell_command import ShellCommandFactory as SCMDComp from .database import MoonrakerDatabase diff --git a/moonraker/components/simplyprint.py b/moonraker/components/simplyprint.py index dc0a9b6..efcac77 100644 --- a/moonraker/components/simplyprint.py +++ b/moonraker/components/simplyprint.py @@ -31,7 +31,7 @@ from typing import ( Callable, ) if TYPE_CHECKING: - from ..app import InternalTransport + from .application import InternalTransport from ..confighelper import ConfigHelper from ..websockets import WebsocketManager from ..common import BaseRemoteConnection diff --git a/moonraker/components/zeroconf.py b/moonraker/components/zeroconf.py index 8c9dd82..3844f55 100644 --- a/moonraker/components/zeroconf.py +++ b/moonraker/components/zeroconf.py @@ -29,7 +29,7 @@ from typing import ( if TYPE_CHECKING: from ..confighelper import ConfigHelper from ..common import WebRequest - from ..app import MoonrakerApp + from .application import MoonrakerApp from .machine import Machine ZC_SERVICE_TYPE = "_moonraker._tcp.local." diff --git a/moonraker/server.py b/moonraker/server.py index bd8c4e5..df26d12 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 .app import MoonrakerApp from .klippy_connection import KlippyConnection from .utils import ( ServerError, @@ -50,6 +49,7 @@ from typing import ( ) if TYPE_CHECKING: from .common import WebRequest + from .components.application import MoonrakerApp from .components.file_manager.file_manager import FileManager from .components.machine import Machine from .components.extensions import ExtensionManager @@ -99,7 +99,8 @@ class Server: self.klippy_connection = KlippyConnection(self) # Tornado Application/Server - self.moonraker_app = app = MoonrakerApp(config) + self.moonraker_app: MoonrakerApp = self.load_component(config, "application") + app = self.moonraker_app self.register_endpoint = app.register_endpoint self.register_debug_endpoint = app.register_debug_endpoint self.register_static_file_handler = app.register_static_file_handler