updated 2 search for a settings.json too
This commit is contained in:
parent
f93811cfa3
commit
ee6227a5c5
1 changed files with 43 additions and 5 deletions
38
main.py
38
main.py
|
@ -50,6 +50,41 @@ def find_dotenv(start_path: pathlib.Path) -> pathlib.Path | None:
|
|||
current = current.parent
|
||||
return None
|
||||
|
||||
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:
|
||||
env_path = find_dotenv(pathlib.Path(__file__).parent)
|
||||
if env_path:
|
||||
load_dotenv(dotenv_path=env_path)
|
||||
|
@ -57,6 +92,9 @@ if env_path:
|
|||
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:
|
||||
locales_dirs.extend(find_locales_dirs(working_dir))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue