2013-09-27 18:09:38 +00:00
#!/usr/bin/env python
# encoding: utf-8
2014-09-02 09:20:09 +00:00
import os
2013-09-27 20:21:46 +00:00
import sys
import optparse
2014-09-03 17:30:09 +00:00
import csv
2014-08-29 13:29:57 +00:00
from path import path
2014-08-29 12:33:04 +00:00
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
2016-01-24 05:33:40 +00:00
from pymath import Expression
from pymath import Polynom
from pymath . calculus . polynomDeg2 import Polynom_deg2
from pymath import Fraction
from pymath import random_str
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 , \
2015-04-23 13:48:00 +00:00
" random_str " : random_str , \
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 ) :
2014-08-29 12:33:04 +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
cwd = path ( " ./ " ) . abspath ( )
template_file = path ( options . template )
2014-01-19 20:37:46 +00:00
2013-09-27 20:21:46 +00:00
if options . output :
2014-08-29 13:29:57 +00:00
output = path ( options . output )
2013-09-27 20:21:46 +00:00
else :
2014-08-29 13:29:57 +00:00
# Template should be named tpl_... tpl will replace by the number/name of the version
output = path ( template_file . dirname ( ) ) / path ( template_file . name [ 3 : ] )
2014-09-03 17:30:09 +00:00
if not options . csv_file :
2014-09-07 09:22:55 +00:00
list_infos = [ { " num " : 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 ) :
a [ ' num ' ] = i + 1
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
2014-11-21 17:27:18 +00:00
tmp_pdf = [ ]
2014-09-03 17:30:09 +00:00
for infos in list_infos :
2014-11-13 16:53:23 +00:00
#print("_______" + str(infos))
2014-09-03 17:30:09 +00:00
dest = path ( str ( infos [ ' num ' ] ) + output )
2014-11-21 17:27:18 +00:00
tmp_pdf . append ( dest . namebase + " .pdf " )
2013-09-27 20:21:46 +00:00
with open ( dest , ' w ' ) as f :
2015-02-08 16:04:05 +00:00
f . write ( template . render ( infos = infos , * * export_dict ) )
2013-10-18 11:48:43 +00:00
2014-09-02 09:20:09 +00:00
if not options . no_compil :
os . system ( " pdflatex " + dest )
2014-11-13 17:05:37 +00:00
if not options . dirty :
os . system ( " rm *.aux *.log " )
if not options . no_join :
2014-11-21 17:27:18 +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")
print ( " rm " + " " . join ( tmp_pdf ) )
os . system ( " rm " + " " . join ( tmp_pdf ) )
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 ( )
2014-09-02 09:20:09 +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_... . " )
2014-08-29 13:29:57 +00:00
parser . add_option ( " -o " , " --output " , action = " store " , type = " string " , dest = " output " , help = " Base name for output ) " )
2014-09-03 17:30:09 +00:00
parser . add_option ( " -c " , " --csv " , action = " store " , type = " string " , dest = " csv_file " , help = " Filename of the csv file where informations are stored " )
2014-09-02 09:20:09 +00:00
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 " )
2014-09-02 09:20:09 +00:00
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 " )
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