More solid parsing of line number from resends

Matches:

  * Resend:23
  * Resend:N23
  * Resend:N:23
  * rs 23
  * rs N23
  * rs N:23
  * rs N23 Expected checksum 109

Based on issue reported via PR #300
This commit is contained in:
Gina Häußge 2017-03-09 17:07:29 +01:00
parent fa16037e9a
commit 559238ca3c
2 changed files with 37 additions and 7 deletions

View file

@ -127,6 +127,9 @@ Groups will be as follows:
regex_firmware_splitter = re.compile("\s*([A-Z0-9_]+):")
"""Regex to use for splitting M115 responses."""
regex_resend_linenumber = re.compile("(N|N:)?(?P<n>%s)" % regex_int_pattern)
"""Regex to use for request line numbers in resend requests"""
def serialList():
baselist=[]
if os.name=="nt":
@ -1862,13 +1865,7 @@ class MachineCom(object):
def _handleResendRequest(self, line):
try:
lineToResend = None
try:
lineToResend = int(line.replace("N:", " ").replace("N", " ").replace(":", " ").split()[-1])
except:
if "rs" in line:
lineToResend = int(line.split()[1])
lineToResend = parse_resend_line(line)
if lineToResend is None:
return False
@ -3014,6 +3011,24 @@ def parse_firmware_line(line):
result[key] = value
return result
def parse_resend_line(line):
"""
Parses the provided resend line and returns requested line number.
Args:
line (str): the line to parse
Returns:
int or None: the extracted line number to resend, or None if no number could be extracted
"""
match = regex_resend_linenumber.search(line)
if match is not None:
return int(match.group("n"))
return None
def gcode_command_for_cmd(cmd):
"""
Tries to parse the provided ``cmd`` and extract the GCODE command identifier from it (e.g. "G0" for "G0 X10.0").

View file

@ -246,3 +246,18 @@ class TestCommHelpers(unittest.TestCase):
from octoprint.util.comm import parse_firmware_line
result = parse_firmware_line(line)
self.assertDictEqual(expected, result)
@data(
("Resend:23", 23),
("Resend: N23", 23),
("Resend: N:23", 23),
("rs 23", 23),
("rs N23", 23),
("rs N:23", 23),
("rs N23 expected checksum 109", 23) # teacup, see #300
)
@unpack
def test_parse_resend_line(self, line, expected):
from octoprint.util.comm import parse_resend_line
result = parse_resend_line(line)
self.assertEqual(expected, result)