forked from inakleinbottle/texoutparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexoutparse.py
294 lines (231 loc) · 8.73 KB
/
texoutparse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
"""
Parser for LaTeX log files.
"""
import re
from collections import deque
class LogFileMessage:
"""
Helper class for storing log file messages.
Messages and attributes of the messages can be accessed and added using
the item notation.
"""
def __init__(self):
self.info = {}
self.context_lines = []
def __str__(self):
return '\n'.join(self.context_lines)
def __getitem__(self, item):
try:
return self.info[item]
except KeyError:
raise KeyError(f'Item {item} was not found.')
def __setitem__(self, key, value):
self.info[key] = value
class _LineIterWrapper:
"""
Wrapper around an iterable that allows peeking ahead to get context lines
without consuming the iterator.
"""
def __init__(self, iterable, ctx_lines):
self.iterable = iter(iterable)
self.cache = deque()
self.ctx_lines = ctx_lines
self.current = None
def __next__(self):
if self.cache:
self.current = current = self.cache.popleft()
else:
self.current = current = next(self.iterable)
return current
def __iter__(self):
return self
def get_context(self):
rv = [self.current] if self.current else []
for _ in range(self.ctx_lines + 1 - len(rv)):
try:
next_val = next(self.iterable)
self.cache.append(next_val)
rv.append(next_val)
except StopIteration:
break
return rv
class LatexLogParser:
"""
Parser for LaTeX Log files.
An LatexLogParser object can parse the log file or output of and generate
lists of errors, warnings, and bad boxes described in the log. Each error.
warning, or bad box is stored as a LogFileMessage in the corresponding
list.
"""
error = re.compile(
r"^(?:! ((?:La|pdf)TeX|Package|Class)(?: (\w+))? [eE]rror(?: \(([\\]?\w+)\))?: (.*)|! (.*))"
)
warning = re.compile(
r"^((?:La|pdf)TeX|Package|Class)(?: (\w+))? [wW]arning(?: \(([\\]?\w+)\))?: (.*)"
)
info = re.compile(
r"^((?:La|pdf)TeX|Package|Class)(?: (\w+))? [iI]nfo(?: \(([\\]?\w+)\))?: (.*)"
)
badbox = re.compile(
r"^(Over|Under)full "
r"\\([hv])box "
r"\((?:badness (\d+)|(\d+(?:\.\d+)?pt) too \w+)\) (?:"
r"(?:(?:in paragraph|in alignment|detected) "
r"(?:at lines (\d+)--(\d+)|at line (\d+)))"
r"|(?:has occurred while [\\]output is active [\[](\d+)?[\]]))"
)
missing_ref = re.compile(
r"^LaTeX Warning: (Citation|Reference) `([^']+)' on page (\d+) undefined on input line (\d+)\."
)
def __init__(self, context_lines=2):
self.warnings = []
self.errors = []
self.badboxes = []
self.missing_refs = []
self.context_lines = context_lines
def __str__(self):
return (f"Errors: {len(self.errors)}, "
f"Warnings: {len(self.warnings)}, "
f"Badboxes: {len(self.badboxes)}")
def process(self, lines):
"""
Process the lines of a logfile to produce a report.
Steps through each non-empty line and passes it to the process_line
function.
:param lines: Iterable over lines of log.
"""
lines_iterable = _LineIterWrapper(lines, self.context_lines)
# cache the line processor for speed
process_line = self.process_line
for i, line in enumerate(lines_iterable):
if not line:
continue
err = process_line(line)
if err is not None:
err.context_lines = lines_iterable.get_context()
def process_line(self, line):
"""
Process a line in the log file and delegate to correct handler.
Tests in turn matches to the badbox regex, warning regex, and
then error regex. Once a match is found, the corresponding
process function is called its result returned.
:param line: Line to process
:returns: LogFileMessage object or None
"""
# Missings refs are very common, try those first
match = self.missing_ref.match(line)
if match is not None:
return self.process_missing_ref(match)
# Badboxes are next most common, so match those first
match = self.badbox.match(line)
if match is not None:
return self.process_badbox(match)
# Now try warnings
match = self.warning.match(line)
if match is not None:
return self.process_warning(match)
# Now try errors
match = self.error.match(line)
if match is not None:
return self.process_error(match)
return None
def process_badbox(self, match):
"""
Process a badbox regex match and return the log message object.
:param match: regex match object to process
:return: LogFileMessage object
"""
# Regex match groups
# 0 - Whole match (line)
# 1 - Type (Over|Under)
# 2 - Direction ([hv])
# 3 - Underfull box badness (badness (\d+))
# 4 - Overfull box over size (\d+(\.\d+)?pt too \w+)
# 5 - Multi-line start line (at lines (\d+)--)
# 6 - Multi-line end line (--(d+))
# 7 - Single line (at line (\d+))
message = LogFileMessage()
message['type'] = match.group(1)
message['direction'] = match.group(2)
# direction is either h or v
message['by'] = match.group(3) or match.group(4)
# single or multi-line
if match.group(7) is not None:
message['lines'] = (match.group(7), match.group(7))
else:
message['lines'] = (match.group(5), match.group(6))
self.badboxes.append(message)
return message
def process_warning(self, match):
"""
Process a warning regex match and return the log message object.
:param match: regex match object to process
:return: LogFileMessage object
"""
# Regex match groups
# 0 - Whole match (line)
# 1 - Type ((?:La|pdf)TeX|Package|Class)
# 2 - Package or Class name (\w*)
# 3 - extra
# 4 - Warning message (.*)
message = LogFileMessage()
message['type'] = type_ = match.group(1)
if type_ == 'Package':
# package name should be group 2
message['package'] = match.group(2)
elif type_ == 'Class':
# class should be group 2
message['class'] = match.group(2)
elif match.group(2) is not None:
# In any other case we want to record the component responsible for
# the warning, if one is present.
message['component'] = match.group(2)
if match.group(3) is not None:
message['extra'] = match.group(3)
message['message'] = match.group(4)
self.warnings.append(message)
return message
def process_error(self, match):
"""
Process a warning regex match and return the log message object.
:param match: regex match object to process
:return: LogFileMessage object
"""
# Regex match groups
# 0 - Whole match (line)
# 1 - Type (LaTeX|Package|Class)
# 2 - Package or Class (\w+)
# 3 - extra (\(([\\]\w+)\))
# 4 - Error message for typed error (.*)
# 5 - TeX error message (.*)
message = LogFileMessage()
if match.group(1) is not None:
message['type'] = type_ = match.group(1)
if type_ == 'Package':
# Package name should be group 2
message['package'] = match.group(2)
elif type_ == 'Class':
# Class name should be group 2
message['class'] = match.group(2)
elif match.group(2) is not None:
message['component'] = match.group(2)
if match.group(3) is not None:
message['extra'] = match.group(3)
message['message'] = match.group(4)
else:
message['message'] = match.group(5)
self.errors.append(message)
return message
def process_missing_ref(self, match):
"""
Process a missing reference regex match and return log message object.
:param match: regex match object to process
:return: LogFileMessage object.
"""
message = LogFileMessage()
message["type"] = f"Missing {match.group(1)}"
message["key"] = match.group(2)
message["page"] = match.group(3)
message["line"] = match.group(4)
self.missing_refs.append(message)
return message