diff options
Diffstat (limited to 'klippy/extras/save_variables.py')
-rw-r--r-- | klippy/extras/save_variables.py | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/klippy/extras/save_variables.py b/klippy/extras/save_variables.py index 2e405657..043f1c6d 100644 --- a/klippy/extras/save_variables.py +++ b/klippy/extras/save_variables.py @@ -6,10 +6,11 @@ # This file may be distributed under the terms of the GNU GPLv3 license. import os, logging, ast, configparser + class SaveVariables: def __init__(self, config): self.printer = config.get_printer() - self.filename = os.path.expanduser(config.get('filename')) + self.filename = os.path.expanduser(config.get("filename")) self.allVariables = {} try: if not os.path.exists(self.filename): @@ -17,28 +18,32 @@ class SaveVariables: self.loadVariables() except self.printer.command_error as e: raise config.error(str(e)) - gcode = self.printer.lookup_object('gcode') - gcode.register_command('SAVE_VARIABLE', self.cmd_SAVE_VARIABLE, - desc=self.cmd_SAVE_VARIABLE_help) + gcode = self.printer.lookup_object("gcode") + gcode.register_command( + "SAVE_VARIABLE", self.cmd_SAVE_VARIABLE, desc=self.cmd_SAVE_VARIABLE_help + ) + def loadVariables(self): allvars = {} varfile = configparser.ConfigParser() try: varfile.read(self.filename) - if varfile.has_section('Variables'): - for name, val in varfile.items('Variables'): + if varfile.has_section("Variables"): + for name, val in varfile.items("Variables"): allvars[name] = ast.literal_eval(val) except: msg = "Unable to parse existing variable file" logging.exception(msg) raise self.printer.command_error(msg) self.allVariables = allvars + cmd_SAVE_VARIABLE_help = "Save arbitrary variables to disk" + def cmd_SAVE_VARIABLE(self, gcmd): - varname = gcmd.get('VARIABLE') - if (varname.lower() != varname): + varname = gcmd.get("VARIABLE") + if varname.lower() != varname: raise gcmd.error("VARIABLE must not contain upper case") - value = gcmd.get('VALUE') + value = gcmd.get("VALUE") try: value = ast.literal_eval(value) except ValueError as e: @@ -47,9 +52,9 @@ class SaveVariables: newvars[varname] = value # Write file varfile = configparser.ConfigParser() - varfile.add_section('Variables') + varfile.add_section("Variables") for name, val in sorted(newvars.items()): - varfile.set('Variables', name, repr(val)) + varfile.set("Variables", name, repr(val)) try: f = open(self.filename, "w") varfile.write(f) @@ -59,8 +64,10 @@ class SaveVariables: logging.exception(msg) raise gcmd.error(msg) self.loadVariables() + def get_status(self, eventtime): - return {'variables': self.allVariables} + return {"variables": self.allVariables} + def load_config(config): return SaveVariables(config) |