diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2019-06-07 19:30:17 -0400 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2019-06-07 19:33:31 -0400 |
commit | 8b005808848dd5395792dba8b9f470ac441c5e72 (patch) | |
tree | d14322e6056d6652d3e15e77fb06698cdf588978 /klippy/extras/gcode_macro.py | |
parent | 01f3b50e7399f3ea327c6de2130dc16b6f8a27f5 (diff) | |
download | kutter-8b005808848dd5395792dba8b9f470ac441c5e72.tar.gz kutter-8b005808848dd5395792dba8b9f470ac441c5e72.tar.xz kutter-8b005808848dd5395792dba8b9f470ac441c5e72.zip |
gcode_macro: Parse variable_X parameters using ast.literal_eval()
Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'klippy/extras/gcode_macro.py')
-rw-r--r-- | klippy/extras/gcode_macro.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/klippy/extras/gcode_macro.py b/klippy/extras/gcode_macro.py index 977a763d..1918b0b9 100644 --- a/klippy/extras/gcode_macro.py +++ b/klippy/extras/gcode_macro.py @@ -3,7 +3,7 @@ # Copyright (C) 2018-2019 Kevin O'Connor <kevin@koconnor.net> # # This file may be distributed under the terms of the GNU GPLv3 license. -import traceback, logging +import traceback, logging, ast import jinja2 @@ -100,9 +100,16 @@ class GCodeMacro: prefix = 'default_parameter_' self.kwparams = { o[len(prefix):].upper(): config.get(o) for o in config.get_prefix_options(prefix) } + self.variables = {} prefix = 'variable_' - self.variables = { o[len(prefix):]: config.get(o) - for o in config.get_prefix_options(prefix) } + for option in config.get_prefix_options(prefix): + try: + self.variables[option[len(prefix):]] = ast.literal_eval( + config.get(option)) + except ValueError as e: + raise config.error( + "Option '%s' in section '%s' is not a valid literal" % ( + option, config.get_name())) def get_status(self, eventtime): return dict(self.variables) cmd_SET_GCODE_VARIABLE_help = "Set the value of a G-Code macro variable" @@ -115,7 +122,12 @@ class GCodeMacro: return raise self.gcode.error("Unknown gcode_macro variable '%s'" % ( variable,)) - self.variables[variable] = value + try: + literal = ast.literal_eval(value) + except ValueError as e: + raise self.gcode.error("Unable to parse '%s' as a literal" % ( + value,)) + self.variables[variable] = literal cmd_desc = "G-Code macro" def cmd(self, params): if self.in_script: |