Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions pypandoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,16 +390,27 @@ def _convert_input(source, format, input_type, to, extra_args=(),


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

level_map = {"CRITICAL": 50,
"ERROR": 40,
"WARNING": 30,
"INFO": 20,
"DEBUG": 10,
"NOTSET": 0}
# Available pandoc logging levels adapted from:
# https://github.com/jgm/pandoc/blob/5e1249481b2e3fc27e845245a0c96c3687a23c3d/src/Text/Pandoc/Logging.hs#L44
def get_python_level(pandoc_level):

level_map = {"ERROR": 40,
"WARNING": 30,
"INFO": 20,
"DEBUG": 10}

if pandoc_level not in level_map:
level = level_map[default_level]
else:
level = level_map[pandoc_level]

return level

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

# Use the default if the first message doesn't have a level
if search is None:
level = default_level
pandoc_level = default_level
else:
level = first[search.start(1):search.end(1)]

log_msgs = [first.replace('[{}] '.format(level), '')]

if level not in level_map:
level = default_level
pandoc_level = first[search.start(1):search.end(1)]

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

for msg in msgs:

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

if search is not None:
yield level_map[level], "\n".join(log_msgs)
level = msg[search.start(1):search.end(1)]
log_msgs = [msg.replace('[{}] '.format(level), '')]
yield get_python_level(pandoc_level), "\n".join(log_msgs)
pandoc_level = msg[search.start(1):search.end(1)]
log_msgs = [msg.replace('[{}] '.format(pandoc_level), '')]
continue

log_msgs.append(msg)

yield level_map[level], "\n".join(log_msgs)
yield get_python_level(pandoc_level), "\n".join(log_msgs)


def _get_base_format(format):
Expand Down
8 changes: 5 additions & 3 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,12 @@ def test_classify_pandoc_logging_default(self):
def test_classify_pandoc_logging_invalid_level(self):

test = ("[WARN] This is some message on\ntwo lines\n"
"[ERROR] This is a second message.")
expected_levels = [30, 40]
"[ERR] This is a second message.\n"
"[ERROR] This is a third message.")
expected_levels = [30, 30, 40]
expected_msgs = ["This is some message on\ntwo lines",
"This is a second message."]
"This is a second message.",
"This is a third message."]

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