Bopytex/opytex/opytex.py

140 lines
5.1 KiB
Python
Raw Normal View History

2013-09-27 18:09:38 +00:00
#!/usr/bin/env python
# encoding: utf-8
import os
2013-09-27 20:21:46 +00:00
import sys
import optparse
2014-09-03 17:30:09 +00:00
import csv
2017-04-04 10:34:49 +00:00
from path import Path
2015-05-16 08:42:39 +00:00
from .texenv import texenv
2015-02-08 16:04:05 +00:00
import math as m
2015-03-19 21:05:00 +00:00
import random as rd
from pymath import Expression
from pymath import Polynom
2016-05-10 11:25:09 +00:00
from pymath import Equation
from pymath.calculus.polynomDeg2 import Polynom_deg2
from pymath import Fraction
from pymath import random_str
2016-02-02 05:41:02 +00:00
from pymath import random_pythagore
2016-08-17 06:38:02 +00:00
from pymath import Dataset
from pymath import WeightedDataset
2015-04-23 13:48:00 +00:00
2015-01-06 08:22:52 +00:00
2015-02-08 16:04:05 +00:00
export_dict = {}
export_dict.update(m.__dict__)
2015-03-19 21:05:00 +00:00
export_dict.update(rd.__dict__)
2015-05-16 08:50:03 +00:00
export_dict.update(__builtins__)
2015-02-08 16:04:05 +00:00
export_dict.update({"Expression":Expression,\
2015-01-06 08:22:52 +00:00
"Polynom":Polynom,\
2015-03-19 21:05:00 +00:00
"Polynom_deg2":Polynom_deg2,\
2015-01-06 08:22:52 +00:00
"Fraction":Fraction,\
2016-05-10 11:25:09 +00:00
"Equation":Equation,\
2015-04-23 13:48:00 +00:00
"random_str": random_str,\
2016-02-02 05:41:02 +00:00
"random_pythagore":random_pythagore,\
2016-08-17 06:38:02 +00:00
"Dataset": Dataset, \
"WeightedDataset": WeightedDataset,\
2015-02-08 16:04:05 +00:00
})
2013-09-27 20:21:46 +00:00
2015-05-16 08:42:39 +00:00
def produce_and_compile(options):
#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-04 10:34:49 +00:00
template_file = Path(options.template)
2014-01-19 20:37:46 +00:00
2017-04-04 10:34:49 +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
list_infos = [{"num": "{:02.0f}".format(i+1)} for i in range(options.num_subj)]
2014-09-03 17:30:09 +00:00
else:
with open(options.csv_file, 'r', encoding = 'ISO-8859-1') as f:
list_infos = list(csv.DictReader(f, delimiter=";"))
for (i,a) in enumerate(list_infos):
2016-02-02 05:45:37 +00:00
a['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:
#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")
with open( dest, 'w') as f:
f.write(template.render( infos = infos, **export_dict ))
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())
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:
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:
2016-02-02 07:12:28 +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())
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")
2016-02-02 07:12:28 +00:00
print("find ./ -iname '[0-9]*.pdf' -exec rm -f {} \;")
os.system("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
2015-05-16 08:42:39 +00:00
def main():
2013-09-27 20:21:46 +00:00
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_... .")
2016-02-02 07:12:28 +00:00
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")
2013-10-18 11:48:43 +00:00
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")
2014-11-13 17:05:37 +00:00
parser.add_option("-j","--no-join", action="store_true", dest="no_join", help="Do not join pdf and clean single pdf")
2016-02-02 07:12:28 +00:00
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")
2013-09-27 20:21:46 +00:00
2014-09-03 17:30:09 +00:00
2013-09-27 20:21:46 +00:00
(options, args) = parser.parse_args()
if not options.template:
print("I need a template!")
sys.exit(0)
2015-05-16 08:42:39 +00:00
produce_and_compile(options)
if __name__ == '__main__':
main()
2013-09-27 18:09:38 +00:00
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del