68cdb62f4c
Work with feed and compile for time being. Nothing for correction or pdfjam
247 lines
6.8 KiB
Python
Executable File
247 lines
6.8 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# encoding: utf-8
|
|
|
|
"""
|
|
Producing then compiling templates
|
|
"""
|
|
|
|
import os
|
|
import logging
|
|
import optparse
|
|
import sys
|
|
|
|
from path import Path
|
|
import pytex
|
|
import pymath
|
|
|
|
formatter = logging.Formatter('%(name)s :: %(levelname)s :: %(message)s')
|
|
steam_handler = logging.StreamHandler()
|
|
steam_handler.setLevel(logging.DEBUG)
|
|
steam_handler.setFormatter(formatter)
|
|
# création de l'objet logger qui va nous servir à écrire dans les logs
|
|
# logger = logging.getLogger("opytex")
|
|
# on met le niveau du logger à DEBUG, comme ça il écrit tout
|
|
logger = logging.getLogger(__name__)
|
|
logger.setLevel(logging.DEBUG)
|
|
logger.addHandler(steam_handler)
|
|
|
|
MAPYTEX_TOOLS = {
|
|
"Expression": pymath.Expression,
|
|
"Polynom": pymath.Polynom,
|
|
"Fraction": pymath.Fraction,
|
|
"Equation": pymath.Equation,
|
|
"random_str": pymath.random_str,
|
|
"random_pythagore": pymath.random_pythagore,
|
|
"Dataset": pymath.Dataset,
|
|
"WeightedDataset": pymath.WeightedDataset,
|
|
}
|
|
|
|
pytex.update_export_dict(MAPYTEX_TOOLS)
|
|
|
|
|
|
def produce_and_compile(options):
|
|
""" Produce and compile subjects
|
|
"""
|
|
# template = report_renderer.get_template(options.template)
|
|
# template = texenv.get_template(options.template)
|
|
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
|
|
/ Path(template.replace("tpl", infos["num"]))
|
|
)
|
|
logger.debug(f"Feeding template toward {dest}")
|
|
tex_files.append(dest)
|
|
pytex.feed(
|
|
template,
|
|
{"infos": infos},
|
|
output=dest,
|
|
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:
|
|
logger.debug(f"Start compiling {texfile}")
|
|
pytex.pdflatex(texfile)
|
|
logger.debug(f"End compiling {texfile}")
|
|
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"
|
|
)
|
|
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' {} \\;"
|
|
)
|
|
|
|
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' {} \\;"
|
|
)
|
|
|
|
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()
|
|
|
|
|
|
def main():
|
|
parser = optparse.OptionParser()
|
|
parser.add_option(
|
|
"-t",
|
|
"--template",
|
|
action="store",
|
|
type="string",
|
|
dest="template",
|
|
help="File with the template. The name should have the following form tpl_... ."
|
|
)
|
|
parser.add_option(
|
|
"-o",
|
|
"--output-dir",
|
|
action="store",
|
|
type="string",
|
|
dest="output_dir",
|
|
help="Where fed templates and compiled files will be placed"
|
|
)
|
|
parser.add_option(
|
|
"-N",
|
|
"--number_subjects",
|
|
action="store",
|
|
type="int",
|
|
dest="num_subj",
|
|
default = 1,
|
|
help="The number of subjects to make"
|
|
)
|
|
parser.add_option(
|
|
"-d",
|
|
"--dirty",
|
|
action="store_true",
|
|
dest="dirty",
|
|
help="Do not clean after compilation"
|
|
)
|
|
parser.add_option(
|
|
"-n",
|
|
"--no-compile",
|
|
action="store_true",
|
|
dest="no_compil",
|
|
help="Do not compile source code"
|
|
)
|
|
parser.add_option(
|
|
"-j",
|
|
"--no-join",
|
|
action="store_true",
|
|
dest="no_join",
|
|
help="Do not join pdf and clean single pdf"
|
|
)
|
|
parser.add_option(
|
|
"-O",
|
|
"--only-corr",
|
|
action="store_true",
|
|
dest="only_corr",
|
|
help="Create and compile only correction from existing subjects"
|
|
)
|
|
parser.add_option(
|
|
"-c",
|
|
"--corr",
|
|
action="store_true",
|
|
dest="corr",
|
|
help="Create and compile correction while making subjects"
|
|
)
|
|
|
|
(options, _) = parser.parse_args()
|
|
|
|
logger.debug(f"CI parser gets {options}")
|
|
|
|
if not options.template:
|
|
print("I need a template!")
|
|
sys.exit(0)
|
|
|
|
produce_and_compile(options)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|
|
|
|
|
|
# -----------------------------
|
|
# Reglages pour 'vim'
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
# cursor: 16 del
|