Add texenv curstomisations and fix relative import bugs with pdflatex

This commit is contained in:
Benjamin Bertrand 2017-04-17 13:09:22 +03:00
parent c017b4aafc
commit 3de627fd1d
2 changed files with 37 additions and 33 deletions

View File

@ -10,7 +10,7 @@ import math as m
import subprocess import subprocess
import random as rd import random as rd
from path import Path from path import Path
from .texenv import texenv from .texenv import *
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -20,15 +20,6 @@ EXPORT_DICT.update(rd.__dict__)
EXPORT_DICT.update(__builtins__) EXPORT_DICT.update(__builtins__)
def add_filter(filtername, filterfunc):
""" Append the filter to texenv
:param filtername: The filter name
:param filterfunc: The fiter function
"""
texenv.filters[filtername] = filterfunc
def update_export_dict(new_dict): def update_export_dict(new_dict):
"""Update global variable with new_dict """Update global variable with new_dict
@ -78,24 +69,28 @@ def pdflatex(latex_file, output_dir=""):
""" """
if not output_dir: if not output_dir:
output_dir = Path(latex_file).dirname().abspath() output_dir = Path(latex_file).dirname().abspath()
logger.debug(f"output_dir for dflatex is {output_dir}")
pwd = Path('./').abspath()
Path(output_dir).cd()
compilation = subprocess.Popen( compilation = subprocess.Popen(
[ [
"pdflatex", "pdflatex",
f"-output-directory={output_dir}", # f"-output-directory={output_dir}",
str(latex_file), # "-halt-on-error",
"-interaction=nonstopmode",
str(Path(latex_file).name),
], ],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
# shell=True # shell=True
) )
out, err = compilation.communicate(timeout=5) for line in compilation.stdout:
if err: if b"Error" in line:
logger.error(err) logger.error(line)
if out: logger.debug(f"{latex_file.name} has been compiled in {output_dir}")
logger.info(out) pwd.cd()
logger.debug(f"{latex_file} has been compiled in {output_dir}")
def clean(dirname=".", garbages=["*.aux", "*.log"]): def clean(dirname=".", garbages=["*.aux", "*.log"]):

View File

@ -6,7 +6,12 @@ import jinja2
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
__all__ = ["texenv"] __all__ = [
"texenv",
"add_filter",
"add_pkg_loader",
"add_path_loader",
]
# Definition of jinja syntax for latex # Definition of jinja syntax for latex
texenv = jinja2.Environment( texenv = jinja2.Environment(
@ -25,23 +30,27 @@ texenv = jinja2.Environment(
extensions=['jinja2.ext.do'] extensions=['jinja2.ext.do']
) )
def feed_template(target, datas, template): def add_filter(filtername, filterfunc):
""" Get the template and feed it to create reports """ Append the filter to texenv
:param target: path where the report will be saved :param filtername: The filter name
:param datas: dictonnary to feed the template :param filterfunc: The fiter function
:param template: the template
""" """
logger.info("Getting template {}".format(template)) texenv.filters[filtername] = filterfunc
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: def add_pkg_loader(pkgname, tpl):
f.write(report.render(**datas, directory=path_to_target)) """ Add a package where templates can be choosen """
logger.info("{} est construit! Ya plus qu'à compiler!".format(target)) texenv.loader.loaders.append(jinja2.PackageLoader(
pkgname,
tpl
))
def add_path_loader(path):
""" Add a path where templates can be choosen """
texenv.loader.loaders.append(jinja2.FileSystemLoader(path))
# ----------------------------- # -----------------------------
# Reglages pour 'vim' # Reglages pour 'vim'