from argparse import ArgumentParser from datetime import datetime from locale import LC_ALL, setlocale from os import makedirs, path from pathlib import Path from typing import Any from docx2pdf import convert from docxtpl import DocxTemplate parser = ArgumentParser(description="Ping script") parser.add_argument( "--convert", dest="convert_output", action="store_true", help="convert output .docx files to .pdf", ) from modules.utils import data_read, json_read 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_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_read( "toponymName", "header", "birth_place", locale=lang ) location_live = local_config["header"]["formats"]["address"].format( 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_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( 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_read("table_education", locale=lang): 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_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_read("birth_date", "header", locale=lang) ).strftime(local_config["header"]["formats"]["date"]), city=location_birth, country=data_read("countryName", "header", "birth_place", locale=lang), ), "location": location_live, "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_read("skills", locale=lang), "about": data_read("about", locale=lang), "hobbies": data_read("hobbies", locale=lang), "signature": local_config["footer"]["placeholders"]["signature"].format( city=signature_location, date=datetime.now().strftime("%x"), ), } template.render(context, autoescape=True) 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__": args = parser.parse_args() 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() if args.convert_output: print("Converting everything to PDF...", flush=True) convert(Path("output").absolute())