Feat: rexrite produce_and_compile

This commit is contained in:
Bertrand Benjamin 2019-12-24 08:01:26 +01:00
parent d66e1a0887
commit 97468b9602
3 changed files with 61 additions and 143 deletions

View File

@ -169,81 +169,68 @@ def texcompile(filename):
def produce_and_compile(options):
""" Produce and compile subjects
"""
working_dir = get_working_dir(options)
logger.debug(f"CI parser gets {options}")
if options["only_corr"]:
options["corr"] = True
tex_files = working_dir.glop("[0-9]*_*.tex")
else:
template = Path(options["template"]).name
logger.debug(f"Template will be {template}")
template = Path(options["template"]).name
directory = Path(options["template"]).parent
metadatas = subject_metadatas(options)
logger.debug(f"Metadata {metadatas}")
list_infos = subject_metadatas(options)
for meta in metadatas:
logger.debug(f"Feeding template toward {meta['texfile']}")
if options["crazy"]:
crazy_feed(
template=Path(meta["directory"]) / meta["template"],
data=meta,
output=meta["texfile"],
force=1,
)
else:
feed(
template=Path(meta["directory"]) / meta["template"],
data=meta,
output=meta["texfile"],
force=1,
)
assert(Path(meta["texfile"]).exists())
logger.debug(f"{meta['texfile']} fed")
logger.debug(f"Metadata {list_infos}")
tex_files = []
for infos in list_infos:
dest = working_dir / Path(template.replace("tpl", infos["num"]))
logger.debug(f"Feeding template toward {dest}")
tex_files.append(dest)
if options["crazy"]:
while True:
try:
pytex.feed(
working_dir / template,
{"infos": infos},
output=dest,
force=1,
)
except:
pass
else:
break
else:
pytex.feed(
working_dir / template, {"infos": infos}, output=dest, force=1
)
logger.debug(f"{dest} fed")
if options["corr"]:
logger.debug(f"Building correction for {meta['texfile']}")
meta.update({
"corr_texfile": activate_printanswers(meta["texfile"]),
})
if not options["no_compile"]:
pdf_files = []
for texfile in tex_files:
logger.debug(f"Start compiling {texfile}")
pytex.pdflatex(texfile)
logger.debug(f"End compiling {texfile}")
pdf_files.append(str(texfile).split(".")[0] + ".pdf")
logger.debug(f"Compiled files : {pdf_files}")
for prefix in ["", "corr_"]:
key = prefix + "texfile"
try:
meta[key]
except KeyError:
pass
else:
texcompile(meta[key])
meta.update({
prefix+'pdffile': meta[key].replace('tex', 'pdf')
})
if not options["no_join"] and not options["no_compile"]:
pdfjoin(
pdf_files,
template.replace("tpl", "all").replace(".tex", ".pdf"),
working_dir,
rm_pdfs=1,
)
if options["corr"]:
pdf_files = []
for texfile in tex_files:
corr_fname = activate_printanswers(texfile)
if not options["no_compile"]:
logger.debug(f"Start compiling {texfile}")
pytex.pdflatex(corr_fname)
logger.debug(f"End compiling {texfile}")
pdf_files.append(str(corr_fname).split(".")[0] + ".pdf")
deactivate_printanswers(corr_fname)
if not options["no_join"] and not options["no_compile"]:
pdfjoin(
pdf_files,
template.replace("tpl", "corr").replace(".tex", ".pdf"),
working_dir,
rm_pdfs=1,
)
if not options["no_join"]:
for prefix in ["", "corr_"]:
key = prefix + "pdffile"
try:
pdfs = [m[key] for m in metadatas]
except KeyError:
pass
else:
pdfjoin(
pdfs,
template.replace("tpl", prefix+"all").replace(".tex", ".pdf"),
directory,
rm_pdfs=1,
)
if not options["dirty"]:
pytex.clean(working_dir)
clean(directory)
# -----------------------------

View File

@ -3,18 +3,7 @@
import click
import logging
from pathlib import Path
from .bopytex import subject_metadatas, crazy_feed, pdfjoin, feed, clean, texcompile
formatter = logging.Formatter("%(name)s :: %(levelname)s :: %(message)s")
steam_handler = logging.StreamHandler()
steam_handler.setLevel(logging.DEBUG)
steam_handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(steam_handler)
from .bopytex import produce_and_compile
@click.command()
@click.argument(
@ -87,70 +76,7 @@ def new(**options):
Feed the template (tpl_...) and then compile it with latex.
"""
logger.debug(f"CI parser gets {options}")
template = Path(options["template"]).name
directory = Path(options["template"]).parent
metadatas = subject_metadatas(options)
logger.debug(f"Metadata {metadatas}")
for meta in metadatas:
logger.debug(f"Feeding template toward {meta['texfile']}")
if options["crazy"]:
crazy_feed(
template=Path(meta["directory"]) / meta["template"],
data=meta,
output=meta["texfile"],
force=1,
)
else:
feed(
template=Path(meta["directory"]) / meta["template"],
data=meta,
output=meta["texfile"],
force=1,
)
assert(Path(meta["texfile"]).exists())
logger.debug(f"{meta['texfile']} fed")
if options["corr"]:
logger.debug(f"Building correction for {meta['texfile']}")
meta.update({
"corr_texfile": activate_printanswers(meta["texfile"]),
})
if not options["no_compile"]:
for prefix in ["", "corr_"]:
key = prefix + "texfile"
try:
meta[key]
except KeyError:
pass
else:
texcompile(meta[key])
meta.update({
prefix+'pdffile': meta[key].replace('tex', 'pdf')
})
if not options["no_join"]:
for prefix in ["", "corr_"]:
key = prefix + "pdffile"
try:
pdfs = [m[key] for m in metadatas]
except KeyError:
pass
else:
pdfjoin(
pdfs,
template.replace("tpl", prefix+"all").replace(".tex", ".pdf"),
directory,
rm_pdfs=1,
)
if not options["dirty"]:
clean(directory)
# produce_and_compile(options)
produce_and_compile(options)
if __name__ == "__main__":

View File

@ -111,6 +111,7 @@ def test_metadatas(prepare_test_template):
"classe": "1ST",
"elo": "1000",
"texfile": "01_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
{
@ -119,6 +120,7 @@ def test_metadatas(prepare_test_template):
"classe": "1ST",
"elo": "1300",
"texfile": "02_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
{
@ -127,6 +129,7 @@ def test_metadatas(prepare_test_template):
"classe": "1ST",
"elo": "100",
"texfile": "03_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
{
@ -135,6 +138,7 @@ def test_metadatas(prepare_test_template):
"classe": "1ST",
"elo": "4000",
"texfile": "04_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
{
@ -143,6 +147,7 @@ def test_metadatas(prepare_test_template):
"classe": "1ST",
"elo": "1300",
"texfile": "05_test.tex",
"template": "tpl_test.tex",
"directory": ".",
},
]