Created
July 28, 2020 23:07
-
-
Save jeetsukumaran/e1d367dfcd23b904834c2598844e8e37 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import re | |
import os | |
import pathlib | |
import sys | |
import argparse | |
import codecs | |
class Colors(object): | |
""" | |
ANSI color codes | |
from: | |
https://gist.github.com/rene-d/9e584a7dd2935d0f461904b9f2950007 | |
""" | |
BLACK = "\033[0;30m" | |
RED = "\033[0;31m" | |
GREEN = "\033[0;32m" | |
BROWN = "\033[0;33m" | |
BLUE = "\033[0;34m" | |
PURPLE = "\033[0;35m" | |
CYAN = "\033[0;36m" | |
LIGHTGRAY = "\033[0;37m" | |
DARKGRAY = "\033[1;30m" | |
LIGHTRED = "\033[1;31m" | |
LIGHTGREEN = "\033[1;32m" | |
YELLOW = "\033[1;33m" | |
LIGHTBLUE = "\033[1;34m" | |
LIGHTPURPLE = "\033[1;35m" | |
LIGHTCYAN = "\033[1;36m" | |
LIGHTWHITE = "\033[1;37m" | |
BOLD = "\033[1m" | |
FAINT = "\033[2m" | |
ITALIC = "\033[3m" | |
UNDERLINE = "\033[4m" | |
BLINK = "\033[5m" | |
NEGATIVE = "\033[7m" | |
CROSSED = "\033[9m" | |
END = "\033[0m" | |
# cancel SGR codes if we don't write to a terminal | |
if not __import__("sys").stdout.isatty(): | |
for _ in dir(): | |
if isinstance(_, str) and _[0] != "_": | |
locals()[_] = "" | |
else: | |
# set Windows console in VT mode | |
if __import__("platform").system() == "Windows": | |
kernel31 = __import__("ctypes").windll.kernel32 | |
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7) | |
del kernel32 | |
class NoColors(object): | |
BLACK = "" | |
RED = "" | |
GREEN = "" | |
BROWN = "" | |
BLUE = "" | |
PURPLE = "" | |
CYAN = "" | |
LIGHTGRAY = "" | |
DARKGRAY = "" | |
LIGHTRED = "" | |
LIGHTGREEN = "" | |
YELLOW = "" | |
LIGHTBLUE = "" | |
LIGHTPURPLE = "" | |
LIGHTCYAN = "" | |
LIGHTWHITE = "" | |
BOLD = "" | |
FAINT = "" | |
ITALIC = "" | |
UNDERLINE = "" | |
BLINK = "" | |
NEGATIVE = "" | |
CROSSED = "" | |
END = "" | |
def main(): | |
parser = argparse.ArgumentParser(description=None) | |
parser.add_argument( | |
"logfile", | |
metavar="LOGFILE", | |
nargs="+", | |
help="Path(s) to LaTeX log file(s).") | |
parser.add_argument("--no-colors", | |
action="store_true", | |
default=False, | |
help="Do not colorize messages.") | |
parser.add_argument("--encoding", | |
default="latin-1", | |
help="Character encoding of source files [default='%(default)s'].") | |
parser.add_argument("--show-no-errors", | |
action="store_true", | |
default=False, | |
help="Also show log files that do not reference errors.") | |
args = parser.parse_args() | |
if args.no_colors: | |
colors = NoColors | |
else: | |
colors = Colors | |
n = r"[\r\n]{0,1}?" | |
p = "LaTeX Error:".format(n) | |
p = n.join(p) | |
p.replace(" ", " {}".format(n)) | |
p = p + r"\s*([^\r\n]+?)[\r\n]{2}" | |
# p = r"{}\s*([^\r\n]+?)[\r\n]{{2}}".format(p) | |
# p = r"\s*({}[^\r\n]+?)[\r\n]{{2}}".format(p) | |
pattern = re.compile(p, re.MULTILINE | re.DOTALL) | |
colors_d = { | |
# "file_color": colors.BLUE + colors.BOLD, | |
"file_color": "", | |
"error_color": colors.RED + colors.BOLD, | |
"noerror_color": colors.GREEN + colors.BOLD, | |
"end_color": colors.END, | |
} | |
for src_path in args.logfile: | |
if src_path == "-": | |
# with open(0, 'rb') as src: | |
# logdata = src.read().decode(args.encoding) | |
logdata = sys.stdin.buffer.raw.read().decode(args.encoding) | |
else: | |
with open(src_path, "rb") as src: | |
logdata = src.read() | |
logdata = logdata.decode(args.encoding) | |
results = pattern.findall(logdata) | |
if results or args.show_no_errors: | |
print("{file_color}[{src_path}]{end_color}".format(src_path=src_path, **colors_d)) | |
# if args.show_no_errors and not results: | |
# print(" : {noerror_color}No errors{end_color}".format(**colors_d)) | |
for result in results: | |
result_str = result.replace("\n", "").replace("\r", "") | |
print(" : {error_color}{err_str}{end_color}".format(err_str=result_str, **colors_d)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment