🚧 WIP: Fixing tests, part 3
This commit is contained in:
1 parent
02566ae9d0
commit
ba96ba8fe8
6 files changed
+54
-37
No files matched your search
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user