♻️ 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,
configpath="~/",
baseconfig="printer.cfg",
old_config="",
logpath="/tmp/klippy.log",
reload_command="RESTART",
restart_onsave=True,
@ -242,11 +241,7 @@ class KlipperPlugin(
def migrate_settings_4(self, settings):
cfg_path = settings.get(["configuration", "configpath"])
if cfg_path.find("printer.cfg") != -1:
new_cfg_path = cfg_path.replace("printer.cfg","")
logger.log_info(self, "migrate setting for 'configuration/configpath': " + cfg_path + " -> " + new_cfg_path)
settings.set(["configuration", "configpath"], new_cfg_path)
else:
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/baseconfig': printer.cfg -> " + baseconfig)

View File

@ -122,19 +122,18 @@ def save_cfg(self, content, filename):
filename = self._settings.get(["configuration", "baseconfig"])
if filename[-4:] != ".cfg":
filename += ".cfg"
filepath = os.path.join(configpath, filename)
logger.log_debug(self, "save filepath: {}".format(filepath))
self._settings.set(["configuration", "old_config"], content)
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
try:
logger.log_debug(self, "Writing Klipper config to {}".format(filepath))
try:
with open(filepath, "w") as f:
f.write(content)
except IOError:

View File

@ -100,6 +100,7 @@ $(function () {
}
};
//initialize the modal window and return done when finished
self.process = function (config) {
return new Promise(function (resolve) {
self.loadedConfig = config.content;
@ -110,7 +111,6 @@ $(function () {
editor.session.setValue(self.CfgContent());
self.CfgChangedExtern = false;
editor.setFontSize(self.settings.settings.plugins.klipper.configuration.fontsize());
self.settings.settings.plugins.klipper.configuration.old_config(config.content);
editor.clearSelection();
self.klipperViewModel.sleep(500).then(
function() {
@ -123,13 +123,15 @@ $(function () {
}
self.onDataUpdaterPluginMessage = function (plugin, data) {
//receive from backend after a SAVE_CONFIG
if (plugin == "klipper" && data.type == "reload" && data.subtype == "config") {
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.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() {
var baseconfig = self.settings.settings.plugins.klipper.configuration.baseconfig();
if (self.CfgChangedExtern && self.CfgFilename() == baseconfig) {
@ -195,7 +198,7 @@ $(function () {
if (response.saved === true) {
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) {
editordialog.modal('hide');
}