From dc9cd3dbd1d81796db17ae170335ed6ccc582505 Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Thu, 5 May 2022 13:49:23 +0100 Subject: [PATCH 1/5] Handle pandoc logging returning two unknown log levels --- pypandoc/__init__.py | 42 ++++++++++++++++++++++++------------------ tests.py | 8 +++++--- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/pypandoc/__init__.py b/pypandoc/__init__.py index aa6a607..26f6339 100644 --- a/pypandoc/__init__.py +++ b/pypandoc/__init__.py @@ -392,14 +392,24 @@ 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. # 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 the level 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} + def get_python_level(pandoc_level): + + level_map = {"CRITICAL": 50, + "ERROR": 40, + "WARNING": 30, + "INFO": 20, + "DEBUG": 10, + "NOTSET": 0} + + 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) @@ -408,29 +418,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): diff --git a/tests.py b/tests.py index 6454159..704f049 100755 --- a/tests.py +++ b/tests.py @@ -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) From 8f831a4c5c9ef00b4d0d189b3fbe44a80334bcdc Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Thu, 5 May 2022 14:24:39 +0100 Subject: [PATCH 2/5] Improve the function description --- pypandoc/__init__.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pypandoc/__init__.py b/pypandoc/__init__.py index 26f6339..714f305 100644 --- a/pypandoc/__init__.py +++ b/pypandoc/__init__.py @@ -390,10 +390,11 @@ 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 or the level does not conform to the - # pandoc standard, 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. def get_python_level(pandoc_level): From f271d73a134fe6bcc43478abd40da2c9b952030f Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Thu, 5 May 2022 14:27:24 +0100 Subject: [PATCH 3/5] Replace redundant yield with return --- pypandoc/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pypandoc/__init__.py b/pypandoc/__init__.py index 714f305..f3977bc 100644 --- a/pypandoc/__init__.py +++ b/pypandoc/__init__.py @@ -437,7 +437,7 @@ def get_python_level(pandoc_level): log_msgs.append(msg) - yield get_python_level(pandoc_level), "\n".join(log_msgs) + return get_python_level(pandoc_level), "\n".join(log_msgs) def _get_base_format(format): From 0fd272b8ab05d424994d5325a7ea2ef8f830bb1a Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Thu, 5 May 2022 14:36:33 +0100 Subject: [PATCH 4/5] Revert "Replace redundant yield with return" This reverts commit f271d73a134fe6bcc43478abd40da2c9b952030f. --- pypandoc/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pypandoc/__init__.py b/pypandoc/__init__.py index f3977bc..714f305 100644 --- a/pypandoc/__init__.py +++ b/pypandoc/__init__.py @@ -437,7 +437,7 @@ def get_python_level(pandoc_level): log_msgs.append(msg) - return get_python_level(pandoc_level), "\n".join(log_msgs) + yield get_python_level(pandoc_level), "\n".join(log_msgs) def _get_base_format(format): From ba5d928074788425d7047f2c8ef7d13ec6e12e44 Mon Sep 17 00:00:00 2001 From: Mathew Topper Date: Thu, 5 May 2022 15:17:42 +0100 Subject: [PATCH 5/5] Make level_map represent pandoc logging levels --- pypandoc/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pypandoc/__init__.py b/pypandoc/__init__.py index 714f305..748fe6f 100644 --- a/pypandoc/__init__.py +++ b/pypandoc/__init__.py @@ -396,14 +396,14 @@ def _classify_pandoc_logging(raw, default_level="WARNING"): # that does not conform to the pandoc standard, use the default_level # value instead. + # 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 = {"CRITICAL": 50, - "ERROR": 40, + level_map = {"ERROR": 40, "WARNING": 30, "INFO": 20, - "DEBUG": 10, - "NOTSET": 0} + "DEBUG": 10} if pandoc_level not in level_map: level = level_map[default_level]