UnderCover/latty.py

74 lines
2.3 KiB
Python

# Copyright Sage Vaillancourt 2021
import os
import subprocess
from dataclasses import dataclass
from flask import send_from_directory
# latex /home/sage/Documents/latex/coverLetter/temp.tex'
# dvipdf /home/sage/Documents/latex/coverLetter/temp.dvi ~/Documents/SageVaillancourt_CoverLetter.pdf'
# rm -r /home/sage/Documents/latex/coverLetter/temp*'
@dataclass
class CLData():
username: str
company: str
jobandpronoun: str
skilltypes: str
myskills: str
closingtext: str
body: str
def get_pairs(self):
return [
("username", self.username),
("company", self.company),
("jobandpronoun", self.jobandpronoun),
("skilltypes", self.skilltypes),
("myskills", self.myskills),
("closingtext", self.closingtext),
("body", self.body),
]
def define(file, name, data, whitespace=""): file.write("\\def \\" + name + "{" + data + whitespace + "}\n")
def generate(data: CLData, unique):
import threading
proj_dir = os.path.dirname(os.getcwd()) + '/undercover/'
output_dir = proj_dir + 'outputs/'
unique_file = output_dir + unique + ".tex"
f = open(unique_file, "w")
define(f, "username", data.username)
define(f, "thecompany", data.company)
define(f, "jobandpronoun", data.jobandpronoun, " ")
define(f, "skilltypes", data.skilltypes, " ")
define(f, "myskills", data.myskills, " ")
define(f, "closingtext", data.closingtext)
define(f, "body", data.body)
f.write(open(proj_dir + "/writing_templates/base.tex", "r").read())
f.close()
base_tex = output_dir + unique + ".tex"
com = "pdflatex -halt-on-error -jobname=outputs/" + unique + " " + base_tex
print("COM: '" + com + "'")
#TODO: If error, don't try to return the pdf
subprocess.run(['bash', '-c', com])
root_dir = os.path.dirname(os.getcwd())
download = root_dir + '/undercover/outputs/'
threading.Timer(60 * 30, cleanup, [output_dir + unique]).start()
return send_from_directory(
download,
unique + ".pdf",
attachment_filename=data.username.replace(" ", "") + "_CoverLetter.pdf",
as_attachment=True
)
def cleanup(unique):
print(unique)
subprocess.run(['bash', '-c', "rm " + unique + ".*"])