Fully implement pytex and functionnalyse it
This commit is contained in:
parent
68cdb62f4c
commit
c0f2882310
171
opytex/opytex.py
171
opytex/opytex.py
@ -39,42 +39,82 @@ 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):
|
||||
""" Produce and compile subjects
|
||||
"""
|
||||
# template = report_renderer.get_template(options.template)
|
||||
# template = texenv.get_template(options.template)
|
||||
working_dir = get_working_dir(options)
|
||||
|
||||
if options.only_corr:
|
||||
options.corr = True
|
||||
tex_files = working_dir.files("[0-9]*_*.tex")
|
||||
else:
|
||||
template = Path(options.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 = [
|
||||
{"num": f"{i+1:02d}"}
|
||||
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 = []
|
||||
for infos in list_infos:
|
||||
# dest = Path(str(infos['num']) + output)
|
||||
dest = (
|
||||
output_dir
|
||||
working_dir
|
||||
/ Path(template.replace("tpl", infos["num"]))
|
||||
)
|
||||
logger.debug(f"Feeding template toward {dest}")
|
||||
@ -86,13 +126,7 @@ def produce_and_compile(options):
|
||||
force=1
|
||||
)
|
||||
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:
|
||||
pdf_files = []
|
||||
for texfile in tex_files:
|
||||
@ -102,64 +136,35 @@ def produce_and_compile(options):
|
||||
pdf_files.append(str(texfile[:-4] + ".pdf"))
|
||||
logger.debug(f"Compiled files : {pdf_files}")
|
||||
|
||||
if not options.dirty:
|
||||
pytex.clean(output_dir)
|
||||
|
||||
if not options.no_join:
|
||||
print(Path("./").abspath())
|
||||
print(
|
||||
"pdfjam "
|
||||
+ " ".join(pdf_files)
|
||||
+ " -o all"
|
||||
+ Path(output_dir).namebase
|
||||
+ ".pdf"
|
||||
if not options.no_join and not options.no_compil:
|
||||
pdfjoin(
|
||||
pdf_files,
|
||||
template.replace('tpl', "all").replace(".tex",".pdf"),
|
||||
working_dir,
|
||||
rm_pdfs=1
|
||||
)
|
||||
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:
|
||||
find_subj_tex_files = "find ./ -iname '[0-9]*_*.tex' "
|
||||
os.system(
|
||||
find_subj_tex_files
|
||||
+ " -exec sed -i 's/%\\\\printanswers/\\\\printanswers/g' {} \\;"
|
||||
)
|
||||
|
||||
pdf_files = []
|
||||
for texfile in tex_files:
|
||||
corr_fname = activate_printanswers(texfile)
|
||||
if not options.no_compil:
|
||||
os.system(
|
||||
find_subj_tex_files
|
||||
+ " -exec pdflatex {} \\;"
|
||||
)
|
||||
os.system(
|
||||
find_subj_tex_files
|
||||
+ " -exec sed -i 's/\\\\printanswers/%\\\\printanswers/g' {} \\;"
|
||||
logger.debug(f"Start compiling {texfile}")
|
||||
pytex.pdflatex(corr_fname)
|
||||
logger.debug(f"End compiling {texfile}")
|
||||
pdf_files.append(str(corr_fname[:-4] + ".pdf"))
|
||||
deactivate_printanswers(corr_fname)
|
||||
|
||||
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:
|
||||
os.system("rm *.aux *.log")
|
||||
|
||||
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()
|
||||
pytex.clean(working_dir)
|
||||
|
||||
|
||||
def main():
|
||||
@ -173,11 +178,11 @@ def main():
|
||||
help="File with the template. The name should have the following form tpl_... ."
|
||||
)
|
||||
parser.add_option(
|
||||
"-o",
|
||||
"--output-dir",
|
||||
"-w",
|
||||
"--working-dir",
|
||||
action="store",
|
||||
type="string",
|
||||
dest="output_dir",
|
||||
dest="working_dir",
|
||||
help="Where fed templates and compiled files will be placed"
|
||||
)
|
||||
parser.add_option(
|
||||
|
Loading…
Reference in New Issue
Block a user