UnderCover/latty.py

68 lines
1.9 KiB
Python
Raw Normal View History

# Copyright Sage Vaillancourt 2021
import os
import subprocess
2021-07-23 23:09:08 -04:00
from dataclasses import dataclass
from flask import send_from_directory
def get_unique():
import uuid
unique = str(uuid.uuid1().hex)
print("Unique ID: " + unique)
return unique
@dataclass
class CLData():
username: str
company: str
jobandpronoun: str
skilltypes: str
myskills: str
closingtext: str
body: str
2021-07-24 01:08:18 -04:00
def get_pairs(self):
return [
2021-07-24 01:08:18 -04:00
("username", self.username),
("company", self.company),
("jobandpronoun", self.jobandpronoun),
("skilltypes", self.skilltypes),
("myskills", self.myskills),
("closingtext", self.closingtext),
("body", self.body),
]
def generate_pdf(self):
import threading
unique_id = get_unique()
root_dir = os.path.dirname(os.getcwd())
proj_dir = root_dir + '/undercover/'
output_dir = proj_dir + 'outputs/'
2021-07-23 23:09:08 -04:00
unique_file = output_dir + unique_id + ".tex"
f = open(unique_file, "w")
for pair in self.get_pairs():
f.write("\\def \\" + pair[0] + "{" + pair[1] + "}\n")
f.write(open(proj_dir + "/writing_templates/base.tex", "r").read())
f.close()
2021-07-23 23:09:08 -04:00
com = "pdflatex -halt-on-error -jobname=outputs/" + unique_id + " " + unique_file
print("COM: '" + com + "'")
try:
subprocess.check_call(['bash', '-c', com])
threading.Timer(60 * 30, cleanup, [output_dir + unique_id]).start()
2021-07-23 23:09:08 -04:00
return send_from_directory(
output_dir,
unique_id + ".pdf",
attachment_filename=self.username.replace(" ", "") + "_CoverLetter.pdf",
as_attachment=True
)
except subprocess.CalledProcessError:
return None
def cleanup(unique):
print(unique)
subprocess.run(['bash', '-c', "rm " + unique + ".*"])