From 745e307ffe4b01ece0bb6eaf923c0175399895cd Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand Date: Mon, 17 Apr 2017 13:10:20 +0300 Subject: [PATCH] Remove pytex redonduncy and adapt it! --- notes_tools/generate_reports_main.py | 3 +- notes_tools/reports/__init__.py | 5 +-- notes_tools/reports/eval_reports.py | 27 ++++++++---- notes_tools/reports/filters.py | 10 +++-- notes_tools/reports/produce_compile.py | 43 ++++++++++++++++++ notes_tools/reports/term_reports.py | 44 +++++++++++-------- notes_tools/reports/texenv.py | 61 -------------------------- 7 files changed, 97 insertions(+), 96 deletions(-) create mode 100644 notes_tools/reports/produce_compile.py delete mode 100644 notes_tools/reports/texenv.py diff --git a/notes_tools/generate_reports_main.py b/notes_tools/generate_reports_main.py index 3b0b981..8b61e0b 100644 --- a/notes_tools/generate_reports_main.py +++ b/notes_tools/generate_reports_main.py @@ -9,7 +9,8 @@ steam_handler.setLevel(logging.DEBUG) steam_handler.setFormatter(formatter) # création de l'objet logger qui va nous servir à écrire dans les logs -logger = logging.getLogger("notes_tools") +logger = logging.getLogger() +#logger = logging.getLogger("notes_tools") # on met le niveau du logger à DEBUG, comme ça il écrit tout logger.setLevel(logging.INFO) logger.addHandler(steam_handler) diff --git a/notes_tools/reports/__init__.py b/notes_tools/reports/__init__.py index af6d0d3..acfc5bd 100644 --- a/notes_tools/reports/__init__.py +++ b/notes_tools/reports/__init__.py @@ -1,10 +1,7 @@ #!/usr/bin/env python # encoding: utf-8 -#from .generate_bilan import generate_bilan -from .texenv import texenv - # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: -# cursor: 16 del +# cursor: 16 del diff --git a/notes_tools/reports/eval_reports.py b/notes_tools/reports/eval_reports.py index d2f9459..4301de2 100755 --- a/notes_tools/reports/eval_reports.py +++ b/notes_tools/reports/eval_reports.py @@ -1,16 +1,24 @@ #!/usr/bin/env python # encoding: utf-8 -from notes_tools.tools import extract_flat_marks, get_class_ws, digest_flat_df, evaluation #students_pov, select_eval -from .texenv import feed_template +""" +Evaluation reports +""" + +from notes_tools.tools import ( + extract_flat_marks, + get_class_ws, + digest_flat_df, + evaluation + ) import pandas as pd -import numpy as np -import xlrd from path import Path +from .produce_compile import produce_compile import logging logger = logging.getLogger(__name__) + def eval_info(classe, ds_df): """TODO: Docstring for build_ds_info. @@ -27,6 +35,7 @@ def eval_info(classe, ds_df): eval_info["Trimestre"] = ds_df["Trimestre"].unique()[0] return eval_info + def build_target_name(classe, evalname, path = Path("./")): """ Build the path where the .tex will be sored @@ -37,14 +46,16 @@ def build_target_name(classe, evalname, path = Path("./")): """ return Path(path + "/" + classe + "/report_" + evalname + ".tex") -def eval_report(classe, evalname, path = Path('./'), - template = "tpl_reports_eval.tex"): + +def eval_report(classe, evalname, path = Path('./'), + template="tpl_reports_eval.tex", force=1): """ Generate the report of a evaluation for a class :param classe: the classe name :param evalname: name of the evaluation :param path: path where xlsx are stored :param template: template for the report + :param force: Override existing documents """ ws = get_class_ws(classe, path) @@ -72,10 +83,10 @@ def eval_report(classe, evalname, path = Path('./'), } target = build_target_name(classe, evalname, path) - feed_template(target, datas, template) + produce_compile(template, datas, target, force) # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: -# cursor: 16 del +# cursor: 16 del diff --git a/notes_tools/reports/filters.py b/notes_tools/reports/filters.py index 81087ff..ef92b94 100644 --- a/notes_tools/reports/filters.py +++ b/notes_tools/reports/filters.py @@ -7,9 +7,11 @@ import logging import matplotlib.pyplot as plt logger = logging.getLogger(__name__) + def includegraphics(fig_ax, document_path="./", fig_path="fig/", - prefix="", scale=1): - """ Jinja2 filter which save the figure and return latex includegraphic to display it. + prefix="", scale=1): + """ Jinja2 filter which save the figure + and return latex includegraphic to display it. :param fig_ax: TODO :param path: TODO @@ -34,7 +36,7 @@ def includegraphics(fig_ax, document_path="./", fig_path="fig/", fig.tight_layout() fig.savefig(path_to_file/filename) - logger.info("Graphique {} sauvé à {}".format(filename, path_to_file)) + logger.info(f"Graphique {filename} sauvé à {path_to_file}") plt.close(fig) return "\includegraphics[scale={sc}]{{{f}}}".format(sc = scale, f=Path(fig_path)/filename) @@ -43,4 +45,4 @@ def includegraphics(fig_ax, document_path="./", fig_path="fig/", # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: -# cursor: 16 del +# cursor: 16 del diff --git a/notes_tools/reports/produce_compile.py b/notes_tools/reports/produce_compile.py new file mode 100644 index 0000000..47905d3 --- /dev/null +++ b/notes_tools/reports/produce_compile.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# encoding: utf-8 + +""" +Producing and compiling reports +""" + +from path import Path +import pytex + +import logging +#logger = logging.getLogger(__name__) +logger = logging.getLogger(__name__) + + +from .filters import includegraphics +pytex.add_filter("includegraphics", includegraphics) +# texenv.filters['includegraphics'] = includegraphics + +pytex.add_pkg_loader("notes_tools.reports", "templates") + + +def produce_compile(template, datas, target, force=1): + """ Get the template and feed it to create reports + + :param target: path where the report will be saved + :param datas: dictonnary to feed the template + :param template: the template + """ + directory = Path(target).dirname() + datas.update({"directory": directory}) + pytex.feed(template, datas, target, force) + logger.info(f"{target} has been created") + pytex.pdflatex(target) + logger.info(f"{target} has been compiled") + # pytex.clean() + # logger.info(f"Clean the directory") + + +# ----------------------------- +# Reglages pour 'vim' +# vim:set autoindent expandtab tabstop=4 shiftwidth=4: +# cursor: 16 del diff --git a/notes_tools/reports/term_reports.py b/notes_tools/reports/term_reports.py index 88a8671..64d5649 100755 --- a/notes_tools/reports/term_reports.py +++ b/notes_tools/reports/term_reports.py @@ -1,16 +1,21 @@ #!/usr/bin/env python # encoding: utf-8 -from notes_tools.tools import extract_flat_marks, get_class_ws, digest_flat_df, term -from .texenv import feed_template -import pandas as pd -import numpy as np -import xlrd -from path import Path +""" Making term reports """ import logging +from notes_tools.tools import ( + extract_flat_marks, + get_class_ws, + digest_flat_df, + term + ) +from .produce_compile import produce_compile +from path import Path + logger = logging.getLogger(__name__) + def term_info(classe, ds_df): """TODO: Docstring for build_ds_info. @@ -25,7 +30,8 @@ def term_info(classe, ds_df): eval_info["Trimestre"] = ds_df["Trimestre"].unique()[0] return eval_info -def build_target_name(classe, term_num, path = Path("./")): + +def build_target_name(classe, term_num, path=Path("./")): """ Build the path where the .tex will be sored >>> build_target_name("312", "DS1") @@ -33,20 +39,22 @@ def build_target_name(classe, term_num, path = Path("./")): >>> build_target_name("312", "DS1", Path("plop/")) Path('plop/312/report_DS1.tex') """ - return Path(path + "/{cls}/report_T{term_num}.tex".format(cls=classe, term_num=term_num)) + return Path(path + f"/{classe}/report_T{term_num}.tex") -def term_report(classe, term_num, path = Path('./'), - template = "tpl_reports_term.tex"): + +def term_report(classe, term_num, path=Path('./'), + template="tpl_reports_term.tex", force=1): """ Generate the report of a evaluation for a class :param classe: the classe name :param evalname: name of the evaluation :param path: path where xlsx are stored :param template: template for the report + :param force: Override existing documents """ ws = get_class_ws(classe, path) - logger.info("Worksheets of {} imported".format(classe)) + logger.info(f"Worksheets of {classe} imported") flat_df = extract_flat_marks(ws) logger.info("Worksheets parsed") @@ -54,23 +62,23 @@ def term_report(classe, term_num, path = Path('./'), term_df = flat_df[flat_df['Trimestre'] == term_num] quest_df, exo_df, eval_df = digest_flat_df(term_df) - #conn_df = exo_df[exo_df["Nom"].str.contains('Conn')] report_info = term_info(classe, eval_df) students_pov = term.students_pov(quest_df, exo_df, eval_df) class_pov = term.class_pov(quest_df, exo_df, eval_df) - datas = {"report_info": report_info, - "classe": class_pov, - "students":students_pov, - } + datas = { + "report_info": report_info, + "classe": class_pov, + "students": students_pov, + } target = build_target_name(classe, term_num, path) - feed_template(target, datas, template) + produce_compile(template, datas, target, force) # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: -# cursor: 16 del +# cursor: 16 del diff --git a/notes_tools/reports/texenv.py b/notes_tools/reports/texenv.py deleted file mode 100644 index ae18833..0000000 --- a/notes_tools/reports/texenv.py +++ /dev/null @@ -1,61 +0,0 @@ -#!/usr/bin/env python -# encoding: utf-8 - -import jinja2, os -from .filters import includegraphics - -import logging -logger = logging.getLogger(__name__) - -__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 = '}', - comment_start_string = '\#{', - comment_end_string = '}', - line_statement_prefix = '%-', - line_comment_prefix = '%#', - loader = jinja2.ChoiceLoader([jinja2.PackageLoader("notes_tools.reports", "templates"), - jinja2.FileSystemLoader(['./']), - ]), - extensions = ['jinja2.ext.do'] -) - -# Filters - -from .filters import includegraphics -texenv.filters['includegraphics'] = includegraphics - -def feed_template(target, datas, template): - """ Get the template and feed it to create reports - - :param target: path where the report will be saved - :param datas: dictonnary to feed the template - :param template: the template - """ - logger.info("Getting template {}".format(template)) - report = texenv.get_template(template) - - path_to_target = target.dirname() - if not path_to_target.exists(): - path_to_target.mkdir() - - with open(target, "w") as f: - f.write(report.render(**datas, directory=path_to_target)) - logger.info("{} est construit! Ya plus qu'à compiler!".format(target)) - -if __name__ == '__main__': - print(texenv.list_templates()) - texenv.get_template("tpl_reports.tex") - - - -# ----------------------------- -# Reglages pour 'vim' -# vim:set autoindent expandtab tabstop=4 shiftwidth=4: -# cursor: 16 del