From d2533b4eaab378b44e176db0ad9c305c7aa8a820 Mon Sep 17 00:00:00 2001 From: Marco Ceppi Date: Mon, 19 Jun 2023 11:38:32 -0400 Subject: [PATCH] feat: begin re-org to yafti.core --- yafti/__main__.py | 2 +- yafti/abc.py | 2 +- yafti/app.py | 2 +- yafti/cli/__init__.py | 0 yafti/core/__init__.py | 0 yafti/{parser.py => core/config.py} | 43 ++++++++++++++++++++------ yafti/{ => core}/registry.py | 0 yafti/screen/consent.py | 2 +- yafti/screen/package/screen/install.py | 2 +- yafti/screen/title.py | 2 +- yafti/screen/window.py | 2 +- 11 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 yafti/cli/__init__.py create mode 100644 yafti/core/__init__.py rename yafti/{parser.py => core/config.py} (55%) rename yafti/{ => core}/registry.py (100%) diff --git a/yafti/__main__.py b/yafti/__main__.py index e5ba677..a47b1cd 100644 --- a/yafti/__main__.py +++ b/yafti/__main__.py @@ -23,7 +23,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( 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 105539f..a9c91c1 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, YaftSaveState +from yafti.core.config import Config, YaftiRunModes, YaftSaveState 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 55% rename from yafti/parser.py rename to yafti/core/config.py index 0ec6754..ab75577 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): @@ -49,15 +50,39 @@ class YaftiProperties(BaseModel): save_state: YaftSaveState = YaftSaveState.always -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 8a82671..68c04af 100644 --- a/yafti/screen/package/screen/install.py +++ b/yafti/screen/package/screen/install.py @@ -84,7 +84,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 2a78649..5fcc00c 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 = """\