feat(configbackup): copy files to data

Change method of backup to just filecopy
This commit is contained in:
thelastWallE 2021-05-30 15:01:41 +02:00
parent 890fe8cdcc
commit 33eeb4dcc3
2 changed files with 47 additions and 42 deletions

2
.gitignore vendored
View File

@ -14,3 +14,5 @@ dist
thunder-tests thunder-tests
vscode.env vscode.env
.venv
OctoKlipper.egg-info

View File

@ -22,7 +22,7 @@ import glob
import os import os
import sys import sys
from octoprint.util.comm import parse_firmware_line from octoprint.util.comm import parse_firmware_line
from octoprint.access.permissions import Permissions, ADMIN_GROUP, USER_GROUP from octoprint.access.permissions import Permissions, ADMIN_GROUP
from .modules import KlipperLogAnalyzer from .modules import KlipperLogAnalyzer
import flask import flask
from flask_babel import gettext from flask_babel import gettext
@ -141,7 +141,7 @@ class KlipperPlugin(
configpath = os.path.expanduser( configpath = os.path.expanduser(
self._settings.get(["configuration", "configpath"]) self._settings.get(["configuration", "configpath"])
) )
data["config"] = ""
try: try:
f = open(configpath, "r") f = open(configpath, "r")
data["config"] = f.read() data["config"] = f.read()
@ -179,7 +179,10 @@ class KlipperPlugin(
configpath = os.path.expanduser( configpath = os.path.expanduser(
self._settings.get(["configuration", "configpath"]) self._settings.get(["configuration", "configpath"])
) )
if self.file_exist(configpath) and (self._parsing_check_response or not check_parse): if self.file_exist(configpath):
self.copy_cfg_to_backup(configpath)
if self._parsing_check_response or not check_parse:
try: try:
f = open(configpath, "w") f = open(configpath, "w")
f.write(data["config"]) f.write(data["config"])
@ -411,6 +414,7 @@ class KlipperPlugin(
listLogFiles=[], listLogFiles=[],
getStats=["logFile"], getStats=["logFile"],
reloadConfig=[], reloadConfig=[],
reloadCfgBackup=[],
checkConfig=["config"] checkConfig=["config"]
) )
@ -442,10 +446,13 @@ class KlipperPlugin(
return self.reload_cfg() return self.reload_cfg()
elif command == "reloadCfgBackup": elif command == "reloadCfgBackup":
self.log_debug("reloadCfgBackup") self.log_debug("reloadCfgBackup")
return self.read_cfg_backup() configpath = os.path.expanduser(
self._settings.get(["configuration", "configpath"])
)
return self.copy_cfg_from_backup(configpath)
elif command == "checkConfig": elif command == "checkConfig":
if "config" in data: if "config" in data:
self.write_cfg_backup(data["config"]) #self.write_cfg_backup(data["config"])
if self.key_exist(data, "configuration", "parse_check"): if self.key_exist(data, "configuration", "parse_check"):
check_parse = data["configuration"]["parse_check"] check_parse = data["configuration"]["parse_check"]
else: else:
@ -625,51 +632,47 @@ class KlipperPlugin(
data["config"] = data["config"].decode('utf-8') data["config"] = data["config"].decode('utf-8')
return flask.jsonify(data=data["config"]) return flask.jsonify(data=data["config"])
def read_cfg_backup(self): def copy_cfg_from_backup(self, dst):
""" """
Read the backuped printer.cfg in the data folder of OctoKlipper Copy the backuped config files in the data folder of OctoKlipper to the given destination
"""
from shutil import copy
:return: bak_config_path = os.path.join(self.get_plugin_data_folder(), "configs/")
The last version of the configfile printer_bak.cfg in the data folder as json src_files = os.listdir(bak_config_path)
""" nio_files = []
data = octoprint.plugin.SettingsPlugin.on_settings_load(self) self.log_debug("reloadCfgBackupPath:" + src_files)
self.oldconfig_path = os.path.join(self.get_plugin_data_folder(), "printer_bak.cfg")
self.log_debug("reloadCfgBackupPath:" + self.oldconfig_path) for file_name in src_files:
if os.path.exists(self.oldconfig_path): full_file_name = os.path.join(bak_config_path, file_name)
if os.path.isfile(full_file_name):
try: try:
f = open(self.oldconfig_path, "r") copy(full_file_name, dst)
data["config"] = f.read()
f.close()
except IOError: except IOError:
self.log_error( self.log_error(
"Error: Klipper config file not found at: {}".format( "Error: Klipper config file not found at: {}".format(
self.oldconfig_path) full_file_name)
) )
nio_files.append(full_file_name)
else: else:
self.log_debug("File done: " + full_file_name)
return nio_files
self._settings.set(["config"], data["config"]) def copy_cfg_to_backup(self, src):
# self.send_message("reload", "config", "", data["config"]) """
# send the configdata to frontend to update ace editor Copy the config file into the data folder of OctoKlipper
if sys.version_info[0] < 3: """
data["config"] = data["config"].decode('utf-8') from shutil import copyfile
self.log_debug("return CfgBackup now")
return flask.jsonify(data=data["config"])
def write_cfg_backup(self, data): filename = os.path.basename(src)
""" dst = os.path.join(self.get_plugin_data_folder(), "configs", "", filename)
Write the backuped printer.cfg into the data folder of OctoKlipper as printer_bak.cfg self.log_debug("CopyCfgBackupPath:" + dst)
"""
data = octoprint.plugin.SettingsPlugin.on_settings_load(self)
self.oldconfig_path = os.path.join(self.get_plugin_data_folder(), "printer_bak.cfg")
self.log_debug("WriteCfgBackupPath:" + self.oldconfig_path)
try: try:
f = open(self.oldconfig_path, "w") copyfile(src, dst)
f.write(data["config"])
f.close()
except IOError: except IOError:
self.log_error( self.log_error(
"Error: Couldn't write Klipper config file to {}".format( "Error: Couldn't copy Klipper config file to {}".format(
self.oldconfig_path) dst)
) )
else: else:
self.log_debug("CfgBackup writen") self.log_debug("CfgBackup writen")