From a20edd7941e792f9df36aa5f65c913612271cb37 Mon Sep 17 00:00:00 2001 From: ballaswag Date: Wed, 17 Jan 2024 02:35:26 -0800 Subject: [PATCH] add guppy config helper --- k1/k1_mods/guppy_config_helper.py | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 k1/k1_mods/guppy_config_helper.py diff --git a/k1/k1_mods/guppy_config_helper.py b/k1/k1_mods/guppy_config_helper.py new file mode 100644 index 0000000..59a2fef --- /dev/null +++ b/k1/k1_mods/guppy_config_helper.py @@ -0,0 +1,32 @@ +class GuppyConfigHelper: + def __init__(self, config): + self.printer = config.get_printer() + + # Register commands + gcode = config.get_printer().lookup_object('gcode') + gcode.register_command("GUPPY_SAVE_CONFIG", self.cmd_guppy_save_config) + gcode.register_command("GUPPY_DELETE_CONFIG", self.cmd_guppy_delete_config) + + def cmd_guppy_save_config(self, gcmd): + self.section = gcmd.get('SECTION', None) + self.pairs = gcmd.get('KEY_VALUE', None) + + if self.section and self.pairs: + configfile = self.printer.lookup_object('configfile') + kv = self.pairs.split(',') + d = dict(s.split(':') for s in kv) + + configfile.remove_section(self.section) + + for k, v in d.items(): + configfile.set(self.section, k, v) + + def cmd_guppy_delete_config(self, gcmd): + self.section = gcmd.get('SECTION', None) + + if self.section: + configfile = self.printer.lookup_object('configfile') + configfile.remove_section(self.section) + +def load_config(config): + return GuppyConfigHelper(config)