From 6f224bd825f4c9bab1d3eb109540420ca564532f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 11 Apr 2018 11:12:56 +0200 Subject: [PATCH] Workaround for typo & whitespace in Malyan M115 responses NAME. instead of NAME:, whitespace between key and value component. --- src/octoprint/util/comm.py | 18 ++++++++++++++---- tests/util/test_comm_helpers.py | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index d4cbc4799..7737b7dc8 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -192,7 +192,7 @@ Groups will be as follows: * ``value``: reported position value """ -regex_firmware_splitter = re.compile("\s*([A-Z0-9_]+):") +regex_firmware_splitter = re.compile("\s*([A-Z0-9_]+):\s*") """Regex to use for splitting M115 responses.""" regex_resend_linenumber = re.compile("(N|N:)?(?P%s)" % regex_int_pattern) @@ -1662,7 +1662,7 @@ class MachineCom(object): pass ##~~ firmware name & version - elif "NAME:" in line: + elif "NAME:" in line or line.startswith("NAME."): # looks like a response to M115 data = parse_firmware_line(line) firmware_name = data.get("FIRMWARE_NAME") @@ -1672,7 +1672,11 @@ class MachineCom(object): # report its firmware name properly in response to M115. Wonderful - why stick to established # protocol when you can do your own thing, right? # - # Example: NAME: Malyan VER: 2.9 MODEL: M200 HW: HA02 + # Examples: + # + # NAME: Malyan VER: 2.9 MODEL: M200 HW: HA02 + # NAME. Malyan VER: 3.8 MODEL: M100 HW: HB02 + # NAME. Malyan VER: 3.7 MODEL: M300 HW: HG01 # # We do a bit of manual fiddling around here to circumvent that issue and get ourselves a # reliable firmware name (NAME + VER) out of the Malyan M115 response. @@ -4013,10 +4017,16 @@ def parse_firmware_line(line): dict: a dictionary with the parsed data """ + if line.startswith("NAME."): + # Good job Malyan. Why use a : when you can also just use a ., right? Let's revert that. + line = list(line) + line[4] = ":" + line = "".join(line) + result = dict() 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): - result[key] = value + result[key] = value.strip() return result def parse_capability_line(line): diff --git a/tests/util/test_comm_helpers.py b/tests/util/test_comm_helpers.py index 10ab72122..119ff406d 100644 --- a/tests/util/test_comm_helpers.py +++ b/tests/util/test_comm_helpers.py @@ -259,7 +259,20 @@ class TestCommHelpers(unittest.TestCase): @data( ("KEY1:Value 1 FIRMWARE_NAME:Some Firmware With Spaces KEY2:Value 2", - dict(KEY1="Value 1", KEY2="Value 2", FIRMWARE_NAME="Some Firmware With Spaces")) + dict(KEY1="Value 1", KEY2="Value 2", FIRMWARE_NAME="Some Firmware With Spaces")), + ("NAME: Malyan VER: 2.9 MODEL: M200 HW: HA02", + dict(NAME="Malyan", VER="2.9", MODEL="M200", HW="HA02")), + ("NAME. Malyan VER: 3.8 MODEL: M100 HW: HB02", + dict(NAME="Malyan", VER="3.8", MODEL="M100", HW="HB02")), + ("NAME. Malyan VER: 3.7 MODEL: M300 HW: HG01", + dict(NAME="Malyan", VER="3.7", MODEL="M300", HW="HG01")), + ("FIRMWARE_NAME:Marlin 1.1.0 From Archive SOURCE_CODE_URL:http:// ... PROTOCOL_VERSION:1.0 MACHINE_TYPE:www.cxsw3d.com EXTRUDER_COUNT:1 UUID:00000000-0000-0000-0000-000000000000", + dict(FIRMWARE_NAME="Marlin 1.1.0 From Archive", + SOURCE_CODE_URL="http:// ...", + PROTOCOL_VERSION="1.0", + MACHINE_TYPE="www.cxsw3d.com", + EXTRUDER_COUNT="1", + UUID="00000000-0000-0000-0000-000000000000")) ) @unpack def test_parse_firmware_line(self, line, expected):