From 6df2dc2b0158b55ecf3c4324ec2ff971aa017f8c Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Sun, 16 Apr 2017 11:36:27 +0300 Subject: [PATCH] Pep corrections --- opytex/__main__.py | 4 +- opytex/opytex.py | 223 +++++++++++++++++++++++++++++++-------------- opytex/texenv.py | 40 ++++---- 3 files changed, 177 insertions(+), 90 deletions(-) diff --git a/opytex/__main__.py b/opytex/__main__.py index 807ca8a..5b4a049 100644 --- a/opytex/__main__.py +++ b/opytex/__main__.py @@ -2,14 +2,12 @@ # encoding: utf-8 - from .opytex import main main() - # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: -# cursor: 16 del +# cursor: 16 del diff --git a/opytex/opytex.py b/opytex/opytex.py index 074af98..3a2c945 100755 --- a/opytex/opytex.py +++ b/opytex/opytex.py @@ -1,61 +1,68 @@ #!/usr/bin/env python # encoding: utf-8 -import os -import sys -import optparse -import csv -from path import Path +""" +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 -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, + }) -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 = report_renderer.get_template(options.template) template = texenv.get_template(options.template) # Saving place cwd = Path("./").abspath() - template_file = Path(options.template) + template_file = Path(options.template) - output = Path(template_file.dirname()) / Path(template_file.name[3:]) + 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)] + 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) + 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() @@ -67,11 +74,16 @@ def produce_and_compile(options): else: tmp_pdf = [] for infos in list_infos: - #print("_______" + str(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 )) + 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) @@ -81,46 +93,125 @@ def produce_and_compile(options): 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( + "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' {} \\;") + 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' {} \\;") + 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 {} \;") + 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") + 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() + (options, _) = parser.parse_args() if not options.template: print("I need a template!") @@ -128,12 +219,12 @@ def main(): produce_and_compile(options) + if __name__ == '__main__': main() - # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: -# cursor: 16 del +# cursor: 16 del diff --git a/opytex/texenv.py b/opytex/texenv.py index 462b5e3..81dd01e 100644 --- a/opytex/texenv.py +++ b/opytex/texenv.py @@ -1,26 +1,26 @@ #!/usr/bin/env python # encoding: utf-8 -import jinja2, os +import os +import jinja2 __all__ = ["texenv"] # Definition of jinja syntax for latex texenv = jinja2.Environment( - block_start_string = '\Block{', - # Gros WTF!! Si on le met en maj ça ne marche pas alors que c'est en maj dans le template... - block_end_string = '}', - variable_start_string = '\Var{', - variable_end_string = '}', - line_statement_prefix = '%-', - line_comment_prefix = '%#', - loader = jinja2.FileSystemLoader(os.path.abspath('.')), - extensions = ['jinja2.ext.do'] + block_start_string='\Block{', + block_end_string='}', + variable_start_string='\Var{', + variable_end_string='}', + line_statement_prefix='%-', + line_comment_prefix='%#', + loader=jinja2.FileSystemLoader(os.path.abspath('.')), + extensions=['jinja2.ext.do'] ) -# Filters -def do_calculus(steps, name = "A", sep = "=", end = "", joining = " \\\\ \n"): +# Filters +def do_calculus(steps, name="A", sep="=", end="", joining=" \\\\ \n"): """Display properly the calculus Generate this form string: @@ -32,9 +32,14 @@ def do_calculus(steps, name = "A", sep = "=", end = "", joining = " \\\\ \n"): """ - ans = joining.join([name + " & " + sep + " & " + str(s) + end for s in steps]) + ans = joining.join([ + name + " & " + + sep + " & " + + str(s) + end for s in steps + ]) return ans + texenv.filters['calculus'] = do_calculus from random import shuffle @@ -42,14 +47,7 @@ texenv.filters['shuffle'] = shuffle -if __name__ == '__main__': - from pymath.expression import Expression - exp = Expression("2/4 + 18") - print(do_calculus(exp.simplify())) - - - # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: -# cursor: 16 del +# cursor: 16 del