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

4
.gitignore vendored
View File

@ -13,4 +13,6 @@ dist
.vscode/** .vscode/**
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)
if os.path.exists(self.oldconfig_path):
try:
f = open(self.oldconfig_path, "r")
data["config"] = f.read()
f.close()
except IOError:
self.log_error(
"Error: Klipper config file not found at: {}".format(
self.oldconfig_path)
)
else:
self._settings.set(["config"], data["config"]) for file_name in src_files:
# self.send_message("reload", "config", "", data["config"]) full_file_name = os.path.join(bak_config_path, file_name)
# send the configdata to frontend to update ace editor if os.path.isfile(full_file_name):
if sys.version_info[0] < 3: try:
data["config"] = data["config"].decode('utf-8') copy(full_file_name, dst)
self.log_debug("return CfgBackup now") except IOError:
return flask.jsonify(data=data["config"]) self.log_error(
"Error: Klipper config file not found at: {}".format(
full_file_name)
)
nio_files.append(full_file_name)
else:
self.log_debug("File done: " + full_file_name)
return nio_files
def write_cfg_backup(self, data): def copy_cfg_to_backup(self, src):
""" """
Write the backuped printer.cfg into the data folder of OctoKlipper as printer_bak.cfg Copy the config file into the data folder of OctoKlipper
""" """
data = octoprint.plugin.SettingsPlugin.on_settings_load(self) from shutil import copyfile
self.oldconfig_path = os.path.join(self.get_plugin_data_folder(), "printer_bak.cfg")
self.log_debug("WriteCfgBackupPath:" + self.oldconfig_path) filename = os.path.basename(src)
dst = os.path.join(self.get_plugin_data_folder(), "configs", "", filename)
self.log_debug("CopyCfgBackupPath:" + dst)
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")