CVGenerator/main.py

164 lines
6.3 KiB
Python
Raw Normal View History

2023-04-26 17:07:49 +03:00
from datetime import datetime
from os import makedirs, path
from pathlib import Path
from typing import Any
from locale import setlocale, LC_ALL
from docxtpl import DocxTemplate
from modules.utils import json_read
def local_if_available(data: dict, data_local: dict, *args: str) -> Any:
return
def main():
for lang in config["languages"]:
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"]:
if alt_name["lang"] == lang:
location_birth = alt_name["name"]
break
if location_birth is None:
location_birth = data["header"]["birth_place"]["toponymName"]
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"],
)
out_table_jobs = []
for job in data["table_jobs"]:
if job["time_start"] == job["time_end"]:
job_time = local_config["jobs"]["placeholders"]["single"].format(
start=datetime.fromisoformat(job["time_start"]).strftime(
local_config["jobs"]["formats"]["default"]
)
)
elif job["time_end"] is None:
job_time = local_config["jobs"]["placeholders"]["ongoing"].format(
start=datetime.fromisoformat(job["time_start"]).strftime(
local_config["jobs"]["formats"]["default"]
)
)
else:
job_time = local_config["jobs"]["placeholders"]["from_to"].format(
start=datetime.fromisoformat(job["time_start"]).strftime(
local_config["jobs"]["formats"]["default"]
),
end=datetime.fromisoformat(job["time_end"]).strftime(
local_config["jobs"]["formats"]["default"]
),
)
out_table_jobs.append(
{
"time": job_time,
"description": job["description"] + "\n",
}
)
out_table_education = []
for education in data["table_education"]:
if education["time_start"] == education["time_end"]:
education_time = local_config["education"]["placeholders"][
"single"
].format(
start=datetime.fromisoformat(education["time_start"]).strftime(
local_config["education"]["formats"]["default"]
)
)
elif education["time_end"] is None:
education_time = local_config["education"]["placeholders"][
"ongoing"
].format(
start=datetime.fromisoformat(education["time_start"]).strftime(
local_config["education"]["formats"]["default"]
)
)
else:
education_time = local_config["education"]["placeholders"][
"from_to"
].format(
start=datetime.fromisoformat(education["time_start"]).strftime(
local_config["education"]["formats"]["default"]
),
end=datetime.fromisoformat(education["time_end"]).strftime(
local_config["education"]["formats"]["default"]
),
)
out_table_education.append(
{
"time": education_time,
"description": education["description"] + "\n",
}
)
context = {
"name": data["header"]["name"],
"surname": data["header"]["surname"],
"birth_data": local_config["header"]["placeholders"][
"birth_date_place"
].format(
date=datetime.fromisoformat(data["header"]["birth_date"]).strftime(
local_config["header"]["formats"]["date"]
),
city=location_birth,
country=data["header"]["birth_place"]["countryName"],
),
"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"],
"table_jobs": out_table_jobs,
"table_education": out_table_education,
"skills": data["skills"],
"hobbies": data["hobbies"],
"signature": local_config["footer"]["placeholders"]["signature"].format(
city=signature_location,
date=datetime.now().strftime("%x"),
),
}
template.render(context)
makedirs("output", exist_ok=True)
template.save(Path("output", f"{lang}.docx"))
print(
f"Created output for '{lang}' and saved to {Path('output', f'{lang}.docx').absolute()}",
flush=True,
)
if __name__ == "__main__":
config = json_read(Path("config.json"))
makedirs(Path(".cache"), exist_ok=True)
if not path.exists(Path(".cache", "location")):
signature_location = input("Please, enter signature location: ")
with open(Path(".cache", "location"), "w", encoding="utf-8") as file:
file.write(signature_location)
else:
with open(Path(".cache", "location"), "r", encoding="utf-8") as file:
cached_location = file.read()
signature_location = input(
f"Please, enter signature location (send Enter to use previous location '{cached_location}'): "
)
signature_location = (
cached_location if signature_location == "" else signature_location
)
main()