CVGenerator/main.py

183 lines
7.1 KiB
Python
Raw Normal View History

2023-04-27 14:53:41 +03:00
from argparse import ArgumentParser
2023-04-26 17:07:49 +03:00
from datetime import datetime
2023-04-27 14:43:46 +03:00
from locale import LC_ALL, setlocale
2023-04-26 17:07:49 +03:00
from os import makedirs, path
from pathlib import Path
from typing import Any
2023-04-27 14:53:41 +03:00
from docx2pdf import convert
2023-04-26 17:07:49 +03:00
from docxtpl import DocxTemplate
2023-04-27 14:53:41 +03:00
parser = ArgumentParser(description="Ping script")
parser.add_argument(
"--convert",
dest="convert_output",
action="store_true",
help="convert output .docx files to .pdf",
)
2023-04-26 17:07:49 +03:00
2023-04-27 14:53:41 +03:00
from modules.utils import data_read, json_read
2023-04-26 17:07:49 +03:00
def main():
for lang in config["languages"]:
2023-04-27 14:02:56 +03:00
# data = json_read(Path("data", "default.json"))
# local_data = json_read(Path("data", f"{lang}.json"))
2023-04-26 17:07:49 +03:00
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
2023-04-27 14:02:56 +03:00
for alt_name in data_read(
"alternateNames", "header", "birth_place", locale=lang
):
2023-04-27 12:53:28 +03:00
if "lang" not in alt_name:
continue
2023-04-26 17:07:49 +03:00
if alt_name["lang"] == lang:
location_birth = alt_name["name"]
break
if location_birth is None:
2023-04-27 14:02:56 +03:00
location_birth = data_read(
"toponymName", "header", "birth_place", locale=lang
)
2023-04-26 17:07:49 +03:00
location_live = local_config["header"]["formats"]["address"].format(
2023-04-27 14:02:56 +03:00
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),
2023-04-26 17:07:49 +03:00
)
out_table_jobs = []
2023-04-27 14:02:56 +03:00
for job in data_read("table_jobs", locale=lang):
2023-04-26 17:07:49 +03:00
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 = []
2023-04-27 14:02:56 +03:00
for education in data_read("table_education", locale=lang):
2023-04-26 17:07:49 +03:00
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 = {
2023-04-27 14:02:56 +03:00
"name": data_read("name", "header", locale=lang),
"surname": data_read("surname", "header", locale=lang),
2023-04-26 17:07:49 +03:00
"birth_data": local_config["header"]["placeholders"][
"birth_date_place"
].format(
2023-04-27 14:02:56 +03:00
date=datetime.fromisoformat(
data_read("birth_date", "header", locale=lang)
).strftime(local_config["header"]["formats"]["date"]),
2023-04-26 17:07:49 +03:00
city=location_birth,
2023-04-27 14:02:56 +03:00
country=data_read("countryName", "header", "birth_place", locale=lang),
2023-04-26 17:07:49 +03:00
),
"location": location_live,
2023-04-27 14:02:56 +03:00
"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),
2023-04-26 17:07:49 +03:00
"table_jobs": out_table_jobs,
"table_education": out_table_education,
2023-04-27 14:02:56 +03:00
"skills": data_read("skills", locale=lang),
"hobbies": data_read("hobbies", locale=lang),
2023-04-26 17:07:49 +03:00
"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__":
2023-04-27 14:53:41 +03:00
args = parser.parse_args()
2023-04-26 17:07:49 +03:00
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()
2023-04-27 14:53:41 +03:00
if args.convert_output:
print(
f"Converting everything to PDF...",
flush=True,
)
convert(Path("output").absolute())