diff --git a/yafti/__main__.py b/yafti/__main__.py index d249021..497f006 100644 --- a/yafti/__main__.py +++ b/yafti/__main__.py @@ -22,7 +22,7 @@ import yafti.setup # noqa from yafti import log from yafti.app import Yafti -from yafti.parser import Config +from yafti.core.config import Config def run(config: typer.FileText = typer.Argument("/etc/yafti.yml"), debug: bool = False): diff --git a/yafti/abc.py b/yafti/abc.py index e681487..7fa529f 100644 --- a/yafti/abc.py +++ b/yafti/abc.py @@ -26,7 +26,7 @@ class YaftiPlugin: async def _show_screen(condition): - from yafti.registry import PLUGINS + from yafti.core.registry import PLUGINS plugin_name = list(condition.keys())[0] plugin = PLUGINS.get(plugin_name) diff --git a/yafti/app.py b/yafti/app.py index f7fbec7..1fa3725 100644 --- a/yafti/app.py +++ b/yafti/app.py @@ -21,7 +21,7 @@ from gi.repository import Adw from pathlib import Path -from yafti.parser import Config, YaftiRunModes +from yafti.core.config import Config, YaftiRunModes from yafti.screen.window import Window diff --git a/yafti/cli/__init__.py b/yafti/cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/yafti/core/__init__.py b/yafti/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/yafti/parser.py b/yafti/core/config.py similarity index 52% rename from yafti/parser.py rename to yafti/core/config.py index 429eb2b..835980d 100644 --- a/yafti/parser.py +++ b/yafti/core/config.py @@ -16,10 +16,11 @@ from enum import Enum from pathlib import Path -from typing import Optional +from typing import Optional, Any import yaml -from pydantic import BaseModel +from pydantic import BaseModel, BaseSettings +from pydantic.env_settings import SettingsSourceCallable class ActionConfig(BaseModel): @@ -43,15 +44,39 @@ class YaftiProperties(BaseModel): mode: YaftiRunModes = YaftiRunModes.changed -class Config(BaseModel): +class Config(BaseSettings): title: str properties: YaftiProperties = YaftiProperties() actions: Optional[ActionConfig] screens: Optional[dict[str, ScreenConfig]] # Screens are parsed per plugin - -def parse(config_file: str) -> Config: - """Parse the YAML or JSON file passed and return a rendered Config object""" - with open(config_file) as f: - cfg = yaml.safe_load(f) - return Config.parse_obj(cfg) + class Config: + env_prefix = "yafti_" + env_nested_delimiter = "__" + env_file = "/etc/yafti.yml" + + @classmethod + def customise_sources( + cls, + init_settings: SettingsSourceCallable, + env_settings: SettingsSourceCallable, + file_secret_settings: SettingsSourceCallable, + ): + return ( + init_settings, + yaml_config_settings_source, + env_settings, + file_secret_settings, + ) + + +def yaml_config_settings_source(settings: BaseSettings) -> dict[str, Any]: + """ + A simple settings source that loads variables from a JSON file + at the project's root. + + Here we happen to choose to use the `env_file_encoding` from Config + when reading `config.json` + """ + encoding = settings.__config__.env_file_encoding + return yaml.safe_load(Path(settings.__config__.env_file).read_text(encoding)) diff --git a/yafti/registry.py b/yafti/core/registry.py similarity index 100% rename from yafti/registry.py rename to yafti/core/registry.py diff --git a/yafti/screen/consent.py b/yafti/screen/consent.py index b22f466..4275c31 100644 --- a/yafti/screen/consent.py +++ b/yafti/screen/consent.py @@ -39,7 +39,7 @@ import yafti.share from yafti import events from yafti.abc import YaftiScreen, YaftiScreenConfig -from yafti.registry import PLUGINS +from yafti.core.registry import PLUGINS _xml = """\ diff --git a/yafti/screen/package/screen/install.py b/yafti/screen/package/screen/install.py index 02f4ee9..3a3873b 100644 --- a/yafti/screen/package/screen/install.py +++ b/yafti/screen/package/screen/install.py @@ -83,7 +83,7 @@ def __init__( **kwargs, ): super().__init__(**kwargs) - from yafti.registry import PLUGINS + from yafti.core.registry import PLUGINS self.status_page.set_title(title) self.package_manager = PLUGINS.get(package_manager) diff --git a/yafti/screen/title.py b/yafti/screen/title.py index 2d2faaf..20ee381 100644 --- a/yafti/screen/title.py +++ b/yafti/screen/title.py @@ -7,7 +7,7 @@ from yafti import events from yafti.abc import YaftiScreen, YaftiScreenConfig -from yafti.registry import PLUGINS +from yafti.core.registry import PLUGINS _xml = """\ diff --git a/yafti/screen/window.py b/yafti/screen/window.py index cf2627c..7875b00 100644 --- a/yafti/screen/window.py +++ b/yafti/screen/window.py @@ -5,7 +5,7 @@ import yafti.share from yafti import events -from yafti.registry import SCREENS +from yafti.core.registry import SCREENS _xml = """\