diff --git a/main.py b/main.py index 5db500e..68af82b 100644 --- a/main.py +++ b/main.py @@ -6,7 +6,7 @@ from locale import setlocale, LC_ALL from docxtpl import DocxTemplate -from modules.utils import json_read +from modules.utils import data_read, json_read def local_if_available(data: dict, data_local: dict, *args: str) -> Any: @@ -15,33 +15,37 @@ def local_if_available(data: dict, data_local: dict, *args: str) -> Any: def main(): for lang in config["languages"]: - data = json_read(Path("data", "default.json")) - local_data = json_read(Path("data", f"{lang}.json")) + # data = json_read(Path("data", "default.json")) + # local_data = json_read(Path("data", f"{lang}.json")) local_config = json_read(Path("configs", f"{lang}.json")) setlocale(LC_ALL, local_config["locale"]) template = DocxTemplate(Path("templates", f"{lang}.docx")) location_birth = None - for alt_name in data["header"]["birth_place"]["alternateNames"]: + for alt_name in data_read( + "alternateNames", "header", "birth_place", locale=lang + ): if "lang" not in alt_name: continue if alt_name["lang"] == lang: location_birth = alt_name["name"] break if location_birth is None: - location_birth = data["header"]["birth_place"]["toponymName"] + location_birth = data_read( + "toponymName", "header", "birth_place", locale=lang + ) location_live = local_config["header"]["formats"]["address"].format( - street=data["header"]["location"]["street"], - number=data["header"]["location"]["number"], - zip=data["header"]["location"]["zip"], - city=data["header"]["location"]["city"], + street=data_read("street", "header", "location", locale=lang), + number=data_read("number", "header", "location", locale=lang), + zip=data_read("zip", "header", "location", locale=lang), + city=data_read("city", "header", "location", locale=lang), ) out_table_jobs = [] - for job in data["table_jobs"]: + for job in data_read("table_jobs", locale=lang): if job["time_start"] == job["time_end"]: job_time = local_config["jobs"]["placeholders"]["single"].format( start=datetime.fromisoformat(job["time_start"]).strftime( @@ -72,7 +76,7 @@ def main(): out_table_education = [] - for education in data["table_education"]: + for education in data_read("table_education", locale=lang): if education["time_start"] == education["time_end"]: education_time = local_config["education"]["placeholders"][ "single" @@ -108,27 +112,27 @@ def main(): ) context = { - "name": data["header"]["name"], - "surname": data["header"]["surname"], + "name": data_read("name", "header", locale=lang), + "surname": data_read("surname", "header", locale=lang), "birth_data": local_config["header"]["placeholders"][ "birth_date_place" ].format( - date=datetime.fromisoformat(data["header"]["birth_date"]).strftime( - local_config["header"]["formats"]["date"] - ), + date=datetime.fromisoformat( + data_read("birth_date", "header", locale=lang) + ).strftime(local_config["header"]["formats"]["date"]), city=location_birth, - country=data["header"]["birth_place"]["countryName"], + country=data_read("countryName", "header", "birth_place", locale=lang), ), "location": location_live, - "family_status": local_data["header"]["family_status"].title(), - "nationality": local_data["header"]["nationality"].title(), - "email": data["header"]["email"], - "phone": data["header"]["phone"], - "website": data["header"]["website"], + "family_status": data_read("family_status", "header", locale=lang).title(), + "nationality": data_read("nationality", "header", locale=lang).title(), + "email": data_read("email", "header", locale=lang), + "phone": data_read("phone", "header", locale=lang), + "website": data_read("website", "header", locale=lang), "table_jobs": out_table_jobs, "table_education": out_table_education, - "skills": data["skills"], - "hobbies": data["hobbies"], + "skills": data_read("skills", locale=lang), + "hobbies": data_read("hobbies", locale=lang), "signature": local_config["footer"]["placeholders"]["signature"].format( city=signature_location, date=datetime.now().strftime("%x"), diff --git a/modules/utils.py b/modules/utils.py index d3dbd1f..15dc7df 100644 --- a/modules/utils.py +++ b/modules/utils.py @@ -1,4 +1,5 @@ from json import loads +from os import path from pathlib import Path from typing import Any, Union @@ -7,3 +8,27 @@ def json_read(filepath: Union[str, Path]) -> Any: with open(str(filepath), "r", encoding="utf-8") as file: output = loads(file.read()) return output + + +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): + default_file = locale_file = json_read(Path("data", f"default.json")) + try: + output = nested_read(key, *args, data=locale_file) + except (KeyError, ValueError): + return f"KEY NOT FOUND IN BOTH {locale} AND default" + return output