#!/usr/bin/env python # encoding: utf-8 """ Producing then compiling templates """ import os import csv import math as m import optparse import random as rd import sys from path import Path from pymath.calculus.polynomDeg2 import Polynom_deg2 from pymath import Dataset from pymath import Equation from pymath import Expression from pymath import Fraction from pymath import Polynom from pymath import random_pythagore from pymath import random_str from pymath import WeightedDataset from .texenv import texenv 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 csv_file: list_infos = list(csv.DictReader(csv_file, delimiter=";")) for (i, info) in enumerate(list_infos): info['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 output_file: output_file.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(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( "-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() 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