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.
This commit is contained in:
Oliver Fawcett-Griffiths 2020-10-04 16:31:10 +13:00
parent e553a69ff5
commit 1d8a6cb352
No known key found for this signature in database
GPG Key ID: 96ACA666FAE31484
1 changed files with 12 additions and 0 deletions

View File

@ -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()