From ba96ba8fe8b29dc9c456ed0029e0ca5f18ac7bf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Thu, 21 Feb 2019 15:29:41 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20WIP:=20Fixing=20tests,=20part=20?= =?UTF-8?q?3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/octoprint/comm/job/__init__.py | 5 ++-- .../comm/protocol/reprap/commands/__init__.py | 19 ++++++++++--- .../protocol/reprap/commands/atcommand.py | 12 ++++---- .../comm/protocol/reprap/commands/gcode.py | 28 ++++++++++--------- .../comm/protocol/reprap/flavors/generic.py | 5 ++-- .../comm/protocol/reprap/util/__init__.py | 22 +++++++-------- 6 files changed, 54 insertions(+), 37 deletions(-) diff --git a/src/octoprint/comm/job/__init__.py b/src/octoprint/comm/job/__init__.py index 7b44e3b23..2fd552222 100644 --- a/src/octoprint/comm/job/__init__.py +++ b/src/octoprint/comm/job/__init__.py @@ -17,6 +17,8 @@ from abc import ABCMeta, abstractmethod, abstractproperty from octoprint.util import monotonic_time +from future.utils import with_metaclass + class LastResult(object): def __init__(self): @@ -29,8 +31,7 @@ class LastResult(object): self.available = False -class Printjob(ProtocolListener, ListenerAware): - __metaclass__ = ABCMeta +class Printjob(with_metaclass(ABCMeta, ProtocolListener, ListenerAware)): parallel = False diff --git a/src/octoprint/comm/protocol/reprap/commands/__init__.py b/src/octoprint/comm/protocol/reprap/commands/__init__.py index c954f787c..21f30bb14 100644 --- a/src/octoprint/comm/protocol/reprap/commands/__init__.py +++ b/src/octoprint/comm/protocol/reprap/commands/__init__.py @@ -7,11 +7,22 @@ __copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms import copy import re +from future.utils import with_metaclass + +from octoprint.util import pp + + +_compiled_regex_type = type(re.compile(r"a")) +def _is_compiled_regex(obj): + return isinstance(obj, _compiled_regex_type) + + __registry = dict() def _register_command_class(cls): __registry[cls.__name__] = cls + def to_command(line, type=None, tags=None, **kwargs): if isinstance(line, Command): return line @@ -19,21 +30,21 @@ def to_command(line, type=None, tags=None, **kwargs): for cls in __registry.values(): if cls.pattern: # noinspection PyUnresolvedReferences,PyProtectedMember - if isinstance(cls.pattern, re._pattern_type) and cls.pattern.match(line): + if _is_compiled_regex(cls.pattern) and cls.pattern.match(line): return cls.from_line(line, type=type, tags=tags, **kwargs) elif callable(cls.pattern) and cls.pattern(line): return cls.from_line(line, type=type, tags=tags, **kwargs) return Command.from_line(line, type=type, tags=tags, **kwargs) + class CommandMetaClass(type): def __new__(mcs, *args, **kwargs): c = type.__new__(mcs, *args, **kwargs) _register_command_class(c) return c -class Command(object): - __metaclass__ = CommandMetaClass +class Command(with_metaclass(CommandMetaClass, object)): pattern = None @@ -51,7 +62,7 @@ class Command(object): self.tags = tags def __repr__(self): - return "{}({!r},type={!r},tags={!r})".format(self.__class__.__name__, self.line, self.type, self.tags) + return "{}({!r},type={!r},tags={})".format(self.__class__.__name__, self.line, self.type, pp(self.tags)) def __str__(self): return self.line diff --git a/src/octoprint/comm/protocol/reprap/commands/atcommand.py b/src/octoprint/comm/protocol/reprap/commands/atcommand.py index 62e9e79f7..4637a5aa2 100644 --- a/src/octoprint/comm/protocol/reprap/commands/atcommand.py +++ b/src/octoprint/comm/protocol/reprap/commands/atcommand.py @@ -6,6 +6,8 @@ __copyright__ = "Copyright (C) 2018 The OctoPrint Project - Released under terms from octoprint.comm.protocol.reprap.commands import Command +from octoprint.util import pp + class AtCommand(Command): pattern = staticmethod(lambda x: x.startswith("@")) @@ -30,9 +32,9 @@ class AtCommand(Command): super(AtCommand, self).__init__(line, **kwargs) def __repr__(self): - return "AtCommand({!r},{!r},{!r},type={!r},tags={!r}".format(self.line, - self.atcommand, - self.parameters, - self.type, - self.tags) + return "AtCommand({!r},{!r},{!r},type={!r},tags={}".format(self.line, + self.atcommand, + self.parameters, + self.type, + pp(self.tags)) diff --git a/src/octoprint/comm/protocol/reprap/commands/gcode.py b/src/octoprint/comm/protocol/reprap/commands/gcode.py index 22222a05f..95572dc96 100644 --- a/src/octoprint/comm/protocol/reprap/commands/gcode.py +++ b/src/octoprint/comm/protocol/reprap/commands/gcode.py @@ -9,6 +9,8 @@ from octoprint.comm.protocol.reprap.commands import Command import re import string +from octoprint.util import pp + class GcodeCommand(Command): pattern = staticmethod(lambda x: GcodeCommand.command_regex.match(x)) @@ -40,11 +42,11 @@ class GcodeCommand(Command): # not a gcode command return Command.from_line(line, **kwargs) - if match.group(b"GM"): - code = match.group(b"GM") + if match.group("GM"): + code = match.group("GM") - if match.group(b"subcode"): - args["subcode"] = int(match.group(b"subcode")) + if match.group("subcode"): + args["subcode"] = int(match.group("subcode")) if match.group(0) != line: rest = line[len(match.group(0)):] @@ -54,9 +56,9 @@ class GcodeCommand(Command): if not matched_param: break - key = matched_param.group(b"key").lower() - if matched_param.group(b"value"): - value = matched_param.group(b"value") + key = matched_param.group("key").lower() + if matched_param.group("value"): + value = matched_param.group("value") if "." in value: value = float(value) else: @@ -70,13 +72,13 @@ class GcodeCommand(Command): if rest: args["param"] = rest - elif match.group(b"T"): + elif match.group("T"): code = "T" - args["tool"] = int(match.group(b"tool")) + args["tool"] = int(match.group("tool")) - elif match.group(b"F"): + elif match.group("F"): code = "F" - args["feedrate"] = int(match.group(b"feedrate")) + args["feedrate"] = int(match.group("feedrate")) return GcodeCommand(code, **args) @@ -126,7 +128,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, + return "GcodeCommand({!r}{},type={!r},tags={})".format(self.code, "," + ",".join(args) if args else "", self.type, - self.tags) + pp(self.tags)) diff --git a/src/octoprint/comm/protocol/reprap/flavors/generic.py b/src/octoprint/comm/protocol/reprap/flavors/generic.py index f51ff3d93..d4cb7b0f0 100644 --- a/src/octoprint/comm/protocol/reprap/flavors/generic.py +++ b/src/octoprint/comm/protocol/reprap/flavors/generic.py @@ -13,6 +13,8 @@ from octoprint.comm.protocol.reprap.util import regex_float_pattern, regex_posit from octoprint.util import chunks, monotonic_time +from future.utils import with_metaclass + _flavor_registry = dict() @@ -37,8 +39,7 @@ class FlavorMeta(type): return cls -class GenericFlavor(object): - __metaclass__ = FlavorMeta +class GenericFlavor(with_metaclass(FlavorMeta, object)): key = "generic" name = "Generic Flavor" diff --git a/src/octoprint/comm/protocol/reprap/util/__init__.py b/src/octoprint/comm/protocol/reprap/util/__init__.py index c8a43ca22..24b3165bd 100644 --- a/src/octoprint/comm/protocol/reprap/util/__init__.py +++ b/src/octoprint/comm/protocol/reprap/util/__init__.py @@ -82,29 +82,29 @@ def normalize_command_handler_result(command, handler_results, tags_to_add=None) Examples: >>> 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([]))] + [GcodeCommand('M105',type=None,tags=set())] >>> normalize_command_handler_result(GcodeCommand("M105"), GcodeCommand("M110")) # doctest: +ALLOW_UNICODE - [GcodeCommand('M110',type=None,tags=set([]))] + [GcodeCommand('M110',type=None,tags=set())] >>> normalize_command_handler_result(GcodeCommand("M105"), ["M110"]) # doctest: +ALLOW_UNICODE - [GcodeCommand('M110',original='M110',type=None,tags=set([]))] + [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([]))] + [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([]))] + [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([]))] + [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(GcodeCommand("M105"), ["M110", None]) # doctest: +ALLOW_UNICODE - [GcodeCommand('M110',original='M110',type=None,tags=set([]))] + [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([]))] + [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([]))] + [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']))] + [GcodeCommand('M110',original='M110',type=None,tags={'tag1', 'tag2'}), GcodeCommand('M117',param='Foobar',original='M117 Foobar',type=None,tags={'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']))] + [GcodeCommand('M110',original='M110',type=None,tags={'tag1', 'tag2', 'tag3'}), GcodeCommand('M117',param='Foobar',original='M117 Foobar',type=None,tags={'tag1', 'tag2', 'tag3'})] Arguments: command (Command): The command for which the handler result was