From 3478bc06ee327d8e2a9ec53d1bcd23971837ac77 Mon Sep 17 00:00:00 2001
From: thelastWallE <12502210+thelastWallE@users.noreply.github.com>
Date: Wed, 3 Nov 2021 11:10:14 +0100
Subject: [PATCH] :zap: refactor configcheck
- move the decission to check the config from serverside to clientside
- add confirmation for saving a faulty config
---
octoprint_klipper/__init__.py | 3 +-
octoprint_klipper/cfgUtils.py | 17 +-
octoprint_klipper/static/js/klipper.js | 4 +-
octoprint_klipper/static/js/klipper_editor.js | 165 +++++++++++-------
4 files changed, 110 insertions(+), 79 deletions(-)
diff --git a/octoprint_klipper/__init__.py b/octoprint_klipper/__init__.py
index 862b6f3..5a41737 100644
--- a/octoprint_klipper/__init__.py
+++ b/octoprint_klipper/__init__.py
@@ -106,6 +106,7 @@ class KlipperPlugin(
]
def get_settings_defaults(self):
+ # TODO #69 put some settings on the localStorage
return dict(
connection=dict(
port="/tmp/printer",
@@ -563,7 +564,7 @@ class KlipperPlugin(
def check_config(self):
data = flask.request.json
data_to_check = data.get("DataToCheck", [])
- response = cfgUtils.check_cfg(self, data_to_check)
+ response = cfgUtils.check_cfg_ok(self, data_to_check)
return flask.jsonify(is_syntax_ok = response)
# save a configfile
diff --git a/octoprint_klipper/cfgUtils.py b/octoprint_klipper/cfgUtils.py
index 3ae3eff..7ce6f2d 100644
--- a/octoprint_klipper/cfgUtils.py
+++ b/octoprint_klipper/cfgUtils.py
@@ -124,13 +124,6 @@ def save_cfg(self, content, filename):
filename += ".cfg"
filepath = os.path.join(configpath, filename)
- logger.log_debug(self, "save filepath: {}".format(filepath))
-
- check_parse = self._settings.get(["configuration", "parse_check"])
- logger.log_debug(self, "check_parse on filesave: {}".format(check_parse))
-
- if check_parse and not check_cfg(self, content):
- return False
logger.log_debug(self, "Writing Klipper config to {}".format(filepath))
try:
@@ -147,7 +140,7 @@ def save_cfg(self, content, filename):
copy_cfg_to_backup(self, filepath)
-def check_cfg(self, data):
+def check_cfg_ok(self, data):
"""Checks the given data on parsing errors.
Args:
@@ -190,14 +183,6 @@ def show_error_message(self, error):
('Error: Invalid Klipper config file:\n' + '{}'.format(str(error))),
)
- util.send_message(
- self,
- type = 'PopUp',
- subtype = 'warning',
- title = 'Invalid Config data\n',
- payload = ('\n' + str(error))
- )
-
def is_float_ok(self, dataToValidated):
diff --git a/octoprint_klipper/static/js/klipper.js b/octoprint_klipper/static/js/klipper.js
index 4b56809..89655e1 100644
--- a/octoprint_klipper/static/js/klipper.js
+++ b/octoprint_klipper/static/js/klipper.js
@@ -252,7 +252,7 @@ $(function () {
};
self.saveOption = function(dir, option, value) {
- if (! (_.includes(["fontsize", "confirm_reload"], option)) ) {
+ if (! (_.includes(["fontsize", "confirm_reload", "parse_check"], option)) ) {
return;
}
@@ -302,7 +302,7 @@ $(function () {
showConfirmationDialog({
title: gettext("Restart Klipper?"),
html: html,
- proceed: [gettext("Restart"), gettext("Restart and don't show again.")],
+ proceed: [gettext("Restart"), gettext("Restart and don't show this again.")],
onproceed: function (idx) {
if (idx > -1) {
request(idx);
diff --git a/octoprint_klipper/static/js/klipper_editor.js b/octoprint_klipper/static/js/klipper_editor.js
index 7eea08f..2576cf6 100644
--- a/octoprint_klipper/static/js/klipper_editor.js
+++ b/octoprint_klipper/static/js/klipper_editor.js
@@ -44,7 +44,7 @@ $(function () {
self.checkExternChange();
editor.focus();
self.setEditorDivSize();
- }
+ };
self.close_selection = function (index) {
switch (index) {
@@ -58,7 +58,7 @@ $(function () {
self.saveCfg({closing: true});
break;
}
- }
+ };
self.closeEditor = function () {
self.CfgContent(editor.getValue());
@@ -82,11 +82,11 @@ $(function () {
} else {
editordialog.modal('hide');
}
- }
+ };
self.addStyleAttribute = function ($element, styleAttribute) {
$element.attr('style', styleAttribute);
- }
+ };
self.setEditorDivSize = function () {
var klipper_modal_body= $('#klipper_editor .modal-body');
@@ -120,7 +120,7 @@ $(function () {
);
}
});
- }
+ };
self.onDataUpdaterPluginMessage = function (plugin, data) {
//receive from backend after a SAVE_CONFIG
@@ -160,30 +160,55 @@ $(function () {
});
}
}
- }
+ };
+
+ self.askSaveFaulty = function () {
+ return new Promise(function (resolve) {
+ var html = "
" +
+ gettext("Your configuration seems to be faulty.") +
+ "
";
+
+ showConfirmationDialog({
+ title: gettext("Save faulty Configuration?"),
+ html: html,
+ cancel: gettext("Do not save!"),
+ proceed: [gettext("Save anyway!"), gettext("Save anyway and don't show this again.")],
+ onproceed: function (idx) {
+ if (idx == 0) {
+ resolve(true);
+ } else {
+ self.klipperViewModel.saveOption("configuration", "parse_check", false);
+ resolve(true);
+ }
+ },
+ oncancel: function () {
+ resolve(false);
+ }
+ });
+ });
+ };
self.checkSyntax = function () {
- if (editor.session) {
- self.klipperViewModel.consoleMessage("debug", "checkSyntax started");
+ return new Promise((resolve, reject) => {
+ if (editor.session) {
+ self.klipperViewModel.consoleMessage("debug", "checkSyntax started");
- OctoPrint.plugins.klipper.checkCfg(editor.session.getValue())
- .done(function (response) {
- var msg = ""
- if (response.is_syntax_ok == true) {
- self.klipperViewModel.showPopUp("success", gettext("SyntaxCheck"), gettext("SyntaxCheck OK"));
- self.editorFocusDelay(1000);
- } else {
- msg = gettext('Syntax NOK')
- showMessageDialog(
- msg,
- {
- title: gettext("SyntaxCheck"),
- onclose: function () { self.editorFocusDelay(1000); }
- }
- );
- }
- });
- };
+ OctoPrint.plugins.klipper.checkCfg(editor.session.getValue())
+ .done(function (response) {
+ if (response.is_syntax_ok == true) {
+ self.klipperViewModel.showPopUp("success", gettext("SyntaxCheck"), gettext("SyntaxCheck OK"));
+ self.editorFocusDelay(1000);
+ resolve(true);
+ } else {
+ self.editorFocusDelay(1000);
+ resolve(false);
+ }
+ })
+ .fail(function () {
+ reject(false);
+ });
+ } else { reject(false); }
+ });
};
self.saveCfg = function (options) {
@@ -193,28 +218,54 @@ $(function () {
if (editor.session) {
self.klipperViewModel.consoleMessage("debug", "SaveCfg start");
- OctoPrint.plugins.klipper.saveCfg(editor.session.getValue(), self.CfgFilename())
- .done(function (response) {
+ var saveRequest = function () {
+ OctoPrint.plugins.klipper.saveCfg(editor.session.getValue(), self.CfgFilename())
+ .done(function (response) {
- if (response.saved === true) {
- self.klipperViewModel.showPopUp("success", gettext("Save Config"), gettext("File saved."));
- self.loadedConfig = editor.session.getValue(); //set loaded config to current for resetting dirtyEditor
- if (closing) {
- editordialog.modal('hide');
- }
- if (self.settings.settings.plugins.klipper.configuration.restart_onsave()==true) {
- self.klipperViewModel.requestRestart();
- }
- } else {
- showMessageDialog(
- gettext('File not saved!'),
- {
- title: gettext("Save Config"),
- onclose: function () { self.editorFocusDelay(1000); }
+ if (response.saved === true) {
+ self.klipperViewModel.showPopUp("success", gettext("Save Config"), gettext("File saved."));
+ self.loadedConfig = editor.session.getValue(); //set loaded config to current for resetting dirtyEditor
+ if (closing) {
+ editordialog.modal('hide');
}
- )
- }
- });
+ if (self.settings.settings.plugins.klipper.configuration.restart_onsave() == true) {
+ self.klipperViewModel.requestRestart();
+ }
+ } else {
+ showMessageDialog(
+ gettext('File not saved!'),
+ {
+ title: gettext("Save Config"),
+ onclose: function () { self.editorFocusDelay(1000); }
+ }
+ );
+ }
+ });
+ };
+
+ if (self.settings.settings.plugins.klipper.configuration.parse_check() == true) {
+ self.checkSyntax().then((syntaxOK) => {
+ if (syntaxOK === false) {
+ self.askSaveFaulty().then((areWeSaving) => {
+ if (areWeSaving === false) {
+ showMessageDialog(
+ gettext('Faulty config not saved!'),
+ {
+ title: gettext("Save Config"),
+ onclose: function () { self.editorFocusDelay(1000); }
+ }
+ );
+ } else {
+ saveRequest();
+ }
+ });
+ } else {
+ saveRequest();
+ }
+ });
+ } else {
+ saveRequest();
+ }
}
};
@@ -227,11 +278,9 @@ $(function () {
self.settings.settings.plugins.klipper.configuration.fontsize(9);
}
- var fontsize = self.settings.settings.plugins.klipper.configuration.fontsize()
+ var fontsize = self.settings.settings.plugins.klipper.configuration.fontsize();
if (editor) {
- editor.setFontSize(
- fontsize
- );
+ editor.setFontSize(fontsize);
editor.resize();
}
@@ -247,11 +296,9 @@ $(function () {
self.settings.settings.plugins.klipper.configuration.fontsize(20);
}
- var fontsize = self.settings.settings.plugins.klipper.configuration.fontsize()
+ var fontsize = self.settings.settings.plugins.klipper.configuration.fontsize();
if (editor) {
- editor.setFontSize(
- fontsize
- );
+ editor.setFontSize(fontsize);
editor.resize();
}
self.klipperViewModel.saveOption("configuration", "fontsize", fontsize);
@@ -262,13 +309,12 @@ $(function () {
.done(function (response) {
self.klipperViewModel.consoleMessage("debug", "reloadFromFile done");
if (response.response.text != "") {
- var msg = response.response.text
showMessageDialog(
- msg,
+ response.response.text,
{
title: gettext("Reload File")
}
- )
+ );
} else {
self.klipperViewModel.showPopUp("success", gettext("Reload Config"), gettext("File reloaded."));
self.CfgChangedExtern = false;
@@ -281,13 +327,12 @@ $(function () {
}
})
.fail(function (response) {
- var msg = response
showMessageDialog(
- msg,
+ response,
{
title: gettext("Reload File")
}
- )
+ );
});
};