Skip to content

Commit b5868e4

Browse files
authored
Handle pandoc logging returning two unknown log levels (#272)
* Handle pandoc logging returning two unknown log levels * Improve the function description * Replace redundant yield with return * Revert "Replace redundant yield with return" This reverts commit f271d73. * Make level_map represent pandoc logging levels
1 parent 2254d0a commit b5868e4

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

pypandoc/__init__.py

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -390,16 +390,27 @@ def _convert_input(source, format, input_type, to, extra_args=(),
390390

391391

392392
def _classify_pandoc_logging(raw, default_level="WARNING"):
393-
# Process raw and yeild the contained logging levels and messages.
393+
# Process raw and yield the contained logging levels and messages.
394394
# Assumes that the messages are formatted like "[LEVEL] message". If the
395-
# first message does not have a level, use the default_level value instead.
395+
# first message does not have a level or any other message has a level
396+
# that does not conform to the pandoc standard, use the default_level
397+
# value instead.
396398

397-
level_map = {"CRITICAL": 50,
398-
"ERROR": 40,
399-
"WARNING": 30,
400-
"INFO": 20,
401-
"DEBUG": 10,
402-
"NOTSET": 0}
399+
# Available pandoc logging levels adapted from:
400+
# https://github.com/jgm/pandoc/blob/5e1249481b2e3fc27e845245a0c96c3687a23c3d/src/Text/Pandoc/Logging.hs#L44
401+
def get_python_level(pandoc_level):
402+
403+
level_map = {"ERROR": 40,
404+
"WARNING": 30,
405+
"INFO": 20,
406+
"DEBUG": 10}
407+
408+
if pandoc_level not in level_map:
409+
level = level_map[default_level]
410+
else:
411+
level = level_map[pandoc_level]
412+
413+
return level
403414

404415
msgs = raw.split("\n")
405416
first = msgs.pop(0)
@@ -408,29 +419,25 @@ def _classify_pandoc_logging(raw, default_level="WARNING"):
408419

409420
# Use the default if the first message doesn't have a level
410421
if search is None:
411-
level = default_level
422+
pandoc_level = default_level
412423
else:
413-
level = first[search.start(1):search.end(1)]
414-
415-
log_msgs = [first.replace('[{}] '.format(level), '')]
416-
417-
if level not in level_map:
418-
level = default_level
424+
pandoc_level = first[search.start(1):search.end(1)]
419425

426+
log_msgs = [first.replace('[{}] '.format(pandoc_level), '')]
420427

421428
for msg in msgs:
422429

423430
search = re.search(r"\[(.*?)\]", msg)
424431

425432
if search is not None:
426-
yield level_map[level], "\n".join(log_msgs)
427-
level = msg[search.start(1):search.end(1)]
428-
log_msgs = [msg.replace('[{}] '.format(level), '')]
433+
yield get_python_level(pandoc_level), "\n".join(log_msgs)
434+
pandoc_level = msg[search.start(1):search.end(1)]
435+
log_msgs = [msg.replace('[{}] '.format(pandoc_level), '')]
429436
continue
430437

431438
log_msgs.append(msg)
432439

433-
yield level_map[level], "\n".join(log_msgs)
440+
yield get_python_level(pandoc_level), "\n".join(log_msgs)
434441

435442

436443
def _get_base_format(format):

tests.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,12 @@ def test_classify_pandoc_logging_default(self):
337337
def test_classify_pandoc_logging_invalid_level(self):
338338

339339
test = ("[WARN] This is some message on\ntwo lines\n"
340-
"[ERROR] This is a second message.")
341-
expected_levels = [30, 40]
340+
"[ERR] This is a second message.\n"
341+
"[ERROR] This is a third message.")
342+
expected_levels = [30, 30, 40]
342343
expected_msgs = ["This is some message on\ntwo lines",
343-
"This is a second message."]
344+
"This is a second message.",
345+
"This is a third message."]
344346

345347
for i, (l, m) in enumerate(pypandoc._classify_pandoc_logging(test)):
346348
self.assertEqual(expected_levels[i], l)

0 commit comments

Comments
 (0)