Improve latex error bubble up

This commit is contained in:
Bertrand Benjamin 2018-05-20 11:59:10 +03:00
parent 36d182c860
commit a122e2bba1
2 changed files with 74 additions and 3 deletions

View File

@ -0,0 +1,60 @@
#!/usr/bin/env python
# encoding: utf-8
"""
Parsing latex error to bubble up import ones
"""
from functools import wraps
def coroutine(func):
@wraps(func)
def start(*args, **kwargs):
cr = func(*args, **kwargs)
next(cr)
return cr
return start
@coroutine
def generic_sink(func):
""" Generic sink
:param function: function that define the end of the sink
>>> print_sink = generic_sink(print)
>>> print_sink.send("coucou")
coucou
"""
while True:
c = (yield)
if c:
func(c)
@coroutine
def filter_errors(target):
""" Filter pdflatex log to bubble up error
https://en.wikibooks.org/wiki/LaTeX/Errors_and_Warnings
>>> s = generic_sink(print)
>>> tex_filter = filter_errors(s)
>>> tex_filter.send("! Undefined control sequence.")
! Undefined control sequence.
>>> tex_filter.send("l.43 \\includegraphics")
l.43 \\includegraphics
>>> tex_filter.send("l.43 \\includegraphics")
>>>
"""
while True:
line = (yield)
if line.startswith("!"):
target.send(line)
line = (yield)
while not line.startswith('See'):
target.send(line)
line = (yield)
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@ -11,8 +11,18 @@ import subprocess
import random as rd import random as rd
from path import Path from path import Path
from .texenv import * from .texenv import *
from .latex_error_parser import generic_sink, filter_errors
formatter = logging.Formatter('%(name)s :: %(levelname)s :: %(message)s')
steam_handler = logging.StreamHandler()
steam_handler.setLevel(logging.DEBUG)
steam_handler.setFormatter(formatter)
# création de l'objet logger qui va nous servir à écrire dans les logs
# on met le niveau du logger à DEBUG, comme ça il écrit tout
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger.addHandler(steam_handler)
EXPORT_DICT = {} EXPORT_DICT = {}
EXPORT_DICT.update(m.__dict__) EXPORT_DICT.update(m.__dict__)
@ -82,7 +92,7 @@ def pdflatex(latex_file, output_dir=""):
compilation = subprocess.Popen( compilation = subprocess.Popen(
[ [
"pdflatex", "pdflatex",
# f"-output-directory={output_dir}", f"-output-directory={output_dir}",
# "-halt-on-error", # "-halt-on-error",
"-interaction=nonstopmode", "-interaction=nonstopmode",
"-shell-escape", "-shell-escape",
@ -93,9 +103,10 @@ def pdflatex(latex_file, output_dir=""):
# shell=True # shell=True
) )
latex_error_logger = filter_errors(generic_sink(logger.error))
for line in compilation.stdout: for line in compilation.stdout:
if b"Error" in line: latex_error_logger.send(line.decode("utf-8").rstrip('\r\n'))
logger.error(line)
logger.debug(f"{latex_file.name} has been compiled in {output_dir}") logger.debug(f"{latex_file.name} has been compiled in {output_dir}")
pwd.cd() pwd.cd()