def render(cv, cvRender: CvRenderBase, baseFolder: str, path: str = None, command: str = "pdflatex", params={}, resources={}): if path == None: path = id_gen() os.system("mkdir Output/" + path) if baseFolder != None: os.system("cp -r Templates/" + baseFolder + "/* Output/" + path + "/") cvString = cvRender.render(cv, params=params, baseFolder=baseFolder, resources=resources) log_step(cv.header.email, cv.cv_hash, "TEX_FULLY_COMPILED") os.system("touch Output/" + path + "/main.tex") file = open("Output/" + path + "/main.tex", "w", encoding="utf-8") file.write(cvString) file.close() p = subprocess.Popen([command, "main.tex"], cwd="Output/" + path, stdout=subprocess.PIPE) p.wait() p_output = p.stdout.read().decode("utf-8", errors='ignore') log_step(cv.header.email, cv.cv_hash, "LATEX_CMD_EXECUTED", p_output) os.system("cp Output/" + path + "/main.pdf Output/" + path + ".pdf") os.system("rm -r Output/" + path + "/") return path
def render(cv, cvRender: CvRenderBase = None, baseFolder: str = None, path: str = None, command: str = "pdflatex", params={}, resources={}): cv_file_content = json.dumps(cv, indent=None, separators=(',', ':')) file = open("Output/" + path + ".json", "w", encoding="utf-8") file.write(cv_file_content) file.close() log_step(cv["header"]["email"], path, "JSON_FILE_RENDERED", cv_file_content) return path
def render(cv, baseFolder: str, params={}, resources={}): file = open("Templates/" + baseFolder + "/main.tex", "r", encoding="utf-8") templateString = file.read() file.close() cvDict = {} headerVars = [ "name", "address", "github", "linkedin", "email", "phone", "birthday", "homepage" ] cvDict["firstname"] = text_clean(cv.header.name.split(' ')[0]) cvDict["surname"] = text_clean(' '.join(cv.header.name.split(' ')[1:])) cvDict["lastname"] = text_clean(cv.header.name.split(' ')[-1]) for var in headerVars: cvDict[var] = text_clean(eval("cv.header." + var)) if cv.header.birthday != None: cvDict["birthday"] = timestring.Date(cv.header.birthday).date cvDict["work_array"] = CvRenderCheetahTemplate.extract_item(cv, 'work') cvDict["education_array"] = CvRenderCheetahTemplate.extract_item( cv, 'education') cvDict["academic_array"] = CvRenderCheetahTemplate.extract_item( cv, 'academic') cvDict["language_array"] = CvRenderCheetahTemplate.extract_item( cv, 'language') cvDict["project_array"] = CvRenderCheetahTemplate.extract_item( cv, 'project') cvDict["achievement_array"] = CvRenderCheetahTemplate.extract_item( cv, 'achievement') cvDict["skill_dict"] = CvRenderCheetahTemplate.extract_skills(cv) cvDict["params"] = params cvDict["break_into_items"] = CvRenderCheetahTemplate.break_into_items cvDict["format_skill"] = CvRenderCheetahTemplate.format_skill cvDict["format_datetime"] = format_datetime for key in resources: resources[key] = text_clean(resources[key]) cvDict["resources"] = resources log_step(cv.header.email, cv.cv_hash, "GENERATING_TEX_FROM_CHEETAH") template = Template(templateString, cvDict) log_step(cv.header.email, cv.cv_hash, "GENERATED_TEX_FROM_CHEETAH") return str(template)
def render(cv, cvRender: CvRenderBase, baseFolder: str, path: str = None, params={}, resources={}): if path == None: path = id_gen() os.system("mkdir Output/" + path) if baseFolder != None: os.system("cp -r Templates/" + baseFolder + "/* Output/" + path + "/") cvString = cvRender.render(cv, params=params, baseFolder=baseFolder, resources=resources) log_step(cv.header.email, cv.cv_hash, "TEX_FULLY_COMPILED") os.system("touch Output/" + path + "/main.tex") file = open("Output/" + path + "/main.tex", "w", encoding="utf-8") file.write(cvString) file.close() shutil.make_archive("Output/" + path, "zip", "Output/" + path + "/") os.system("rm -r Output/" + path + "/") return path
def get_cv_queue(ch, method, properties, body): mes = "" ts = time.time() email = "" cv_type = "" lang = "" try: body = body.decode('utf-8') dic = json.loads(body) print('[*] Consuming job: ' + dic["path"], flush=True) cv_type = dic["render_key"] email = dic["curriculum_vitae"]["header"]["email"] log_step(email, dic["path"], "CONSUMED_FROM_RABBITMQ") lang = dic["params"]["lang"] ans = render_from_cv_dict(dic) file = open('Output/' + ans + '.pdf', 'rb') ansb = file.read() file.close() log_step(email, dic["path"], "SENDING_TO_STORAGE") requests.post('http://storage:6000/', data={ 'email': email, 'id': ans, 'content': ansb }) mes = "OK" except Exception: exc_type, exc_value, exc_traceback = sys.exc_info() p_output = repr( traceback.format_exception(exc_type, exc_value, exc_traceback)) log_step(email, dic["path"], "ERROR_REPORTED_IN_RENDERER", "", p_output) mes = "err" email_hash = hashlib.sha256() email_hash.update(str.encode(email)) email = email_hash.hexdigest() log = [{ "measurement": "accuracy", "fields": { "status": mes, "render_time": float(time.time() - ts), "model": cv_type, "email": email, "lang": lang, }, "tags": { "status": mes, "render_time": float(time.time() - ts), "model": cv_type, "email": email, "lang": lang, }, }] client.write_points(log) ch.basic_ack(delivery_tag=method.delivery_tag)
def render_from_cv_dict(req): refresh_render_map() ret = "" log_step(req["curriculum_vitae"]["header"]["email"], req["path"], "GENERATING_CV_AST") path = None if 'path' in req: path = req['path'] params = {} render_key = "awesome" params['section_order'] = [ 'work', 'education', 'achievement', 'project', 'academic', 'language', 'skill' ] req_cv = req['curriculum_vitae'] if 'render_key' in req: render_key = req['render_key'] params = render_map[render_key]['fixed_params'] if 'params' in req: params.update(req['params']) if 'section_order' in req: params['section_order'] = req['section_order'] if 'header' not in req_cv: log_step('', req['path'], "MISSING_HEADER") raise Exception('Header is missing') sm = SchemaManager('./Models/') sm.load('Cv') enc = Encoder(sm) cv = enc.to_object(req_cv, 'Cv') setattr(cv, 'cv_hash', req["path"]) log_step(cv.header.email, cv.cv_hash, "CV_AST_GENERATED") baseFolder = render_map[render_key]['base_folder'] if 'lang' not in params: params['lang'] = 'en_US' resources = get_resources(params['lang'][:2]) path = Renders.CvRenderTexToPdf.render( cv, path=path, cvRender=Renders.CvRenderCheetahTemplate, baseFolder=baseFolder, command=render_map[render_key]['command'], params=params, resources=resources) return path