2023-04-26 17:07:49 +03:00
|
|
|
from json import loads
|
2023-04-27 14:02:56 +03:00
|
|
|
from os import path
|
2023-04-26 17:07:49 +03:00
|
|
|
from pathlib import Path
|
|
|
|
from typing import Any, Union
|
|
|
|
|
|
|
|
|
|
|
|
def json_read(filepath: Union[str, Path]) -> Any:
|
|
|
|
with open(str(filepath), "r", encoding="utf-8") as file:
|
|
|
|
output = loads(file.read())
|
|
|
|
return output
|
2023-04-27 14:02:56 +03:00
|
|
|
|
|
|
|
|
|
|
|
def nested_read(key: str, *args: str, data: dict) -> Any:
|
|
|
|
this_key = data
|
|
|
|
for dict_key in args:
|
|
|
|
this_key = this_key[dict_key]
|
|
|
|
return this_key[key]
|
|
|
|
|
|
|
|
|
|
|
|
def data_read(key: str, *args: str, locale: str) -> Any:
|
|
|
|
locale_file = (
|
|
|
|
json_read(Path("data", f"{locale}.json"))
|
|
|
|
if path.exists(Path("data", f"{locale}.json"))
|
|
|
|
else {}
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
output = nested_read(key, *args, data=locale_file)
|
|
|
|
except (KeyError, ValueError):
|
2024-04-02 23:50:11 +03:00
|
|
|
default_file = locale_file = json_read(Path("data", "default.json"))
|
2023-04-27 14:02:56 +03:00
|
|
|
try:
|
|
|
|
output = nested_read(key, *args, data=locale_file)
|
|
|
|
except (KeyError, ValueError):
|
|
|
|
return f"KEY NOT FOUND IN BOTH {locale} AND default"
|
|
|
|
return output
|