Bopytex/opytex/opytex.py
Benjamin Bertrand 28daa4eedb Change path Path
2017-04-04 13:34:49 +03:00

140 lines
5.1 KiB
Python
Executable File

#!/usr/bin/env python
# encoding: utf-8
import os
import sys
import optparse
import csv
from path import Path
from .texenv import texenv
import math as m
import random as rd
from pymath import Expression
from pymath import Polynom
from pymath import Equation
from pymath.calculus.polynomDeg2 import Polynom_deg2
from pymath import Fraction
from pymath import random_str
from pymath import random_pythagore
from pymath import Dataset
from pymath import WeightedDataset
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,\
})
def produce_and_compile(options):
#template = report_renderer.get_template(options.template)
template = texenv.get_template(options.template)
# Saving place
cwd = Path("./").abspath()
template_file = Path(options.template)
output = Path(template_file.dirname()) / Path(template_file.name[3:])
if not options.csv_file:
# {: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)]
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):
a['num'] = "{:02.0f}".format(i+1)
if output.dirname() != "":
output.dirname().cd()
output = output.name
if options.only_corr:
options.corr = True
else:
tmp_pdf = []
for infos in list_infos:
#print("_______" + str(infos))
dest = Path(str(infos['num']) + output)
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:
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")
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' {} \\;")
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).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("find ./ -iname '[0-9]*.pdf' -exec rm -f {} \;")
os.system("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("-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, args) = parser.parse_args()
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