updated 2 search for a settings.json too

This commit is contained in:
WhatDidYouExpect 2025-07-23 15:46:48 +02:00
parent f93811cfa3
commit ee6227a5c5

44
main.py
View file

@ -50,13 +50,51 @@ def find_dotenv(start_path: pathlib.Path) -> pathlib.Path | None:
current = current.parent current = current.parent
return None return None
env_path = find_dotenv(pathlib.Path(__file__).parent) def load_settings_json() -> dict | None:
if env_path: 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:
env_path = find_dotenv(pathlib.Path(__file__).parent)
if env_path:
load_dotenv(dotenv_path=env_path) load_dotenv(dotenv_path=env_path)
print(f"[VOLTA] {GREEN}Loaded .env from {env_path}{RESET}") print(f"[VOLTA] {GREEN}Loaded .env from {env_path}{RESET}")
else: else:
print(f"[VOLTA] {YELLOW}No .env file found from {__file__} upwards.{RESET}") 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)) locales_dirs.extend(find_locales_dirs(module_dir))
if working_dir != module_dir: if working_dir != module_dir:
locales_dirs.extend(find_locales_dirs(working_dir)) locales_dirs.extend(find_locales_dirs(working_dir))