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 random as rd
from path import Path
from .texenv import texenv
from .texenv import *
logger = logging.getLogger(__name__)
@ -20,15 +20,6 @@ EXPORT_DICT.update(rd.__dict__)
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):
"""Update global variable with new_dict
@ -78,24 +69,28 @@ def pdflatex(latex_file, output_dir=""):
"""
if not output_dir:
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(
[
"pdflatex",
f"-output-directory={output_dir}",
str(latex_file),
# f"-output-directory={output_dir}",
# "-halt-on-error",
"-interaction=nonstopmode",
str(Path(latex_file).name),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
# shell=True
)
out, err = compilation.communicate(timeout=5)
if err:
logger.error(err)
if out:
logger.info(out)
logger.debug(f"{latex_file} has been compiled in {output_dir}")
for line in compilation.stdout:
if b"Error" in line:
logger.error(line)
logger.debug(f"{latex_file.name} has been compiled in {output_dir}")
pwd.cd()
def clean(dirname=".", garbages=["*.aux", "*.log"]):
@ -111,4 +106,4 @@ def clean(dirname=".", garbages=["*.aux", "*.log"]):
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del
# cursor: 16 del

View File

@ -6,7 +6,12 @@ import jinja2
logger = logging.getLogger(__name__)
__all__ = ["texenv"]
__all__ = [
"texenv",
"add_filter",
"add_pkg_loader",
"add_path_loader",
]
# Definition of jinja syntax for latex
texenv = jinja2.Environment(
@ -25,23 +30,27 @@ texenv = jinja2.Environment(
extensions=['jinja2.ext.do']
)
def feed_template(target, datas, template):
""" Get the template and feed it to create reports
def add_filter(filtername, filterfunc):
""" Append the filter to texenv
:param target: path where the report will be saved
:param datas: dictonnary to feed the template
:param template: the template
:param filtername: The filter name
:param filterfunc: The fiter function
"""
logger.info("Getting template {}".format(template))
report = texenv.get_template(template)
texenv.filters[filtername] = filterfunc
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))
def add_pkg_loader(pkgname, tpl):
""" Add a package where templates can be choosen """
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'