Feat: reorganisation in feed

This commit is contained in:
Bertrand Benjamin 2022-02-02 14:48:32 +01:00
parent 7bbaf1132d
commit fdb43cc18f
3 changed files with 38 additions and 19 deletions

View File

@ -40,30 +40,48 @@ def update_export_dict(new_dict):
"""
EXPORT_DICT.update(new_dict)
def run_python(template:str, data:dict):
""" Runs python commands using data
def feed(template, data, output="", force=0):
""" Feed template with data to output
:param template: string containing python commands
:param data: dictionary with variables needed to run commands
:param template: jinja2 template with texenv environment
:param data: Data dictionnary
"""
jinja_template = texenv.from_string(template)
return jinja_template.render(**data)
def build_output(filename):
""" Replace tpl with a number in filename and insure that this file does not exists """
num = 1
output_p = Path(filename.replace('tpl', f'{num:02d}'))
while output_p.exists():
logger.debug(f"{output_p} exists. Try next one")
num += 1
output_p = Path(filename.replace('tpl', f'{num:02d}'))
return output_p
def feed(template_filename, data, output="", force=0):
""" Feed template_filename with data and write it to output
:param template_filename: jinja2 template with texenv environment
:param data: Data dictionary
:param output: name of the output file
(by default: tpl is replaced by a 2 digits number)
:param force: Override is the output already exists
:return: name of fed template
:return: output filename
"""
logger.info(f"Getting template {template}")
tpl = texenv.get_template(str(template))
logger.info(f"Getting template_filename {template_filename}")
with open(template_filename, "r") as f:
fed = run_python("".join(f.readlines()), data)
if not output:
num = 1
output_p = Path(template.replace('tpl', f'{num:02d}'))
while output_p.exists() and not force:
logger.debug(f"{output_p} exists. Try next one")
num += 1
output_p = Path(template.replace('tpl', f'{num:02d}'))
output_p = build_output(template_filename)
else:
output_p = Path(output)
if not force and output_p.exists():
logger.error(f"{output} exists. Use force=1 do override it")
raise ValueError(f"{output} exists. Use force=1 do override it")
@ -74,8 +92,9 @@ def feed(template, data, output="", force=0):
output_dir.mkdir(exist_ok=True)
with open(output_p, "w") as output_f:
output_f.write(tpl.render(**EXPORT_DICT, **data))
logger.info(f"{template} has been rendered to {output}.")
output_f.write(fed)
logger.info(f"{template_filename} has been rendered to {output}.")
return output_p
@ -93,7 +112,7 @@ def pdflatex(tex_filename, output_dir=""):
os.chdir(output_dir)
compilation = subprocess.Popen(
[
"pdflatex",
"lualatex",
f"-output-directory={output_dir}",
# "-halt-on-error",
"-interaction=nonstopmode",

0
tests/__init__.py Normal file
View File