From 46d4a73955edebb417d2197fe6842b106ddd4b60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 6 Apr 2020 16:39:28 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20minor=20refactorings?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * validate custom identifiers for as_dict method * minor performance improvement --- src/octoprint/printer/standard.py | 17 +++++++++++------ src/octoprint/util/comm.py | 6 +++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/octoprint/printer/standard.py b/src/octoprint/printer/standard.py index c3c895ecf..10ccd329d 100644 --- a/src/octoprint/printer/standard.py +++ b/src/octoprint/printer/standard.py @@ -891,6 +891,8 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): def _addTemperatureData(self, tools=None, bed=None, chamber=None, custom=None): if tools is None: tools = dict() + if custom is None: + custom = dict() data = dict(time=int(time.time())) for tool in tools.keys(): @@ -899,10 +901,8 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): data["bed"] = self._dict(actual=bed[0], target=bed[1]) if chamber is not None and isinstance(chamber, tuple): data["chamber"] = self._dict(actual=chamber[0], target=chamber[1]) - if custom is None: - custom = dict() - for identifier in custom.keys(): - data[identifier] = self._dict(actual=custom[identifier][0], target=custom[identifier][1]) + for identifier, values in custom.items(): + data[identifier] = self._dict(actual=values[0], target=values[1]) self._temps.append(data) @@ -1060,8 +1060,13 @@ class Printer(PrinterInterface, comm.MachineComPrintCallback): """ self._addLog(to_unicode(message, "utf-8", errors="replace")) - def on_comm_temperature_update(self, temp, bedTemp, chamberTemp, customTemp): - self._addTemperatureData(tools=copy.deepcopy(temp), bed=copy.deepcopy(bedTemp), chamber=copy.deepcopy(chamberTemp), custom=copy.deepcopy(customTemp)) + def on_comm_temperature_update(self, tools, bed, chamber, custom=None): + if custom is None: + custom = dict() + self._addTemperatureData(tools=copy.deepcopy(tools), + bed=copy.deepcopy(bed), + chamber=copy.deepcopy(chamber), + custom=copy.deepcopy(custom)) def on_comm_position_update(self, position, reason=None): payload = dict(reason=reason) diff --git a/src/octoprint/util/comm.py b/src/octoprint/util/comm.py index e53600660..8e65f6491 100644 --- a/src/octoprint/util/comm.py +++ b/src/octoprint/util/comm.py @@ -279,6 +279,8 @@ class PositionRecord(object): return dict((attr, getattr(self, attr)) for attr in attrs) class TemperatureRecord(object): + RESERVED_IDENTIFIER_REGEX = re.compile("[0-9]+|[bc]") + def __init__(self): self._tools = dict() self._bed = (None, None) @@ -302,6 +304,8 @@ class TemperatureRecord(object): self._chamber = self._to_new_tuple(current, actual, target) def set_custom(self, identifier, actual=None, target=None): + if self.RESERVED_IDENTIFIER_REGEX.match(identifier): + raise ValueError("{} is a reserved identifier".format(identifier)) current = self._custom.get(identifier, (None, None)) self._custom[identifier] = self._to_new_tuple(current, actual, target) @@ -340,7 +344,7 @@ class TemperatureRecord(object): custom = self.custom for identifier, data in custom.items(): result[identifier] = dict(actual=data[0], - target=data[1]) + target=data[1]) return result