aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2024-11-26 13:30:57 -0500
committerKevin O'Connor <kevin@koconnor.net>2024-12-01 14:16:13 -0500
commitd45b9c92d8711592aeca5af65ff50a2fcfe55f2b (patch)
tree9d088c7a68d0b9eeb8d5287dbbf992ed96cae381
parent49205f92ff7a2ee8d3ed5dc9992afb74d82c597f (diff)
downloadkutter-d45b9c92d8711592aeca5af65ff50a2fcfe55f2b.tar.gz
kutter-d45b9c92d8711592aeca5af65ff50a2fcfe55f2b.tar.xz
kutter-d45b9c92d8711592aeca5af65ff50a2fcfe55f2b.zip
gcode: Improve handling of extended g-code commands with '*;#' characters
The g-code command parser did not allow three characters to be passed as parameters to commands (asterisk, semicolon, pound sign). Rework the parsing code to better leverage the python shlex package so that these characters can be supported. In particular, this should allow better support for printing g-code files that have unusual characters in the filename. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
-rw-r--r--klippy/gcode.py26
1 files changed, 11 insertions, 15 deletions
diff --git a/klippy/gcode.py b/klippy/gcode.py
index 8a6b3b8d..dc110261 100644
--- a/klippy/gcode.py
+++ b/klippy/gcode.py
@@ -1,6 +1,6 @@
# Parse gcode commands
#
-# Copyright (C) 2016-2021 Kevin O'Connor <kevin@koconnor.net>
+# Copyright (C) 2016-2024 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import os, re, logging, collections, shlex
@@ -253,26 +253,22 @@ class GCodeDispatch:
def _respond_state(self, state):
self.respond_info("Klipper state: %s" % (state,), log=False)
# Parameter parsing helpers
- extended_r = re.compile(
- r'^\s*(?:N[0-9]+\s*)?'
- r'(?P<cmd>[a-zA-Z_][a-zA-Z0-9_]+)(?:\s+|$)'
- r'(?P<args>[^#*;]*?)'
- r'\s*(?:[#*;].*)?$')
def _get_extended_params(self, gcmd):
- m = self.extended_r.match(gcmd.get_commandline())
- if m is None:
- raise self.error("Malformed command '%s'"
- % (gcmd.get_commandline(),))
- eargs = m.group('args')
+ rawparams = gcmd.get_raw_command_parameters()
+ # Extract args while allowing shell style quoting
+ s = shlex.shlex(rawparams, posix=True)
+ s.whitespace_split = True
+ s.commenters = '#;'
try:
- eparams = [earg.split('=', 1) for earg in shlex.split(eargs)]
+ eparams = [earg.split('=', 1) for earg in s]
eparams = { k.upper(): v for k, v in eparams }
- gcmd._params.clear()
- gcmd._params.update(eparams)
- return gcmd
except ValueError as e:
raise self.error("Malformed command '%s'"
% (gcmd.get_commandline(),))
+ # Update gcmd with new parameters
+ gcmd._params.clear()
+ gcmd._params.update(eparams)
+ return gcmd
# G-Code special command handlers
def cmd_default(self, gcmd):
cmd = gcmd.get_command()