Extracted all used regexes in the comm layer and precompiled them

Possible improvement of #390

(cherry picked from commit 89cae22b91)

Conflicts:
	src/octoprint/util/comm.py
This commit is contained in:
Gina Häußge 2014-03-03 09:20:59 +01:00
parent 82ee48b166
commit 9107fd839b

View file

@ -177,6 +177,19 @@ class MachineCom(object):
# print job
self._currentFile = None
# regexes
floatPattern = "[-+]?[0-9]*\.?[0-9]+"
intPattern = "[0-9]+"
self._regex_command = re.compile("^\s*([GM]\d+|T)")
self._regex_float = re.compile(floatPattern)
self._regex_paramZFloat = re.compile("Z(%s)" % floatPattern)
self._regex_paramSInt = re.compile("S(%s)" % intPattern)
self._regex_paramNInt = re.compile("N(%s)" % intPattern)
self._regex_paramTInt = re.compile("T(%s)" % intPattern)
self._regex_minMaxError = re.compile("Error:[0-9]\n")
self._regex_sdPrintingByte = re.compile("([0-9]*)/([0-9]*)")
self._regex_sdFileOpened = re.compile("File opened:\s*(.*?)\s+Size:\s*([0-9]*)")
def __del__(self):
self.close()
@ -647,12 +660,12 @@ class MachineCom(object):
self._callback.mcSdFiles(self._sdFiles)
elif 'SD printing byte' in line:
# answer to M27, at least on Marlin, Repetier and Sprinter: "SD printing byte %d/%d"
match = re.search("([0-9]*)/([0-9]*)", line)
match = self._regex_sdPrintingByte.search("([0-9]*)/([0-9]*)", line)
self._currentFile.setFilepos(int(match.group(1)))
self._callback.mcProgress()
elif 'File opened' in line:
# answer to M23, at least on Marlin, Repetier and Sprinter: "File opened:%s Size:%d"
match = re.search("File opened:\s*(.*?)\s+Size:\s*([0-9]*)", line)
match = self._regex_sdFileOpened.search(line)
self._currentFile = PrintingSdFileInformation(match.group(1), int(match.group(2)))
elif 'File selected' in line:
# final answer to M23, at least on Marlin, Repetier and Sprinter: "File selected"
@ -894,7 +907,7 @@ class MachineCom(object):
# Marlin reports an MIN/MAX temp error as "Error:x\n: Extruder switched off. MAXTEMP triggered !\n"
# But a bed temp error is reported as "Error: Temperature heated bed switched off. MAXTEMP triggered !!"
# So we can have an extra newline in the most common case. Awesome work people.
if re.match('Error:[0-9]\n', line):
if self._regex_minMaxError.match(line):
line = line.rstrip() + self._readline()
#Skip the communication errors, as those get corrected.
if 'checksum mismatch' in line \
@ -1004,7 +1017,7 @@ class MachineCom(object):
return
if not self.isStreaming():
gcode = re.search("^\s*([GM]\d+|T)", cmd)
gcode = self._regex_command.search(cmd)
if gcode:
gcode = gcode.group(1)
@ -1060,13 +1073,15 @@ class MachineCom(object):
def _gcode_G0(self, cmd):
if 'Z' in cmd:
try:
z = float(re.search('Z([0-9\.]*)', cmd).group(1))
if self._currentZ != z:
self._currentZ = z
self._callback.mcZChange(z)
except ValueError:
pass
match = self._regex_paramZFloat.search(cmd)
if match:
try:
z = float(match.group(1))
if self._currentZ != z:
self._currentZ = z
self._callback.mcZChange(z)
except ValueError:
pass
return cmd
_gcode_G1 = _gcode_G0
@ -1077,10 +1092,10 @@ class MachineCom(object):
def _gcode_M104(self, cmd):
toolNum = self._currentExtruder
toolMatch = re.search('T([0-9]+)', cmd)
toolMatch = self._regex_paramTInt.search(cmd)
if toolMatch:
toolNum = int(toolMatch.group(1))
match = re.search('S([0-9]+)', cmd)
match = self._regex_paramSInt.search(cmd)
if match:
try:
self._targetTemp[toolNum] = float(match.group(1))
@ -1089,7 +1104,7 @@ class MachineCom(object):
return cmd
def _gcode_M140(self, cmd):
match = re.search('S([0-9]+)', cmd)
match = self._regex_paramSInt.search(cmd)
if match:
try:
self._bedTargetTemp = float(match.group(1))
@ -1107,7 +1122,7 @@ class MachineCom(object):
def _gcode_M110(self, cmd):
newLineNumber = None
match = re.search("N([0-9]+)", cmd)
match = self._regex_paramNInt.search(cmd)
if match:
try:
newLineNumber = int(match.group(1))