From df78de97e22778027f858318f027301555e8fd79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Wed, 2 Feb 2022 18:29:05 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20comm:=20improve=20regex=20?= =?UTF-8?q?&=20use=20named=20groups=20for=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/util/comm.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index a7614cfa4..2d779e7da 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -112,15 +112,16 @@ Groups will be as follows: """ regex_temp = re.compile( - r"(?PB|C|T(?P\d*)|([\w]+)):\s*(?P%s)(\s*\/?\s*(?P%s))?" + r"(?PB|C|T(?P\d*)|([\w]+)):\s*(?P%s)(\s*\/?\s*(?P%s))?" % (regex_float_pattern, regex_float_pattern) ) """Regex matching temperature entries in line. Groups will be as follows: - * ``tool``: whole tool designator, incl. optional ``toolnum`` (str) - * ``toolnum``: tool number, if provided (int) + * ``sensor``: whole sensor designator, incl. optional ``toolnum``, e.g. "T1", "B", + "C" or anything custom (str) + * ``toolnum``: tool number, if provided, only for T0, T1, etc (int) * ``actual``: actual temperature (float) * ``target``: target temperature, if provided (float) """ @@ -3731,7 +3732,7 @@ class MachineCom: except serial.SerialException: self._logger.info( - "Failed to connect: Port {} is busy or does not exist".format(p) + f"Failed to connect: Port {p} is busy or does not exist" ) return None @@ -6197,27 +6198,27 @@ def parse_temperature_line(line, current): """ result = {} - maxToolNum = 0 + max_tool_num = 0 for match in re.finditer(regex_temp, line): values = match.groupdict() - tool = values["tool"] + sensor = values["sensor"] toolnum = values.get("toolnum", None) - toolNumber = int(toolnum) if toolnum is not None and len(toolnum) else None - if toolNumber and toolNumber > maxToolNum: - maxToolNum = toolNumber + tool_number = int(toolnum) if toolnum is not None and len(toolnum) else None + if tool_number and tool_number > max_tool_num: + max_tool_num = tool_number try: - actual = float(match.group(4)) + actual = float(values["actual"]) target = None - if match.group(4) and match.group(6): - target = float(match.group(6)) + if values["target"]: + target = float(values["target"]) - result[tool] = (actual, target) + result[sensor] = (actual, target) except ValueError: # catch conversion issues, we'll rather just not get the temperature update instead of killing the connection pass - return max(maxToolNum, current), canonicalize_temperatures(result, current) + return max(max_tool_num, current), canonicalize_temperatures(result, current) def parse_firmware_line(line):