Bopytex/opytex/opytex.py

247 lines
6.8 KiB
Python
Raw Normal View History

2013-09-27 18:09:38 +00:00
#!/usr/bin/env python
# encoding: utf-8
2017-04-16 08:36:27 +00:00
"""
Producing then compiling templates
"""
import os
import logging
2017-04-16 08:36:27 +00:00
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)
2015-01-06 08:22:52 +00:00
2013-09-27 20:21:46 +00:00
2015-05-16 08:42:39 +00:00
def produce_and_compile(options):
""" Produce and compile subjects
"""
2017-04-16 08:36:27 +00:00
# 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}")
2013-09-27 20:21:46 +00:00
2014-08-29 13:29:57 +00:00
# Saving place
# cwd = Path("./").abspath()
2014-01-19 20:37:46 +00:00
# template_file = Path(options.template)
2014-08-29 13:29:57 +00:00
list_infos = [
{"num": f"{i+1:02d}"}
for i in range(options.num_subj)
]
2014-09-03 17:30:09 +00:00
# if output.dirname() != "":
# output.dirname().cd()
2014-08-29 13:29:57 +00:00
# output = output.name
2013-09-27 20:21:46 +00:00
2016-02-02 07:12:28 +00:00
if options.only_corr:
options.corr = True
else:
tex_files = []
2016-02-02 07:12:28 +00:00
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}")
2016-02-02 07:12:28 +00:00
if not options.dirty:
pytex.clean(output_dir)
2016-02-02 07:12:28 +00:00
if not options.no_join:
2017-04-04 10:34:49 +00:00
print(Path("./").abspath())
2017-04-16 08:36:27 +00:00
print(
"pdfjam "
+ " ".join(pdf_files)
2017-04-16 08:36:27 +00:00
+ " -o all"
+ Path(output_dir).namebase
2017-04-16 08:36:27 +00:00
+ ".pdf"
)
os.system(
"pdfjam "
+ " ".join(pdf_files)
2017-04-16 08:36:27 +00:00
+ " -o all"
+ Path(output_dir).namebase
2017-04-16 08:36:27 +00:00
+ ".pdf"
)
# os.system("pdfjam *.pdf -o all" + Path(output).namebase + ".pdf")
print("rm " + " ".join(pdf_files))
os.system("rm " + " ".join(pdf_files))
2016-02-02 07:12:28 +00:00
if options.corr:
2017-04-16 08:36:27 +00:00
find_subj_tex_files = "find ./ -iname '[0-9]*_*.tex' "
os.system(
find_subj_tex_files
+ " -exec sed -i 's/%\\\\printanswers/\\\\printanswers/g' {} \\;"
)
2013-10-18 11:48:43 +00:00
if not options.no_compil:
2017-04-16 08:36:27 +00:00
os.system(
find_subj_tex_files
+ " -exec pdflatex {} \\;"
)
os.system(
find_subj_tex_files
+ " -exec sed -i 's/\\\\printanswers/%\\\\printanswers/g' {} \\;"
)
2016-02-02 07:12:28 +00:00
if not options.dirty:
os.system("rm *.aux *.log")
2014-11-13 17:05:37 +00:00
2016-02-02 07:12:28 +00:00
if not options.no_join:
2017-04-04 10:34:49 +00:00
print(Path("./").abspath())
2017-04-16 08:36:27 +00:00
print(
"pdfjam `find ./ -iname '[0-9]*.pdf'` -o corr"
+ Path(output_dir).namebase + ".pdf"
2017-04-16 08:36:27 +00:00
)
os.system(
"pdfjam `find ./ -iname '[0-9]*.pdf'` -o corr"
+ Path(output_dir).namebase + ".pdf"
2017-04-16 08:36:27 +00:00
)
# 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 {} \;")
2013-09-27 20:21:46 +00:00
# cwd.cd()
2014-01-19 20:37:46 +00:00
2017-04-16 08:36:27 +00:00
2015-05-16 08:42:39 +00:00
def main():
2013-09-27 20:21:46 +00:00
parser = optparse.OptionParser()
2017-04-16 08:36:27 +00:00
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",
2017-04-16 08:36:27 +00:00
action="store",
type="string",
dest="output_dir",
help="Where fed templates and compiled files will be placed"
2017-04-16 08:36:27 +00:00
)
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",
2017-04-16 08:36:27 +00:00
"--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()
2013-09-27 20:21:46 +00:00
logger.debug(f"CI parser gets {options}")
2013-09-27 20:21:46 +00:00
if not options.template:
print("I need a template!")
sys.exit(0)
2015-05-16 08:42:39 +00:00
produce_and_compile(options)
2017-04-16 08:36:27 +00:00
2015-05-16 08:42:39 +00:00
if __name__ == '__main__':
main()
2013-09-27 18:09:38 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
2017-04-16 08:36:27 +00:00
# cursor: 16 del