refactor: refactor OctoEverywhere to dataclass

Signed-off-by: Dominik Willner <th33xitus@gmail.com>
This commit is contained in:
dw-0
2024-08-04 18:44:45 +02:00
parent acde067e68
commit 1fc50848b0

View File

@@ -6,10 +6,11 @@
# # # #
# This file may be distributed under the terms of the GNU GPLv3 license # # This file may be distributed under the terms of the GNU GPLv3 license #
# ======================================================================= # # ======================================================================= #
from __future__ import annotations
from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from subprocess import CalledProcessError, run from subprocess import CalledProcessError, run
from typing import List
from components.moonraker import MOONRAKER_CFG_NAME from components.moonraker import MOONRAKER_CFG_NAME
from components.octoeverywhere import ( from components.octoeverywhere import (
@@ -26,32 +27,23 @@ from core.instance_manager.base_instance import BaseInstance
from utils.logger import Logger from utils.logger import Logger
# todo: make this to a dataclass @dataclass
class Octoeverywhere(BaseInstance): class Octoeverywhere(BaseInstance):
@classmethod dir: Path = OE_DIR
def blacklist(cls) -> List[str]: env_dir: Path = OE_ENV_DIR
return ["None", "mcu", "bambu", "companion"] store_dir: Path = OE_STORE_DIR
cfg_file: Path | None = None
sys_cfg_file: Path | None = None
log: Path | None = None
def __init__(self, suffix: str = ""): def __init__(self, suffix: str = ""):
super().__init__(instance_type=self, suffix=suffix) super().__init__(suffix=suffix)
self.dir: Path = OE_DIR
self.env_dir: Path = OE_ENV_DIR
self.store_dir: Path = OE_STORE_DIR
self._cfg_file = self.cfg_dir.joinpath(OE_CFG_NAME)
self._sys_cfg_file = self.cfg_dir.joinpath(OE_SYS_CFG_NAME)
self._log = self.log_dir.joinpath(OE_LOG_NAME)
@property def __post_init__(self):
def cfg_file(self) -> Path: super().__post_init__()
return self._cfg_file self.cfg_file = self.cfg_dir.joinpath(OE_CFG_NAME)
self.sys_cfg_file = self.cfg_dir.joinpath(OE_SYS_CFG_NAME)
@property self.log = self.log_dir.joinpath(OE_LOG_NAME)
def sys_cfg_file(self) -> Path:
return self._sys_cfg_file
@property
def log(self) -> Path:
return self._log
def create(self) -> None: def create(self) -> None:
Logger.print_status("Creating OctoEverywhere for Klipper Instance ...") Logger.print_status("Creating OctoEverywhere for Klipper Instance ...")
@@ -74,15 +66,15 @@ class Octoeverywhere(BaseInstance):
raise raise
def delete(self) -> None: def delete(self) -> None:
service_file = self.get_service_file_name(extension=True) service_file: str = self.get_service_file_name(extension=True)
service_file_path = self.get_service_file_path() service_file_path: Path = self.get_service_file_path()
Logger.print_status( Logger.print_status(
f"Deleting OctoEverywhere for Klipper Instance: {service_file}" f"Deleting OctoEverywhere for Klipper Instance: {service_file}"
) )
try: try:
command = ["sudo", "rm", "-f", service_file_path] command = ["sudo", "rm", "-f", service_file_path.as_posix()]
run(command, check=True) run(command, check=True)
self.delete_logfiles(OE_LOG_NAME) self.delete_logfiles(OE_LOG_NAME)
Logger.print_ok(f"Service file deleted: {service_file_path}") Logger.print_ok(f"Service file deleted: {service_file_path}")