mirror of
https://github.com/Cian-H/I-Form_Server_Node_Deployer.git
synced 2025-12-22 22:22:02 +00:00
Cleaned up lazy/messy config implementation
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
def main() -> None:
|
||||
"""Entry point for the CLI
|
||||
"""
|
||||
from . import config
|
||||
from .config import config
|
||||
config.update_config("cli")
|
||||
from .node_deployer import app
|
||||
app()
|
||||
@@ -11,7 +11,7 @@ def main() -> None:
|
||||
def debug() -> None:
|
||||
"""Entry point for the debug CLI
|
||||
"""
|
||||
from . import config
|
||||
from .config import config
|
||||
config.update_config("debug")
|
||||
from .node_deployer import app
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ from selenium.webdriver.support import expected_conditions as EC
|
||||
from selenium.webdriver.support.ui import WebDriverWait
|
||||
import typer
|
||||
|
||||
from . import config
|
||||
from .config import config
|
||||
from .cli import cli_spinner
|
||||
from .debug import debug_guard
|
||||
from .utils import ensure_build_dir
|
||||
|
||||
@@ -4,7 +4,7 @@ from typing import Callable
|
||||
|
||||
from rich.progress import Progress, SpinnerColumn, TextColumn
|
||||
|
||||
from . import config
|
||||
from .config import config
|
||||
from .utils import Singleton
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
# flake8: noqa: F821
|
||||
# type: ignore
|
||||
#* This file sets a number of config constants by modifying its own globals
|
||||
#* As a result, F821 and typing is disabled as the interpreter cannot be
|
||||
#* trusted to know when F821 or UndefinedVeriable errors should be raised.
|
||||
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
import docker
|
||||
import tomllib
|
||||
@@ -17,6 +12,25 @@ PROJECT_ROOT: Path = Path(__file__).parent.parent.parent.absolute()
|
||||
type ConfigLabel = str | list[str]
|
||||
|
||||
|
||||
class Config(SimpleNamespace):
|
||||
def __init__(self, config_label: ConfigLabel, **kwargs) -> None:
|
||||
"""Initialises the configuration object
|
||||
|
||||
Args:
|
||||
config_label (ConfigLabel): The configuration to initialise with
|
||||
**kwargs: Additional keyword arguments to become attributes
|
||||
"""
|
||||
self.__dict__.update(self.get_config(config_label))
|
||||
self.update_config()
|
||||
_kwargs = {
|
||||
"CLIENT": CLIENT,
|
||||
"MAX_PORT": MAX_PORT,
|
||||
"PROJECT_ROOT": PROJECT_ROOT,
|
||||
}
|
||||
_kwargs.update(kwargs)
|
||||
super().__init__(**_kwargs)
|
||||
|
||||
@staticmethod
|
||||
def get_config(config_label: ConfigLabel = "default") -> dict:
|
||||
"""Gets the specified configuration from config.toml
|
||||
|
||||
@@ -37,8 +51,7 @@ def get_config(config_label: ConfigLabel = "default") -> dict:
|
||||
out_config.update(configs[c])
|
||||
return out_config
|
||||
|
||||
|
||||
def finalise_config(config: dict) -> None:
|
||||
def finalise_config(self, config: dict) -> None:
|
||||
"""Finalises the configuration by converting paths to Path objects and
|
||||
appropriately setting secondary parameters such as relative paths
|
||||
|
||||
@@ -53,36 +66,30 @@ def finalise_config(config: dict) -> None:
|
||||
case "CWD_MOUNTDIR":
|
||||
config[k] = Path(v)
|
||||
# Then, get required paths from config or globals if not present
|
||||
build_dir = config.get("BUILD_DIR", BUILD_DIR)
|
||||
cwd_mountdir = config.get("CWD_MOUNTDIR", CWD_MOUNTDIR)
|
||||
src_dir = config.get("SRC_DIR", SRC_DIR)
|
||||
build_dir = config.get("BUILD_DIR", self.BUILD_DIR)
|
||||
cwd_mountdir = config.get("CWD_MOUNTDIR", self.CWD_MOUNTDIR)
|
||||
src_dir = config.get("SRC_DIR", self.SRC_DIR)
|
||||
# Finally, construct the secondary parameters
|
||||
config["FUELIGNITION_BUILD_DIR"] = build_dir / config.get(
|
||||
"FUELIGNITION_BUILD_DIR",
|
||||
FUELIGNITION_BUILD_DIR
|
||||
)
|
||||
config["DOCKERFILE_DIR"] = src_dir / config.get(
|
||||
"DOCKERFILE_DIR",
|
||||
DOCKERFILE_DIR
|
||||
"FUELIGNITION_BUILD_DIR", self.FUELIGNITION_BUILD_DIR
|
||||
)
|
||||
config["DOCKERFILE_DIR"] = src_dir / config.get("DOCKERFILE_DIR", self.DOCKERFILE_DIR)
|
||||
config["CWD_MOUNT"] = docker.types.Mount(
|
||||
target=str(cwd_mountdir),
|
||||
source=str(PROJECT_ROOT),
|
||||
type="bind",
|
||||
)
|
||||
|
||||
|
||||
def apply_config(config: dict) -> None:
|
||||
"""Applies the specified configuration to this module's globals
|
||||
def apply_config(self, config: dict) -> None:
|
||||
"""Applies the specified configuration to this object's attributes
|
||||
|
||||
Args:
|
||||
config (dict): The configuration to apply
|
||||
"""
|
||||
finalise_config(config)
|
||||
globals().update(config)
|
||||
self.finalise_config(config)
|
||||
self.__dict__.update(config)
|
||||
|
||||
|
||||
def update_config(config_label: ConfigLabel = "default") -> None:
|
||||
def update_config(self, config_label: ConfigLabel = "default") -> None:
|
||||
"""Updates the configuration to the specified configuration
|
||||
|
||||
Args:
|
||||
@@ -90,17 +97,7 @@ def update_config(config_label: ConfigLabel = "default") -> None:
|
||||
The label of the configuration to update to.
|
||||
Defaults to "default".
|
||||
"""
|
||||
apply_config(get_config(config_label))
|
||||
self.apply_config(self.get_config(config_label))
|
||||
|
||||
|
||||
def init(config_label: ConfigLabel) -> None:
|
||||
"""Initialises the configuration module
|
||||
|
||||
Args:
|
||||
config_label (ConfigLabel): The configuration to initialise with
|
||||
"""
|
||||
globals().update(get_config(config_label))
|
||||
update_config()
|
||||
|
||||
|
||||
init(config_label="default")
|
||||
config = Config(config_label="default")
|
||||
|
||||
@@ -6,7 +6,7 @@ from docker.types import Mount
|
||||
import typer
|
||||
from typing import Tuple
|
||||
|
||||
from . import config
|
||||
from .config import config
|
||||
from .cli import cli_spinner
|
||||
from .create_img import create_img
|
||||
from .debug import debug_guard
|
||||
|
||||
@@ -5,7 +5,7 @@ from typing import Annotated
|
||||
|
||||
import typer
|
||||
|
||||
from . import config
|
||||
from .config import config
|
||||
from .autoignition import json_to_img
|
||||
from .cli import cli_spinner
|
||||
from .debug import debug_guard
|
||||
|
||||
@@ -4,7 +4,7 @@ from typing import Callable
|
||||
|
||||
import typer
|
||||
|
||||
from . import config
|
||||
from .config import config
|
||||
|
||||
|
||||
def debug_guard(f: Callable) -> Callable:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import typer
|
||||
|
||||
from . import config
|
||||
from .config import config
|
||||
from .autoignition import json_to_img
|
||||
from .create_disk import create_ignition_disk
|
||||
from .create_img import create_img
|
||||
|
||||
@@ -2,7 +2,7 @@ from functools import wraps
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
from . import config
|
||||
from .config import config
|
||||
|
||||
|
||||
def ensure_build_dir(f: Callable) -> Callable:
|
||||
|
||||
Reference in New Issue
Block a user