From d091d2de28744de89167044a2857bfc35acf51b5 Mon Sep 17 00:00:00 2001 From: cp2004 <31997505+cp2004@users.noreply.github.com> Date: Thu, 31 Dec 2020 12:01:51 +0000 Subject: [PATCH 1/5] :bug: Improve firmware name splitting Marlin includes the build time in it's firmware name, until now the expression split it on the colon, and failed to do it properly. Example format that now works: ``` FIRMWARE_NAME:Marlin 2.0.7.2 (Nov 27 2020 14:30:11) ``` --- src/octoprint/util/comm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 00a2e6f13..2045c7e94 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -182,7 +182,7 @@ Groups will be as follows: * ``value``: reported position value """ -regex_firmware_splitter = re.compile(r"\s*([A-Z0-9_]+):\s*") +regex_firmware_splitter = re.compile(r"\s*([A-Z_]+):\s*") """Regex to use for splitting M115 responses.""" regex_resend_linenumber = re.compile(r"(N|N:)?(?P%s)" % regex_int_pattern) From d0f6bf3e0634020130e6efa8b44a086bed6ecead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 22 Feb 2021 18:53:45 +0100 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=90=9B=20Allow=20numbers,=20but=20not?= =?UTF-8?q?=20at=20the=20start?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/util/comm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 2045c7e94..36921a7b6 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -182,7 +182,7 @@ Groups will be as follows: * ``value``: reported position value """ -regex_firmware_splitter = re.compile(r"\s*([A-Z_]+):\s*") +regex_firmware_splitter = re.compile(r"\s*([A-Z][A-Z0-9_]*):\s*") """Regex to use for splitting M115 responses.""" regex_resend_linenumber = re.compile(r"(N|N:)?(?P%s)" % regex_int_pattern) From e43f4ddb50352cbb65ad726b26f3add69a121950 Mon Sep 17 00:00:00 2001 From: cp2004 <31997505+cp2004@users.noreply.github.com> Date: Mon, 22 Feb 2021 18:34:15 +0000 Subject: [PATCH 3/5] :green_heart: Add more tests for firmware info splitting Part of #3916 --- tests/util/test_comm_helpers.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index 32926470f..4a0fb4e0e 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -376,6 +376,25 @@ class TestCommHelpers(unittest.TestCase): "UUID": "00000000-0000-0000-0000-000000000000", }, ), + # Test firmware name with time created + ( + "FIRMWARE_NAME:Marlin 2.0.7.2 (Nov 27 2020 14:30:11) SOURCE_CODE_URL:https://github.com/MarlinFirmware/Marlin PROTOCOL_VERSION:1.0 MACHINE_TYPE:Ender 5 Pro EXTRUDER_COUNT:1 UUID:cede2a2f-41a2-4748-9b12-c55c62f367ff", + { + "FIRMWARE_NAME": "Marlin 2.0.7.2 (Nov 27 2020 14:30:11)", + "SOURCE_CODE_URL": "https://github.com/MarlinFirmware/Marlin", + "PROTOCOL_VERSION": "1.0", + "MACHINE_TYPE": "Ender 5 Pro", + "EXTRUDER_COUNT": "1", + "UUID": "cede2a2f-41a2-4748-9b12-c55c62f367ff", + } + ), + # Test that keys beginning with _ or number are ignored + ( + "KEY1:VALUE1 _KEY2:INVALID 123:INVALID", + { + "KEY1": "VALUE1 _", "KEY2": "INVALID 123:INVALID" + } + ) ) @unpack def test_parse_firmware_line(self, line, expected): From bd3f7f8897a6ab6b20be0f137f177a099d8b3a59 Mon Sep 17 00:00:00 2001 From: cp2004 <31997505+cp2004@users.noreply.github.com> Date: Mon, 22 Feb 2021 18:46:24 +0000 Subject: [PATCH 4/5] :art: Make black happy again --- tests/util/test_comm_helpers.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index 4a0fb4e0e..81b01f07b 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -386,15 +386,13 @@ class TestCommHelpers(unittest.TestCase): "MACHINE_TYPE": "Ender 5 Pro", "EXTRUDER_COUNT": "1", "UUID": "cede2a2f-41a2-4748-9b12-c55c62f367ff", - } + }, ), # Test that keys beginning with _ or number are ignored ( "KEY1:VALUE1 _KEY2:INVALID 123:INVALID", - { - "KEY1": "VALUE1 _", "KEY2": "INVALID 123:INVALID" - } - ) + {"KEY1": "VALUE1 _", "KEY2": "INVALID 123:INVALID"}, + ), ) @unpack def test_parse_firmware_line(self, line, expected): From fec2f9b83e3e17341f6cbbc2dd1afc96ade519c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Tue, 23 Feb 2021 11:32:36 +0100 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=90=9B=20Further=20firmware=20line=20?= =?UTF-8?q?parser=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Something like _KEY should never get recognized as a key. --- src/octoprint/util/comm.py | 8 ++++++-- tests/util/test_comm_helpers.py | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index 36921a7b6..e29f1b971 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -182,7 +182,7 @@ Groups will be as follows: * ``value``: reported position value """ -regex_firmware_splitter = re.compile(r"\s*([A-Z][A-Z0-9_]*):\s*") +regex_firmware_splitter = re.compile(r"(^|\s+)([A-Z][A-Z0-9_]*):") """Regex to use for splitting M115 responses.""" regex_resend_linenumber = re.compile(r"(N|N:)?(?P%s)" % regex_int_pattern) @@ -6091,6 +6091,10 @@ def parse_firmware_line(line): The result will be a dictionary mapping from the contained keys to the contained values. + Valid keys must only contain A-Z, 0-9 and _ and must start with a letter. See the + unit tests for valid and invalid examples. There sadly is no existing specification + of the key format, but this is the format extracted from real life logs. + Arguments: line (str): the line to parse @@ -6108,7 +6112,7 @@ def parse_firmware_line(line): split_line = regex_firmware_splitter.split(line.strip())[ 1: ] # first entry is empty start of trimmed string - for key, value in chunks(split_line, 2): + for _, key, value in chunks(split_line, 3): result[key] = value.strip() return result diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index 81b01f07b..1d1969b65 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -390,8 +390,8 @@ class TestCommHelpers(unittest.TestCase): ), # Test that keys beginning with _ or number are ignored ( - "KEY1:VALUE1 _KEY2:INVALID 123:INVALID", - {"KEY1": "VALUE1 _", "KEY2": "INVALID 123:INVALID"}, + "KEY1:VALUE1 _KEY2:INVALID 123:INVALID 1KEY:INVALID KEY2:VALUE2", + {"KEY1": "VALUE1 _KEY2:INVALID 123:INVALID 1KEY:INVALID", "KEY2": "VALUE2"}, ), ) @unpack