From 1d8a6cb35296cd6415ab317f2f31ad1a885f6c77 Mon Sep 17 00:00:00 2001 From: Oliver Fawcett-Griffiths Date: Sun, 4 Oct 2020 16:31:10 +1300 Subject: [PATCH] Implement further config validation Basically just parse the config file before we save it, ensuring that it can be parsed by `configparser` - the same parser used by Klipper. This PR depende on the changes from https://github.com/AliceGrey/OctoprintKlipperPlugin/pull/8 in order to send a warning popup when validation fails. --- octoprint_klipper/__init__.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/octoprint_klipper/__init__.py b/octoprint_klipper/__init__.py index 2a0d216..fc35621 100644 --- a/octoprint_klipper/__init__.py +++ b/octoprint_klipper/__init__.py @@ -22,6 +22,7 @@ import os from octoprint.util.comm import parse_firmware_line from .modules import KlipperLogAnalyzer import flask +import configparser class KlipperPlugin( octoprint.plugin.StartupPlugin, @@ -116,6 +117,17 @@ class KlipperPlugin( try: data["config"] = data["config"].encode('utf-8') + # Basic validation of config file - makes sure it parses + try: + parser = configparser.RawConfigParser() + parser.read_string(data["config"]) + except configParser.Error as error: + self._logger.error( + "Error: Invalid Klipper config file: {}".format(str(error)) + ) + self.sendMessage("errorPopUp","warning", "OctoKlipper Settings", "Invalid Klipper config file: " + str(error)) + + f = open(configpath, "w") f.write(data["config"]) f.close()