Added default config override

This commit is contained in:
Profitroll 2023-04-27 13:02:56 +02:00
parent 012a354f8a
commit f9dcf7e07b
2 changed files with 53 additions and 24 deletions

52
main.py
View File

@ -6,7 +6,7 @@ from locale import setlocale, LC_ALL
from docxtpl import DocxTemplate 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: 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(): def main():
for lang in config["languages"]: for lang in config["languages"]:
data = json_read(Path("data", "default.json")) # data = json_read(Path("data", "default.json"))
local_data = json_read(Path("data", f"{lang}.json")) # local_data = json_read(Path("data", f"{lang}.json"))
local_config = json_read(Path("configs", f"{lang}.json")) local_config = json_read(Path("configs", f"{lang}.json"))
setlocale(LC_ALL, local_config["locale"]) setlocale(LC_ALL, local_config["locale"])
template = DocxTemplate(Path("templates", f"{lang}.docx")) template = DocxTemplate(Path("templates", f"{lang}.docx"))
location_birth = None 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: if "lang" not in alt_name:
continue continue
if alt_name["lang"] == lang: if alt_name["lang"] == lang:
location_birth = alt_name["name"] location_birth = alt_name["name"]
break break
if location_birth is None: 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( location_live = local_config["header"]["formats"]["address"].format(
street=data["header"]["location"]["street"], street=data_read("street", "header", "location", locale=lang),
number=data["header"]["location"]["number"], number=data_read("number", "header", "location", locale=lang),
zip=data["header"]["location"]["zip"], zip=data_read("zip", "header", "location", locale=lang),
city=data["header"]["location"]["city"], city=data_read("city", "header", "location", locale=lang),
) )
out_table_jobs = [] 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"]: if job["time_start"] == job["time_end"]:
job_time = local_config["jobs"]["placeholders"]["single"].format( job_time = local_config["jobs"]["placeholders"]["single"].format(
start=datetime.fromisoformat(job["time_start"]).strftime( start=datetime.fromisoformat(job["time_start"]).strftime(
@ -72,7 +76,7 @@ def main():
out_table_education = [] 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"]: if education["time_start"] == education["time_end"]:
education_time = local_config["education"]["placeholders"][ education_time = local_config["education"]["placeholders"][
"single" "single"
@ -108,27 +112,27 @@ def main():
) )
context = { context = {
"name": data["header"]["name"], "name": data_read("name", "header", locale=lang),
"surname": data["header"]["surname"], "surname": data_read("surname", "header", locale=lang),
"birth_data": local_config["header"]["placeholders"][ "birth_data": local_config["header"]["placeholders"][
"birth_date_place" "birth_date_place"
].format( ].format(
date=datetime.fromisoformat(data["header"]["birth_date"]).strftime( date=datetime.fromisoformat(
local_config["header"]["formats"]["date"] data_read("birth_date", "header", locale=lang)
), ).strftime(local_config["header"]["formats"]["date"]),
city=location_birth, city=location_birth,
country=data["header"]["birth_place"]["countryName"], country=data_read("countryName", "header", "birth_place", locale=lang),
), ),
"location": location_live, "location": location_live,
"family_status": local_data["header"]["family_status"].title(), "family_status": data_read("family_status", "header", locale=lang).title(),
"nationality": local_data["header"]["nationality"].title(), "nationality": data_read("nationality", "header", locale=lang).title(),
"email": data["header"]["email"], "email": data_read("email", "header", locale=lang),
"phone": data["header"]["phone"], "phone": data_read("phone", "header", locale=lang),
"website": data["header"]["website"], "website": data_read("website", "header", locale=lang),
"table_jobs": out_table_jobs, "table_jobs": out_table_jobs,
"table_education": out_table_education, "table_education": out_table_education,
"skills": data["skills"], "skills": data_read("skills", locale=lang),
"hobbies": data["hobbies"], "hobbies": data_read("hobbies", locale=lang),
"signature": local_config["footer"]["placeholders"]["signature"].format( "signature": local_config["footer"]["placeholders"]["signature"].format(
city=signature_location, city=signature_location,
date=datetime.now().strftime("%x"), date=datetime.now().strftime("%x"),

View File

@ -1,4 +1,5 @@
from json import loads from json import loads
from os import path
from pathlib import Path from pathlib import Path
from typing import Any, Union 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: with open(str(filepath), "r", encoding="utf-8") as file:
output = loads(file.read()) output = loads(file.read())
return output 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