♻️ refactor(setting): migration baseconfig

-refactor the migration for the baseconfig
- remove old_config setting
- change name for self.ConfigChangedAfterSave_Config()
This commit is contained in:
thelastWallE 2021-10-31 11:29:23 +01:00
parent c9b2d3a49b
commit ef05c3e271
3 changed files with 16 additions and 19 deletions

View File

@ -132,7 +132,6 @@ class KlipperPlugin(
debug_logging=False, debug_logging=False,
configpath="~/", configpath="~/",
baseconfig="printer.cfg", baseconfig="printer.cfg",
old_config="",
logpath="/tmp/klippy.log", logpath="/tmp/klippy.log",
reload_command="RESTART", reload_command="RESTART",
restart_onsave=True, restart_onsave=True,
@ -242,16 +241,12 @@ class KlipperPlugin(
def migrate_settings_4(self, settings): def migrate_settings_4(self, settings):
cfg_path = settings.get(["configuration", "configpath"]) cfg_path = settings.get(["configuration", "configpath"])
if cfg_path.find("printer.cfg") != -1:
new_cfg_path = cfg_path.replace("printer.cfg","") new_cfg_path, baseconfig = os.path.split(cfg_path)
logger.log_info(self, "migrate setting for 'configuration/configpath': " + cfg_path + " -> " + new_cfg_path) logger.log_info(self, "migrate setting for 'configuration/configpath': " + cfg_path + " -> " + new_cfg_path)
settings.set(["configuration", "configpath"], new_cfg_path) logger.log_info(self, "migrate setting for 'configuration/baseconfig': printer.cfg -> " + baseconfig)
else: settings.set(["configuration", "configpath"], new_cfg_path)
new_cfg_path, baseconfig = os.path.split(cfg_path) settings.set(["configuration", "baseconfig"], baseconfig)
logger.log_info(self, "migrate setting for 'configuration/configpath': " + cfg_path + " -> " + new_cfg_path)
logger.log_info(self, "migrate setting for 'configuration/baseconfig': printer.cfg -> " + baseconfig)
settings.set(["configuration", "configpath"], new_cfg_path)
settings.set(["configuration", "baseconfig"], baseconfig)
if settings.get(["configuration", "reload_command"]) != "manually" : if settings.get(["configuration", "reload_command"]) != "manually" :
logger.log_info(self, "migrate setting for 'configuration/restart_onsave': False -> True") logger.log_info(self, "migrate setting for 'configuration/restart_onsave': False -> True")

View File

@ -122,19 +122,18 @@ def save_cfg(self, content, filename):
filename = self._settings.get(["configuration", "baseconfig"]) filename = self._settings.get(["configuration", "baseconfig"])
if filename[-4:] != ".cfg": if filename[-4:] != ".cfg":
filename += ".cfg" filename += ".cfg"
filepath = os.path.join(configpath, filename) filepath = os.path.join(configpath, filename)
logger.log_debug(self, "save filepath: {}".format(filepath)) logger.log_debug(self, "save filepath: {}".format(filepath))
self._settings.set(["configuration", "old_config"], content)
check_parse = self._settings.get(["configuration", "parse_check"]) check_parse = self._settings.get(["configuration", "parse_check"])
logger.log_debug(self, "check_parse on filesave: {}".format(check_parse)) logger.log_debug(self, "check_parse on filesave: {}".format(check_parse))
if check_parse and not check_cfg(self, content): if check_parse and not check_cfg(self, content):
return False return False
logger.log_debug(self, "Writing Klipper config to {}".format(filepath))
try: try:
logger.log_debug(self, "Writing Klipper config to {}".format(filepath))
with open(filepath, "w") as f: with open(filepath, "w") as f:
f.write(content) f.write(content)
except IOError: except IOError:

View File

@ -100,6 +100,7 @@ $(function () {
} }
}; };
//initialize the modal window and return done when finished
self.process = function (config) { self.process = function (config) {
return new Promise(function (resolve) { return new Promise(function (resolve) {
self.loadedConfig = config.content; self.loadedConfig = config.content;
@ -110,7 +111,6 @@ $(function () {
editor.session.setValue(self.CfgContent()); editor.session.setValue(self.CfgContent());
self.CfgChangedExtern = false; self.CfgChangedExtern = false;
editor.setFontSize(self.settings.settings.plugins.klipper.configuration.fontsize()); editor.setFontSize(self.settings.settings.plugins.klipper.configuration.fontsize());
self.settings.settings.plugins.klipper.configuration.old_config(config.content);
editor.clearSelection(); editor.clearSelection();
self.klipperViewModel.sleep(500).then( self.klipperViewModel.sleep(500).then(
function() { function() {
@ -123,13 +123,15 @@ $(function () {
} }
self.onDataUpdaterPluginMessage = function (plugin, data) { self.onDataUpdaterPluginMessage = function (plugin, data) {
//receive from backend after a SAVE_CONFIG
if (plugin == "klipper" && data.type == "reload" && data.subtype == "config") { if (plugin == "klipper" && data.type == "reload" && data.subtype == "config") {
self.klipperViewModel.consoleMessage("debug", "onDataUpdaterPluginMessage klipper reload baseconfig"); self.klipperViewModel.consoleMessage("debug", "onDataUpdaterPluginMessage klipper reload baseconfig");
self.ConfigChangedAfterSave(); self.ConfigChangedAfterSave_Config();
} }
}; };
self.ConfigChangedAfterSave = function () { //set externally changed config flag if the current file is the base config
self.ConfigChangedAfterSave_Config = function () {
if (!self.klipperViewModel.hasRight("CONFIG")) return; if (!self.klipperViewModel.hasRight("CONFIG")) return;
if (self.CfgFilename() == self.settings.settings.plugins.klipper.configuration.baseconfig()) { if (self.CfgFilename() == self.settings.settings.plugins.klipper.configuration.baseconfig()) {
@ -138,6 +140,7 @@ $(function () {
} }
}; };
//check if the config was externally changed and ask for a reload
self.checkExternChange = function() { self.checkExternChange = function() {
var baseconfig = self.settings.settings.plugins.klipper.configuration.baseconfig(); var baseconfig = self.settings.settings.plugins.klipper.configuration.baseconfig();
if (self.CfgChangedExtern && self.CfgFilename() == baseconfig) { if (self.CfgChangedExtern && self.CfgFilename() == baseconfig) {
@ -195,7 +198,7 @@ $(function () {
if (response.saved === true) { if (response.saved === true) {
self.klipperViewModel.showPopUp("success", gettext("Save Config"), gettext("File saved.")); self.klipperViewModel.showPopUp("success", gettext("Save Config"), gettext("File saved."));
self.loadedConfig = editor.session.getValue(); self.loadedConfig = editor.session.getValue(); //set loaded config to current for resetting dirtyEditor
if (closing) { if (closing) {
editordialog.modal('hide'); editordialog.modal('hide');
} }