Bopytex/opytex/opytex.py

231 lines
6.1 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
2014-09-03 17:30:09 +00:00
import csv
2015-02-08 16:04:05 +00:00
import math as m
2017-04-16 08:36:27 +00:00
import optparse
2015-03-19 21:05:00 +00:00
import random as rd
2017-04-16 08:36:27 +00:00
import sys
from path import Path
from pymath.calculus.polynomDeg2 import Polynom_deg2
2017-04-16 08:36:27 +00:00
from pymath import Dataset
from pymath import Equation
from pymath import Expression
from pymath import Fraction
2017-04-16 08:36:27 +00:00
from pymath import Polynom
2016-02-02 05:41:02 +00:00
from pymath import random_pythagore
2017-04-16 08:36:27 +00:00
from pymath import random_str
2016-08-17 06:38:02 +00:00
from pymath import WeightedDataset
2017-04-16 08:36:27 +00:00
from .texenv import texenv
2015-04-23 13:48:00 +00:00
2017-04-16 08:36:27 +00:00
EXPORT_DICT = {}
EXPORT_DICT.update(m.__dict__)
EXPORT_DICT.update(rd.__dict__)
EXPORT_DICT.update(__builtins__)
EXPORT_DICT.update({
"Expression": Expression,
"Polynom": Polynom,
"Polynom_deg2": Polynom_deg2,
"Fraction": Fraction,
"Equation": Equation,
"random_str": random_str,
"random_pythagore": random_pythagore,
"Dataset": Dataset,
"WeightedDataset": WeightedDataset,
})
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):
2017-04-16 08:36:27 +00:00
# template = report_renderer.get_template(options.template)
template = texenv.get_template(options.template)
2013-09-27 20:21:46 +00:00
2014-08-29 13:29:57 +00:00
# Saving place
2017-04-04 10:34:49 +00:00
cwd = Path("./").abspath()
2014-08-29 13:29:57 +00:00
2017-04-16 08:36:27 +00:00
template_file = Path(options.template)
2014-01-19 20:37:46 +00:00
2017-04-16 08:36:27 +00:00
output = Path(template_file.dirname()) / Path(template_file.name[3:])
2014-08-29 13:29:57 +00:00
2014-09-03 17:30:09 +00:00
if not options.csv_file:
2016-02-02 05:45:37 +00:00
# {:02:0f} means that we want a 2 digits number
2017-04-16 08:36:27 +00:00
list_infos = [
{"num": "{:02.0f}".format(i+1)}
for i in range(options.num_subj)
]
2014-09-03 17:30:09 +00:00
else:
2017-04-16 08:36:27 +00:00
with open(options.csv_file, 'r', encoding='ISO-8859-1') as csv_file:
list_infos = list(csv.DictReader(csv_file, delimiter=";"))
for (i, info) in enumerate(list_infos):
info['num'] = "{:02.0f}".format(i+1)
2014-09-03 17:30:09 +00:00
2014-08-29 13:40:36 +00:00
if output.dirname() != "":
2014-08-29 13:29:57 +00:00
output.dirname().cd()
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:
tmp_pdf = []
for infos in list_infos:
2017-04-16 08:36:27 +00:00
# print("_______" + str(infos))
2017-04-04 10:34:49 +00:00
dest = Path(str(infos['num']) + output)
2016-02-02 07:12:28 +00:00
tmp_pdf.append(dest.namebase + ".pdf")
2017-04-16 08:36:27 +00:00
with open(dest, 'w') as output_file:
output_file.write(
template.render(
infos=infos,
**EXPORT_DICT
)
)
2016-02-02 07:12:28 +00:00
if not options.no_compil:
os.system("pdflatex " + dest)
if not options.dirty:
os.system("rm *.aux *.log")
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(tmp_pdf)
+ " -o all"
+ Path(output).namebase
+ ".pdf"
)
os.system(
"pdfjam "
+ " ".join(tmp_pdf)
+ " -o all"
+ Path(output).namebase
+ ".pdf"
)
# os.system("pdfjam *.pdf -o all" + Path(output).namebase + ".pdf")
2016-02-02 07:12:28 +00:00
print("rm " + " ".join(tmp_pdf))
os.system("rm " + " ".join(tmp_pdf))
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).namebase + ".pdf"
)
os.system(
"pdfjam `find ./ -iname '[0-9]*.pdf'` -o corr"
+ Path(output).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 {} \;")
2013-09-27 20:21:46 +00:00
2014-08-29 13:29:57 +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(
"-C",
"--csv",
action="store",
type="string",
dest="csv_file",
help="Filename of the csv file where informations on subjects are stored"
)
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()
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