From c3d7d7980cb6caf4620a2ccb8df102d3d77642f3 Mon Sep 17 00:00:00 2001 From: thelastWallE <12502210+thelastWallE@users.noreply.github.com> Date: Wed, 7 Apr 2021 00:00:42 +0200 Subject: [PATCH] added parsingCheck Checkbox -Strict mode disabled for parsing checks -reworked debug messages --- .editorconfig | 2 +- octoprint_klipper/__init__.py | 92 +++++---- octoprint_klipper/static/css/klipper.css | 8 + octoprint_klipper/static/js/klipper.js | 18 +- .../static/js/klipper_settings.js | 12 +- .../static/js/lib/ace/mode-klipper_config.js | 84 ++++----- .../templates/klipper_settings.jinja2 | 80 ++++---- .../templates/klipper_tab_main.jinja2 | 178 +++++++++++------- 8 files changed, 269 insertions(+), 205 deletions(-) diff --git a/.editorconfig b/.editorconfig index cd7618d..d02b3d9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -21,4 +21,4 @@ indent_size = 2 indent_size = 2 [**.jinja2] -indent_size = 3 +indent_size = 2 diff --git a/octoprint_klipper/__init__.py b/octoprint_klipper/__init__.py index cd0cb95..2d544b4 100644 --- a/octoprint_klipper/__init__.py +++ b/octoprint_klipper/__init__.py @@ -44,7 +44,7 @@ class KlipperPlugin( octoprint.plugin.EventHandlerPlugin): _parsing_response = False - _parsing_check_response = False + _parsing_check_response = True _message = "" def __init__(self): @@ -128,7 +128,8 @@ class KlipperPlugin( old_config="", logpath="/tmp/klippy.log", reload_command="RESTART", - navbar=True + navbar=True, + parse_check=False ) ) @@ -160,24 +161,35 @@ class KlipperPlugin( ) if "config" in data: - try: - if sys.version_info[0] < 3: - data["config"] = data["config"].encode('utf-8') + if self.key_exist(data, "configuration", "parse_check"): + check_parse = data["configuration"]["parse_check"] + else: + check_parse = self._settings.get(["configuration", "parse_check"]) - # check for configpath if it was changed during changing of the configfile - if self.key_exist(data, "configuration", "configpath"): - configpath = os.path.expanduser( - data["configuration"]["configpath"] - ) - else: - # if the configpath was not changed during changing the printer.cfg. Then the configpath would not be in data[] - configpath = os.path.expanduser( - self._settings.get(["configuration", "configpath"]) - ) - if self.file_exist(configpath) and self._parsing_check_response: + if sys.version_info[0] < 3: + data["config"] = data["config"].encode('utf-8') + + # check for configpath if it was changed during changing of the configfile + if self.key_exist(data, "configuration", "configpath"): + configpath = os.path.expanduser( + data["configuration"]["configpath"] + ) + else: + # if the configpath was not changed during changing the printer.cfg. Then the configpath would not be in data[] + configpath = os.path.expanduser( + self._settings.get(["configuration", "configpath"]) + ) + if self.file_exist(configpath) and (self._parsing_check_response or not check_parse): + try: f = open(configpath, "w") f.write(data["config"]) f.close() + + + self.log_debug("Writing Klipper config to {}".format(configpath)) + except IOError: + self.log_error("Error: Couldn't write Klipper config file: {}".format(configpath)) + else: #load the reload command from changed data if it is not existing load the saved setting if self.key_exist(data, "configuration", "reload_command"): reload_command = os.path.expanduser( @@ -189,14 +201,9 @@ class KlipperPlugin( if reload_command != "manually": # Restart klippy to reload config self._printer.commands(reload_command) - self.log_info("Reloading Klipper Configuration.") - - self.log_debug("Writing Klipper config to {}".format(configpath)) - except IOError: - self.log_error("Error: Couldn't write Klipper config file: {}".format(configpath)) - else: - # we dont want to write the klipper conf to the octoprint settings - data.pop("config", None) + self.log_info("Restarting Klipper.") + # we dont want to write the klipper conf to the octoprint settings + data.pop("config", None) # save the rest of changed settings into config.yaml of octoprint old_debug_logging = self._settings.get_boolean(["configuration", "debug_logging"]) @@ -367,22 +374,32 @@ class KlipperPlugin( if "FIRMWARE_VERSION" in printerInfo: self.log_info("Firmware version: {}".format( printerInfo["FIRMWARE_VERSION"])) + elif "// probe" in line: + msg = line.strip('/') + self.log_info(msg) + write_parsing_response_buffer() elif "//" in line: + # add lines with // to a buffer self._message = self._message + line.strip('/') if not self._parsing_response: self.update_status("info", self._message) self._parsing_response = True + elif "!!" in line: + msg = line.strip('!') + self.update_status("error", msg) + self.log_error(msg) + self.write_parsing_response_buffer() else: - if self._parsing_response: - self._parsing_response = False - self.log_info(self._message) - self._message = "" - if "!!" in line: - msg = line.strip('!') - self.update_status("error", msg) - self.log_error(msg) + self.write_parsing_response_buffer() return line + def write_parsing_response_buffer(self): + # write buffer with // lines after a gcode response without // + if self._parsing_response: + self._parsing_response = False + self.log_info(self._message) + self._message = "" + def get_api_commands(self): return dict( listLogFiles=[], @@ -441,10 +458,11 @@ class KlipperPlugin( elif command == "checkConfig": if "config" in data: if not self.validate_configfile(data["config"]): - self.log_debug("validateConfig ->" + data["config"]) + self.log_debug("validateConfig not ok") self._settings.set(["configuration", "old_config"], data["config"]) return flask.jsonify(checkConfig="not OK") else: + self.log_debug("validateConfig ok") self._settings.set(["configuration", "old_config"], "") return flask.jsonify(checkConfig="OK") @@ -494,7 +512,6 @@ class KlipperPlugin( def log_info(self, message): self._octoklipper_logger.info(message) self.send_message("log", "info", message, message) - self.send_message("console", "info", message, message) def log_debug(self, message): self._octoklipper_logger.debug(message) @@ -505,7 +522,6 @@ class KlipperPlugin( def log_error(self, error): self._octoklipper_logger.error(error) self.send_message("log", "error", error, error) - self.send_message("console", "error", error, error) def file_exist(self, filepath): if not os.path.isfile(filepath): @@ -544,15 +560,13 @@ class KlipperPlugin( # learn file to be validated try: - dataToValidated = configparser.RawConfigParser() + dataToValidated = configparser.RawConfigParser(strict=False) # if sys.version_info[0] < 3: buf = StringIO.StringIO(dataToBeValidated) dataToValidated.readfp(buf) else: dataToValidated.read_string(dataToBeValidated) - except configparser.DuplicateSectionError: - self._parsing_check_response = True except configparser.Error as error: if sys.version_info[0] < 3: error.message = error.message.replace("\\n","") @@ -568,7 +582,7 @@ class KlipperPlugin( "Error: Invalid Klipper config file:\n" + "{}".format(str(error)) ) - self.send_message("PopUp", "warning", "Invalid Config", + self.send_message("PopUp", "warning", "OctoKlipper: Invalid Config data\n", "Config got not saved!\n" + "You can reload your last changes\n" + "on the 'Klipper Configuration' tab.\n\n" + str(error)) diff --git a/octoprint_klipper/static/css/klipper.css b/octoprint_klipper/static/css/klipper.css index cd77fcd..75dcb26 100644 --- a/octoprint_klipper/static/css/klipper.css +++ b/octoprint_klipper/static/css/klipper.css @@ -113,6 +113,14 @@ div#settings_plugin_klipper.tab-pane.active form.form-horizontal div.tab-content margin: 0px 2px 2px 2px; } +div#settings_plugin_klipper.tab-pane.active form.form-horizontal div.tab-content div#conf.tab-pane.active div.control-group label.inline input.inline-checkbox { + vertical-align:-0.2em; +} + +div#settings_plugin_klipper.tab-pane.active form.form-horizontal div.tab-content div#conf.tab-pane.active div.control-group label.inline { + display:inline; +} + #macros #item.control-group { margin-bottom: 2px; border: 2px solid #ccc; diff --git a/octoprint_klipper/static/js/klipper.js b/octoprint_klipper/static/js/klipper.js index 7b6e5f7..2fc62e1 100644 --- a/octoprint_klipper/static/js/klipper.js +++ b/octoprint_klipper/static/js/klipper.js @@ -16,7 +16,6 @@ $(function () { function KlipperViewModel(parameters) { var self = this; - var console_debug = false; self.header = OctoPrint.getRequestHeaders({ "content-type": "application/json", @@ -145,6 +144,7 @@ $(function () { break; default: self.logMessage(data.time, data.subtype, data.payload); + self.consoleMessage(data.subtype, data.payload); } //if ("warningPopUp" == data.type){ @@ -178,14 +178,14 @@ $(function () { }; self.consoleMessage = function (type, message) { - if (type == "info"){ - console.info("OctoKlipper : " + message); - } else if (type == "debug"){ - if (console_debug){ - console.debug("OctoKlipper : " + message); + if (self.settings.settings.plugins.klipper.configuration.debug_logging() === true) { + if (type == "info"){ + console.info("OctoKlipper : " + message); + } else if (type == "debug"){ + console.debug("OctoKlipper : " + message); + } else { + console.error("OctoKlipper : " + message); } - } else { - console.error("OctoKlipper : " + message); } return }; @@ -204,7 +204,7 @@ $(function () { $.ajax(settings).done(function (response) { self.consoleMessage( "debug", - "Reloaded from Backend " + response); + "Reloaded config file from Backend"); }); } diff --git a/octoprint_klipper/static/js/klipper_settings.js b/octoprint_klipper/static/js/klipper_settings.js index 02d0342..732560c 100644 --- a/octoprint_klipper/static/js/klipper_settings.js +++ b/octoprint_klipper/static/js/klipper_settings.js @@ -21,6 +21,7 @@ $(function() { var editor = null; self.settings = parameters[0]; + self.klipperViewModel = parameters[1]; self.header = OctoPrint.getRequestHeaders({ "content-type": "application/json", @@ -30,8 +31,8 @@ $(function() { self.apiUrl = OctoPrint.getSimpleApiUrl("klipper"); self.onSettingsBeforeSave = function () { - if (editor.session) { - //console.debug("OctoKlipper : onSettingsBeforeSave:" + editor.session.getValue()) + if (editor.session && self.settings.settings.plugins.klipper.configuration.parse_check() === true) { + self.klipperViewModel.consoleMessage("debug", "onSettingsBeforeSave:") var settings = { "crossDomain": true, "url": self.apiUrl, @@ -108,7 +109,7 @@ $(function() { self.loadLastSession = function () { if (self.settings.settings.plugins.klipper.configuration.old_config() != "") { - console.debug("OctoKlipper : lastSession:" + self.settings.settings.plugins.klipper.configuration.old_config()) + self.klipperViewModel.consoleMessage("info","lastSession:" + self.settings.settings.plugins.klipper.configuration.old_config()) if (editor.session) { editor.session.setValue(self.settings.settings.plugins.klipper.configuration.old_config()); editor.clearSelection(); @@ -194,7 +195,10 @@ $(function() { OCTOPRINT_VIEWMODELS.push({ construct: KlipperSettingsViewModel, - dependencies: ["settingsViewModel"], + dependencies: [ + "settingsViewModel", + "klipperViewModel" + ], elements: ["#settings_plugin_klipper"] }); }); diff --git a/octoprint_klipper/static/js/lib/ace/mode-klipper_config.js b/octoprint_klipper/static/js/lib/ace/mode-klipper_config.js index 20b621b..4e2978c 100644 --- a/octoprint_klipper/static/js/lib/ace/mode-klipper_config.js +++ b/octoprint_klipper/static/js/lib/ace/mode-klipper_config.js @@ -1,11 +1,11 @@ ace.define("ace/mode/klipper_config_highlight_rules",[], function(require, exports, module) { "use strict"; - + var oop = require("../lib/oop"); var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules; - + var KlipperConfigHighlightRules = function() { - + this.$rules = { start: [{ include: "#single_line_comment" @@ -60,7 +60,7 @@ ace.define("ace/mode/klipper_config_highlight_rules",[], function(require, expor regex: /^\[/, push: [{ token: "text", - regex: /\]\s*$/, + regex: /\]/, next: "pop" }, { include: "#known_config_block_name" @@ -238,29 +238,29 @@ ace.define("ace/mode/klipper_config_highlight_rules",[], function(require, expor }] }] } - + this.normalizeRules(); }; - + KlipperConfigHighlightRules.metaData = { "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json", name: "Klipper Config", scopeName: "source.klipper-config" } - - + + oop.inherits(KlipperConfigHighlightRules, TextHighlightRules); - + exports.KlipperConfigHighlightRules = KlipperConfigHighlightRules; }); - + ace.define("ace/mode/folding/cstyle",[], function(require, exports, module) { "use strict"; - + var oop = require("../../lib/oop"); var Range = require("../../range").Range; var BaseFoldMode = require("./fold_mode").FoldMode; - + var FoldMode = exports.FoldMode = function(commentRegex) { if (commentRegex) { this.foldingStartMarker = new RegExp( @@ -272,9 +272,9 @@ ace.define("ace/mode/klipper_config_highlight_rules",[], function(require, expor } }; oop.inherits(FoldMode, BaseFoldMode); - + (function() { - + this.foldingStartMarker = /([\{\[\(])[^\}\]\)]*$|^\s*(\/\*)/; this.foldingStopMarker = /^[^\[\{\(]*([\}\]\)])|^[\s\*]*(\*\/)/; this.singleLineBlockCommentRe= /^\s*(\/\*).*\*\/\s*$/; @@ -283,59 +283,59 @@ ace.define("ace/mode/klipper_config_highlight_rules",[], function(require, expor this._getFoldWidgetBase = this.getFoldWidget; this.getFoldWidget = function(session, foldStyle, row) { var line = session.getLine(row); - + if (this.singleLineBlockCommentRe.test(line)) { if (!this.startRegionRe.test(line) && !this.tripleStarBlockCommentRe.test(line)) return ""; } - + var fw = this._getFoldWidgetBase(session, foldStyle, row); - + if (!fw && this.startRegionRe.test(line)) return "start"; // lineCommentRegionStart - + return fw; }; - + this.getFoldWidgetRange = function(session, foldStyle, row, forceMultiline) { var line = session.getLine(row); - + if (this.startRegionRe.test(line)) return this.getCommentRegionBlock(session, line, row); - + var match = line.match(this.foldingStartMarker); if (match) { var i = match.index; - + if (match[1]) return this.openingBracketBlock(session, match[1], row, i); - + var range = session.getCommentFoldRange(row, i + match[0].length, 1); - + if (range && !range.isMultiLine()) { if (forceMultiline) { range = this.getSectionRange(session, row); } else if (foldStyle != "all") range = null; } - + return range; } - + if (foldStyle === "markbegin") return; - + var match = line.match(this.foldingStopMarker); if (match) { var i = match.index + match[0].length; - + if (match[1]) return this.closingBracketBlock(session, match[1], row, i); - + return session.getCommentFoldRange(row, i, -1); } }; - + this.getSectionRange = function(session, row) { var line = session.getLine(row); var startIndent = line.search(/\S/); @@ -352,7 +352,7 @@ ace.define("ace/mode/klipper_config_highlight_rules",[], function(require, expor if (startIndent > indent) break; var subRange = this.getFoldWidgetRange(session, "all", row); - + if (subRange) { if (subRange.start.row <= startRow) { break; @@ -364,14 +364,14 @@ ace.define("ace/mode/klipper_config_highlight_rules",[], function(require, expor } endRow = row; } - + return new Range(startRow, startColumn, endRow, session.getLine(endRow).length); }; this.getCommentRegionBlock = function(session, line, row) { var startColumn = line.search(/\s*$/); var maxRow = session.getLength(); var startRow = row; - + var re = /^\s*(?:\/\*|\/\/|--)#?(end)?region\b/; var depth = 1; while (++row < maxRow) { @@ -380,38 +380,38 @@ ace.define("ace/mode/klipper_config_highlight_rules",[], function(require, expor if (!m) continue; if (m[1]) depth--; else depth++; - + if (!depth) break; } - + var endRow = row; if (endRow > startRow) { return new Range(startRow, startColumn, endRow, line.length); } }; - + }).call(FoldMode.prototype); - + }); - + ace.define("ace/mode/klipper_config",[], function(require, exports, module) { "use strict"; - + var oop = require("../lib/oop"); var TextMode = require("./text").Mode; var KlipperConfigHighlightRules = require("./klipper_config_highlight_rules").KlipperConfigHighlightRules; var FoldMode = require("./folding/cstyle").FoldMode; - + var Mode = function() { this.HighlightRules = KlipperConfigHighlightRules; this.foldingRules = new FoldMode(); }; oop.inherits(Mode, TextMode); - + (function() { this.$id = "ace/mode/klipper_config" }).call(Mode.prototype); - + exports.Mode = Mode; }); (function() { ace.require(["ace/mode/klipper_config"], function(m) { diff --git a/octoprint_klipper/templates/klipper_settings.jinja2 b/octoprint_klipper/templates/klipper_settings.jinja2 index b3696ac..8b5f127 100644 --- a/octoprint_klipper/templates/klipper_settings.jinja2 +++ b/octoprint_klipper/templates/klipper_settings.jinja2 @@ -153,19 +153,19 @@