From 4c76225c41a3453d2ece479bfc9df96b11efb56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 15 Feb 2019 18:54:07 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20WIP:=20Fixing=20tests,=20part=20?= =?UTF-8?q?2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comm/protocol/reprap/commands/gcode.py | 29 ++++----- .../comm/protocol/reprap/util/__init__.py | 60 +++++++++---------- src/octoprint/util/__init__.py | 8 +-- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/src/octoprint/comm/protocol/reprap/commands/gcode.py b/src/octoprint/comm/protocol/reprap/commands/gcode.py index 8e8355e4c..22222a05f 100644 --- a/src/octoprint/comm/protocol/reprap/commands/gcode.py +++ b/src/octoprint/comm/protocol/reprap/commands/gcode.py @@ -37,13 +37,14 @@ class GcodeCommand(Command): match = GcodeCommand.command_regex.match(line) if match is None: - raise ValueError("{!r} is not a Gcode line".format(line)) + # not a gcode command + return Command.from_line(line, **kwargs) - if match.group("GM"): - code = match.group("GM") + if match.group(b"GM"): + code = match.group(b"GM") - if match.group("subcode"): - args["subcode"] = int(match.group("subcode")) + if match.group(b"subcode"): + args["subcode"] = int(match.group(b"subcode")) if match.group(0) != line: rest = line[len(match.group(0)):] @@ -53,9 +54,9 @@ class GcodeCommand(Command): if not matched_param: break - key = matched_param.group("key").lower() - if matched_param.group("value"): - value = matched_param.group("value") + key = matched_param.group(b"key").lower() + if matched_param.group(b"value"): + value = matched_param.group(b"value") if "." in value: value = float(value) else: @@ -69,13 +70,13 @@ class GcodeCommand(Command): if rest: args["param"] = rest - elif match.group("T"): + elif match.group(b"T"): code = "T" - args["tool"] = int(match.group("tool")) + args["tool"] = int(match.group(b"tool")) - elif match.group("F"): + elif match.group(b"F"): code = "F" - args["feedrate"] = int(match.group("feedrate")) + args["feedrate"] = int(match.group(b"feedrate")) return GcodeCommand(code, **args) @@ -125,7 +126,7 @@ class GcodeCommand(Command): def _to_repr(self): args = [k + "=" + repr(getattr(self, k)) for k in self.possible_params if getattr(self, k, None) is not None] - return "GcodeCommand({!r},{},type={!r},tags={!r})".format(self.code, - ",".join(args), + return "GcodeCommand({!r}{},type={!r},tags={!r})".format(self.code, + "," + ",".join(args) if args else "", self.type, self.tags) diff --git a/src/octoprint/comm/protocol/reprap/util/__init__.py b/src/octoprint/comm/protocol/reprap/util/__init__.py index 2a79c259a..c8a43ca22 100644 --- a/src/octoprint/comm/protocol/reprap/util/__init__.py +++ b/src/octoprint/comm/protocol/reprap/util/__init__.py @@ -4,7 +4,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' __copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms of the AGPLv3 License" -from octoprint.comm.protocol.reprap.commands import Command +from octoprint.comm.protocol.reprap.commands import Command, to_command from octoprint.util import PrependableQueue, TypeAlreadyInQueue, CountedEvent # noinspection PyCompatibility @@ -80,32 +80,31 @@ def normalize_command_handler_result(command, handler_results, tags_to_add=None) be empty in which case the command is to be suppressed. Examples: - >>> normalize_command_handler_result("M105", None, "M105", None, None, None) - [(u'M105', None, u'M105', None, None)] - >>> normalize_command_handler_result("M105", None, "M105", None, None, "M110") - [(u'M110', None, u'M110', None, None)] - >>> normalize_command_handler_result("M105", None, "M105", None, None, ["M110"]) - [(u'M110', None, u'M110', None, None)] - >>> normalize_command_handler_result("M105", None, "M105", None, None, ["M110", "M117 Foobar"]) - [(u'M110', None, u'M110', None, None), (u'M117 Foobar', None, u'M117', None, None)] - >>> normalize_command_handler_result("M105", None, "M105", None, None, [("M110",), "M117 Foobar"]) - [(u'M110', None, u'M110', None, None), (u'M117 Foobar', None, u'M117', None, None)] - >>> normalize_command_handler_result("M105", None, "M105", None, None, [("M110", "lineno_reset"), "M117 Foobar"]) - [(u'M110', u'lineno_reset', u'M110', None, None), (u'M117 Foobar', None, u'M117', None, None)] - >>> normalize_command_handler_result("M105", None, "M105", None, None, []) + >>> from octoprint.comm.protocol.reprap.commands.gcode import GcodeCommand + >>> normalize_command_handler_result(GcodeCommand("M105"), None) # doctest: +ALLOW_UNICODE + [GcodeCommand('M105',type=None,tags=set([]))] + >>> normalize_command_handler_result(GcodeCommand("M105"), GcodeCommand("M110")) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',type=None,tags=set([]))] + >>> normalize_command_handler_result(GcodeCommand("M105"), ["M110"]) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',original='M110',type=None,tags=set([]))] + >>> normalize_command_handler_result(GcodeCommand("M105"), ["M110", "M117 Foobar"]) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',original='M110',type=None,tags=set([])), GcodeCommand('M117',param='Foobar',original='M117 Foobar',type=None,tags=set([]))] + >>> normalize_command_handler_result(GcodeCommand("M105"), [("M110",), "M117 Foobar"]) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',original='M110',type=None,tags=set([])), GcodeCommand('M117',param='Foobar',original='M117 Foobar',type=None,tags=set([]))] + >>> normalize_command_handler_result(GcodeCommand("M105"), [("M110", "lineno_reset"), "M117 Foobar"]) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',original='M110',type='lineno_reset',tags=set([])), GcodeCommand('M117',param='Foobar',original='M117 Foobar',type=None,tags=set([]))] + >>> normalize_command_handler_result(GcodeCommand("M105"), []) # doctest: +ALLOW_UNICODE [] - >>> normalize_command_handler_result("M105", None, "M105", None, None, ["M110", None]) - [(u'M110', None, u'M110', None, None)] - >>> normalize_command_handler_result("M105", None, "M105", None, None, [("M110",), (None, "ignored")]) - [(u'M110', None, u'M110', None, None)] - >>> normalize_command_handler_result("M105", None, "M105", None, None, [("M110",), ("M117 Foobar", "display_message"), ("tuple", "of", "unexpected", "length"), ("M110", "lineno_reset")]) - [(u'M110', None, u'M110', None, None), (u'M117 Foobar', u'display_message', u'M117', None, None), (u'M110', u'lineno_reset', u'M110', None, None)] - >>> normalize_command_handler_result("M105", None, "M105", None, {"tag1", "tag2"}, ["M110", "M117 Foobar"]) - [(u'M110', None, u'M110', None, set([u'tag1', u'tag2'])), (u'M117 Foobar', None, u'M117', None, set([u'tag1', u'tag2']))] - >>> normalize_command_handler_result("M105", None, "M105", None, {"tag1", "tag2"}, ["M110", "M105", "M117 Foobar"], tags_to_add={"tag3"}) - [(u'M110', None, u'M110', None, set([u'tag1', u'tag2', u'tag3'])), (u'M105', None, u'M105', None, set([u'tag1', u'tag2'])), (u'M117 Foobar', None, u'M117', None, set([u'tag1', u'tag2', u'tag3']))] - >>> normalize_command_handler_result("M105", None, "M105", None, {"tag1", "tag2"}, ["M110", ("M105", "temperature_poll"), "M117 Foobar"], tags_to_add={"tag3"}) - [(u'M110', None, u'M110', None, set([u'tag1', u'tag2', u'tag3'])), (u'M105', u'temperature_poll', u'M105', None, set([u'tag1', u'tag2', u'tag3'])), (u'M117 Foobar', None, u'M117', None, set([u'tag1', u'tag2', u'tag3']))] + >>> normalize_command_handler_result(GcodeCommand("M105"), ["M110", None]) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',original='M110',type=None,tags=set([]))] + >>> normalize_command_handler_result(GcodeCommand("M105"), [("M110",), (None, "ignored")]) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',original='M110',type=None,tags=set([]))] + >>> normalize_command_handler_result(GcodeCommand("M105"), [("M110",), ("M117 Foobar", "display_message"), ("tuple", "of", "unexpected", "length"), ("M110", "lineno_reset")]) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',original='M110',type=None,tags=set([])), GcodeCommand('M117',param='Foobar',original='M117 Foobar',type='display_message',tags=set([])), GcodeCommand('M110',original='M110',type='lineno_reset',tags=set([]))] + >>> normalize_command_handler_result(GcodeCommand("M105",tags={"tag1", "tag2"}), ["M110", "M117 Foobar"]) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',original='M110',type=None,tags=set(['tag1', 'tag2'])), GcodeCommand('M117',param='Foobar',original='M117 Foobar',type=None,tags=set(['tag1', 'tag2']))] + >>> normalize_command_handler_result(GcodeCommand("M105",tags={"tag1", "tag2"}), ["M110", "M117 Foobar"], tags_to_add={"tag3"}) # doctest: +ALLOW_UNICODE + [GcodeCommand('M110',original='M110',type=None,tags=set(['tag1', 'tag2', 'tag3'])), GcodeCommand('M117',param='Foobar',original='M117 Foobar',type=None,tags=set(['tag1', 'tag2', 'tag3']))] Arguments: command (Command): The command for which the handler result was @@ -152,8 +151,9 @@ def normalize_command_handler_result(command, handler_results, tags_to_add=None) # entry is just a string, replace command with it if handler_result != original.line: # command changed, swap it - command = Command.from_line(handler_result, type=original.type, - tags=expand_tags(original.tags, tags_to_add)) + command = to_command(handler_result, + type=original.type, + tags=expand_tags(original.tags, tags_to_add)) result.append(command) elif isinstance(handler_result, Command): @@ -191,10 +191,10 @@ def normalize_command_handler_result(command, handler_results, tags_to_add=None) # command or command_type changed, tags need to be rewritten command_tags = expand_tags(command_tags, tags_to_add) - result.append(Command.from_line(command_line, type=command_type, tags=command_tags)) + result.append(to_command(command_line, type=command_type, tags=command_tags)) # reset to original - command, command_type, gcode, subcode, tags = original + command = original return result diff --git a/src/octoprint/util/__init__.py b/src/octoprint/util/__init__.py index e24f0a4cb..72a98759c 100644 --- a/src/octoprint/util/__init__.py +++ b/src/octoprint/util/__init__.py @@ -1598,11 +1598,11 @@ class dictview(collections.Mapping): """ >>> my_dict = dict(a="a", b="b", flag=True, value=23) >>> view = dictview(my_dict) - >>> to_native_str(view["a"]) + >>> view["a"] # doctest: +ALLOW_UNICODE 'a' >>> view["flag"] True - >>> view["foo"] + >>> view["foo"] # doctest: +ALLOW_UNICODE Traceback (most recent call last): ... KeyError: 'foo' @@ -1647,13 +1647,13 @@ class protectedkeydict(collections.MutableMapping): >>> protected = protectedkeydict(my_dict) >>> to_native_str(protected["a"]) 'a' - >>> protected["a"] = "b" + >>> protected["a"] = "b" # doctest: +ALLOW_UNICODE Traceback (most recent call last): ... KeyError: "Cannot set protected key 'a'" >>> protected["b"] = "b" >>> protected["b"] = "b2" - >>> del protected["a"] + >>> del protected["a"] # doctest: +ALLOW_UNICODE Traceback (most recent call last): ... KeyError: "Cannot delete protected key 'a'"