Fully implement pytex and functionnalyse it

This commit is contained in:
Benjamin Bertrand 2017-04-17 08:27:03 +03:00
parent 68cdb62f4c
commit c0f2882310

View File

@ -39,42 +39,82 @@ MAPYTEX_TOOLS = {
pytex.update_export_dict(MAPYTEX_TOOLS) pytex.update_export_dict(MAPYTEX_TOOLS)
def get_working_dir(options):
""" Get the working directory """
if options.working_dir:
working_dir = Path(options.working_dir)
else:
try:
template = Path(options.template)
except TypeError:
raise ValueError("Need to set the working directory \
or to give a template")
else:
working_dir = template.dirname()
logger.debug(f"The output directory will be {working_dir}")
return working_dir
def activate_printanswers(texfile):
""" Activate printanswers mod in texfile """
output_fname = "corr_" + texfile
with open(texfile, 'r') as input_f:
with open(output_fname, "w") as output_f:
for line in input_f.readlines():
output_f.write(line.replace(
r'%\printanswers',
r'\printanswers'
))
return output_fname
def deactivate_printanswers(corr_fname):
""" Activate printanswers mod in texfile """
Path(corr_fname).remove()
def pdfjoin(pdf_files, destname, working_dir=".", rm_pdfs=1):
"""TODO: Docstring for pdfjoin.
:param pdf_files: list of pdf files to join
:param destname: name for joined pdf
:param working_dir: the working directory
:param rm_pdfs: Remove pdf_files after joining them
:returns: TODO
"""
joined_pdfs = Path(working_dir) / Path(destname)
pdf_files_str = " ".join(pdf_files)
pdfjam = f"pdfjam {pdf_files_str} -o {joined_pdfs}"
logger.debug(f"Run {pdfjam}")
logger.info("Joining pdf files")
os.system(pdfjam)
if rm_pdfs:
logger.info(f"Remove {pdf_files_str}")
os.system(f"rm {pdf_files_str}")
def produce_and_compile(options): def produce_and_compile(options):
""" Produce and compile subjects """ Produce and compile subjects
""" """
# template = report_renderer.get_template(options.template) working_dir = get_working_dir(options)
# template = texenv.get_template(options.template)
if options.only_corr:
options.corr = True
tex_files = working_dir.files("[0-9]*_*.tex")
else:
template = Path(options.template) template = Path(options.template)
logger.debug(f"Template will be {template}") logger.debug(f"Template will be {template}")
if options.output_dir:
output_dir = Path(options.output_dir)
else:
output_dir = template.dirname()
logger.debug(f"The output directory will be {output_dir}")
# Saving place
# cwd = Path("./").abspath()
# template_file = Path(options.template)
list_infos = [ list_infos = [
{"num": f"{i+1:02d}"} {"num": f"{i+1:02d}"}
for i in range(options.num_subj) for i in range(options.num_subj)
] ]
# if output.dirname() != "":
# output.dirname().cd()
# output = output.name
if options.only_corr:
options.corr = True
else:
tex_files = [] tex_files = []
for infos in list_infos: for infos in list_infos:
# dest = Path(str(infos['num']) + output)
dest = ( dest = (
output_dir working_dir
/ Path(template.replace("tpl", infos["num"])) / Path(template.replace("tpl", infos["num"]))
) )
logger.debug(f"Feeding template toward {dest}") logger.debug(f"Feeding template toward {dest}")
@ -86,13 +126,7 @@ def produce_and_compile(options):
force=1 force=1
) )
logger.debug(f"{dest} fed") logger.debug(f"{dest} fed")
# with open(dest, 'w') as output_file:
# output_file.write(
# template.render(
# infos=infos,
# **EXPORT_DICT
# )
# )
if not options.no_compil: if not options.no_compil:
pdf_files = [] pdf_files = []
for texfile in tex_files: for texfile in tex_files:
@ -102,64 +136,35 @@ def produce_and_compile(options):
pdf_files.append(str(texfile[:-4] + ".pdf")) pdf_files.append(str(texfile[:-4] + ".pdf"))
logger.debug(f"Compiled files : {pdf_files}") logger.debug(f"Compiled files : {pdf_files}")
if not options.dirty: if not options.no_join and not options.no_compil:
pytex.clean(output_dir) pdfjoin(
pdf_files,
if not options.no_join: template.replace('tpl', "all").replace(".tex",".pdf"),
print(Path("./").abspath()) working_dir,
print( rm_pdfs=1
"pdfjam "
+ " ".join(pdf_files)
+ " -o all"
+ Path(output_dir).namebase
+ ".pdf"
) )
os.system(
"pdfjam "
+ " ".join(pdf_files)
+ " -o all"
+ Path(output_dir).namebase
+ ".pdf"
)
# os.system("pdfjam *.pdf -o all" + Path(output).namebase + ".pdf")
print("rm " + " ".join(pdf_files))
os.system("rm " + " ".join(pdf_files))
if options.corr: if options.corr:
find_subj_tex_files = "find ./ -iname '[0-9]*_*.tex' " pdf_files = []
os.system( for texfile in tex_files:
find_subj_tex_files corr_fname = activate_printanswers(texfile)
+ " -exec sed -i 's/%\\\\printanswers/\\\\printanswers/g' {} \\;"
)
if not options.no_compil: if not options.no_compil:
os.system( logger.debug(f"Start compiling {texfile}")
find_subj_tex_files pytex.pdflatex(corr_fname)
+ " -exec pdflatex {} \\;" logger.debug(f"End compiling {texfile}")
) pdf_files.append(str(corr_fname[:-4] + ".pdf"))
os.system( deactivate_printanswers(corr_fname)
find_subj_tex_files
+ " -exec sed -i 's/\\\\printanswers/%\\\\printanswers/g' {} \\;" if not options.no_join and not options.no_compil:
pdfjoin(
pdf_files,
template.replace('tpl', "corr").replace(".tex",".pdf"),
working_dir,
rm_pdfs=1
) )
if not options.dirty: if not options.dirty:
os.system("rm *.aux *.log") pytex.clean(working_dir)
if not options.no_join:
print(Path("./").abspath())
print(
"pdfjam `find ./ -iname '[0-9]*.pdf'` -o corr"
+ Path(output_dir).namebase + ".pdf"
)
os.system(
"pdfjam `find ./ -iname '[0-9]*.pdf'` -o corr"
+ Path(output_dir).namebase + ".pdf"
)
# os.system("pdfjam *.pdf -o all" + Path(output).namebase + ".pdf")
print(r"find ./ -iname '[0-9]*.pdf' -exec rm -f {} \;")
os.system(r"find ./ -iname '[0-9]*.pdf' -exec rm -f {} \;")
# cwd.cd()
def main(): def main():
@ -173,11 +178,11 @@ def main():
help="File with the template. The name should have the following form tpl_... ." help="File with the template. The name should have the following form tpl_... ."
) )
parser.add_option( parser.add_option(
"-o", "-w",
"--output-dir", "--working-dir",
action="store", action="store",
type="string", type="string",
dest="output_dir", dest="working_dir",
help="Where fed templates and compiled files will be placed" help="Where fed templates and compiled files will be placed"
) )
parser.add_option( parser.add_option(