From ee6227a5c5dd840a490d65e6ac3025c9a0234024 Mon Sep 17 00:00:00 2001 From: WhatDidYouExpect <89535984+WhatDidYouExpect@users.noreply.github.com> Date: Wed, 23 Jul 2025 15:46:48 +0200 Subject: [PATCH] updated 2 search for a settings.json too --- main.py | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 4b62144..ec1edad 100644 --- a/main.py +++ b/main.py @@ -50,12 +50,50 @@ def find_dotenv(start_path: pathlib.Path) -> pathlib.Path | None: current = current.parent return None -env_path = find_dotenv(pathlib.Path(__file__).parent) -if env_path: - load_dotenv(dotenv_path=env_path) - print(f"[VOLTA] {GREEN}Loaded .env from {env_path}{RESET}") +def load_settings_json() -> dict | None: + start_path = working_dir.resolve() + current = start_path.resolve() + while current != current.parent: + candidate = current / "settings.json" + if candidate.exists(): + try: + with open(candidate, "r", encoding="utf-8") as f: + data = json.load(f) + print(f"[VOLTA] {GREEN}Loaded settings.json locale '{data.get('locale')}' from {candidate}{RESET}") + return data + except Exception as e: + print(f"[VOLTA] {RED}Failed to load settings.json at {candidate}: {e}{RESET}") + return None + current = current.parent + + for root, dirs, files in os.walk(start_path): + if "settings.json" in files: + candidate = pathlib.Path(root) / "settings.json" + try: + with open(candidate, "r", encoding="utf-8") as f: + data = json.load(f) + print(f"[VOLTA] {GREEN}Loaded settings.json locale '{data.get('locale')}' from {candidate}{RESET}") + return data + except Exception as e: + print(f"[VOLTA] {RED}Failed to load settings.json at {candidate}: {e}{RESET}") + return None + + print(f"[VOLTA] {YELLOW}No settings.json found scanning up or down from {start_path}{RESET}") + return None + +settings = load_settings_json() +if settings and "locale" in settings: + LOCALE = settings["locale"] else: - print(f"[VOLTA] {YELLOW}No .env file found from {__file__} upwards.{RESET}") + env_path = find_dotenv(pathlib.Path(__file__).parent) + if env_path: + load_dotenv(dotenv_path=env_path) + print(f"[VOLTA] {GREEN}Loaded .env from {env_path}{RESET}") + else: + print(f"[VOLTA] {YELLOW}No .env file found from {__file__} upwards.{RESET}") + + LOCALE = os.getenv("LOCALE") or None + locales_dirs.extend(find_locales_dirs(module_dir)) if working_dir != module_dir: