🦄 refactor(py): refactor py
This commit is contained in:
parent
b2575c55db
commit
5dfbcd803f
|
@ -208,64 +208,66 @@ class KlipperPlugin(
|
||||||
def get_settings_version(self):
|
def get_settings_version(self):
|
||||||
return 3
|
return 3
|
||||||
|
|
||||||
|
|
||||||
|
#migrate Settings
|
||||||
def on_settings_migrate(self, target, current):
|
def on_settings_migrate(self, target, current):
|
||||||
if current is None:
|
|
||||||
settings = self._settings
|
settings = self._settings
|
||||||
|
if current is None:
|
||||||
if settings.has(["serialport"]):
|
self.migrate_old_settings(settings)
|
||||||
settings.set(["connection", "port"],
|
if current is not None and current < 3:
|
||||||
settings.get(["serialport"]))
|
self.migrate_configuration(
|
||||||
settings.remove(["serialport"])
|
settings,
|
||||||
|
"shortStatus_navbar",
|
||||||
if settings.has(["replace_connection_panel"]):
|
"navbar",
|
||||||
settings.set(
|
|
||||||
["connection", "replace_connection_panel"],
|
|
||||||
settings.get(["replace_connection_panel"])
|
|
||||||
)
|
)
|
||||||
settings.remove(["replace_connection_panel"])
|
|
||||||
|
|
||||||
if settings.has(["probeHeight"]):
|
if current is not None and current < 4:
|
||||||
settings.set(["probe", "height"],
|
self.migrate_configuration(
|
||||||
settings.get(["probeHeight"]))
|
settings,
|
||||||
settings.remove(["probeHeight"])
|
"old_config",
|
||||||
|
"temp_config",
|
||||||
|
)
|
||||||
|
|
||||||
if settings.has(["probeLift"]):
|
setting_path = settings.get(["configuration", "configpath"])
|
||||||
settings.set(["probe", "lift"], settings.get(["probeLift"]))
|
if setting_path.find("printer.cfg")!=-1:
|
||||||
settings.remove(["probeLift"])
|
new_setting_path = setting_path.replace("printer.cfg","")
|
||||||
|
logger.log_info(self, "migrate setting for 'configuration/configpath': " + setting_path + " -> " + new_setting_path)
|
||||||
|
settings.set(["configuration", "configpath"], new_setting_path)
|
||||||
|
|
||||||
if settings.has(["probeSpeedXy"]):
|
def migrate_old_settings(self, settings):
|
||||||
settings.set(["probe", "speed_xy"],
|
|
||||||
settings.get(["probeSpeedXy"]))
|
|
||||||
settings.remove(["probeSpeedXy"])
|
|
||||||
|
|
||||||
if settings.has(["probeSpeedZ"]):
|
self.migrate_settings(settings, "serialport", "connection", "port")
|
||||||
settings.set(["probe", "speed_z"],
|
self.migrate_settings(settings, "replace_connection_panel", "connection", "replace_connection_panel")
|
||||||
settings.get(["probeSpeedZ"]))
|
self.migrate_settings(settings, "probeHeight", "probe", "height")
|
||||||
settings.remove(["probeSpeedZ"])
|
self.migrate_settings(settings, "probeLift", "probe", "lift")
|
||||||
|
self.migrate_settings(settings, "probeSpeedXy", "probe", "speed_xy")
|
||||||
|
self.migrate_settings(settings, "probeSpeedZ", "probe", "speed_z")
|
||||||
|
self.migrate_settings(settings, "configPath", "configpath")
|
||||||
|
|
||||||
if settings.has(["probePoints"]):
|
if settings.has(["probePoints"]):
|
||||||
points = settings.get(["probePoints"])
|
points = settings.get(["probePoints"])
|
||||||
points_new = []
|
points_new = [dict(name="", x=int(p["x"]), y=int(p["y"]), z=0) for p in points]
|
||||||
for p in points:
|
|
||||||
points_new.append(
|
|
||||||
dict(name="", x=int(p["x"]), y=int(p["y"]), z=0))
|
|
||||||
settings.set(["probe", "points"], points_new)
|
settings.set(["probe", "points"], points_new)
|
||||||
settings.remove(["probePoints"])
|
settings.remove(["probePoints"])
|
||||||
|
|
||||||
if settings.has(["configPath"]):
|
def migrate_settings(self, settings, old, new, new2="") -> None:
|
||||||
logger.log_info(self, "migrate setting for: configPath")
|
if settings.has([old]):
|
||||||
settings.set(["config_path"], settings.get(["configPath"]))
|
if new2 != "":
|
||||||
settings.remove(["configPath"])
|
logger.log_info(self, "migrate setting for '" + old + "' -> '" + new + "/" + new2 + "'")
|
||||||
|
settings.set([new, new2], settings.get([old]))
|
||||||
|
else:
|
||||||
|
logger.log_info(self, "migrate setting for '" + old + "' -> '" + new + "'")
|
||||||
|
settings.set([new], settings.get([old]))
|
||||||
|
settings.remove([old])
|
||||||
|
|
||||||
|
def migrate_settings_configuration(self, settings, new, old):
|
||||||
|
if settings.has(["configuration", old]):
|
||||||
|
logger.log_info(self, "migrate setting for 'configuration/" + old + "' -> 'configuration/" + new + "'")
|
||||||
|
settings.set(["configuration", new], settings.get(["configuration", old]))
|
||||||
|
settings.remove(["configuration", old])
|
||||||
|
|
||||||
if current is not None and current < 3:
|
|
||||||
settings = self._settings
|
|
||||||
if settings.has(["configuration", "navbar"]):
|
|
||||||
logger.log_info(self, "migrate setting for: configuration/navbar")
|
|
||||||
settings.set(["configuration", "shortStatus_navbar"], settings.get(["configuration", "navbar"]))
|
|
||||||
settings.remove(["configuration", "navbar"])
|
|
||||||
|
|
||||||
# -- Template Plugin
|
# -- Template Plugin
|
||||||
|
|
||||||
def get_template_configs(self):
|
def get_template_configs(self):
|
||||||
return [
|
return [
|
||||||
dict(type="navbar", custom_bindings=True),
|
dict(type="navbar", custom_bindings=True),
|
||||||
|
@ -354,18 +356,18 @@ class KlipperPlugin(
|
||||||
# -- Event Handler Plugin
|
# -- Event Handler Plugin
|
||||||
|
|
||||||
def on_event(self, event, payload):
|
def on_event(self, event, payload):
|
||||||
if "UserLoggedIn" == event:
|
if event == "UserLoggedIn":
|
||||||
util.update_status(self, "info", "Klipper: Standby")
|
util.update_status(self, "info", "Klipper: Standby")
|
||||||
if "Connecting" == event:
|
if event == "Connecting":
|
||||||
util.update_status(self, "info", "Klipper: Connecting ...")
|
util.update_status(self, "info", "Klipper: Connecting ...")
|
||||||
elif "Connected" == event:
|
elif event == "Connected":
|
||||||
util.update_status(self, "info", "Klipper: Connected to host")
|
util.update_status(self, "info", "Klipper: Connected to host")
|
||||||
logger.log_info(
|
logger.log_info(
|
||||||
self,
|
self,
|
||||||
"Connected to host via {} @{}bps".format(payload["port"], payload["baudrate"]))
|
"Connected to host via {} @{}bps".format(payload["port"], payload["baudrate"]))
|
||||||
elif "Disconnected" == event:
|
elif event == "Disconnected":
|
||||||
util.update_status(self, "info", "Klipper: Disconnected from host")
|
util.update_status(self, "info", "Klipper: Disconnected from host")
|
||||||
elif "Error" == event:
|
elif event == "Error":
|
||||||
util.update_status(self, "error", "Klipper: Error")
|
util.update_status(self, "error", "Klipper: Error")
|
||||||
logger.log_error(self, payload["error"])
|
logger.log_error(self, payload["error"])
|
||||||
|
|
||||||
|
@ -407,9 +409,7 @@ class KlipperPlugin(
|
||||||
def get_api_commands(self):
|
def get_api_commands(self):
|
||||||
return dict(
|
return dict(
|
||||||
listLogFiles=[],
|
listLogFiles=[],
|
||||||
getStats=["logFile"],
|
getStats=["logFile"]
|
||||||
reloadConfig=[],
|
|
||||||
checkConfig=["config"]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def on_api_command(self, command, data):
|
def on_api_command(self, command, data):
|
||||||
|
@ -428,8 +428,6 @@ class KlipperPlugin(
|
||||||
size=filesize
|
size=filesize
|
||||||
))
|
))
|
||||||
return flask.jsonify(data=files)
|
return flask.jsonify(data=files)
|
||||||
else:
|
|
||||||
return flask.jsonify(data=files)
|
|
||||||
elif command == "getStats":
|
elif command == "getStats":
|
||||||
if "logFile" in data:
|
if "logFile" in data:
|
||||||
log_analyzer = KlipperLogAnalyzer.KlipperLogAnalyzer(
|
log_analyzer = KlipperLogAnalyzer.KlipperLogAnalyzer(
|
||||||
|
@ -458,7 +456,7 @@ class KlipperPlugin(
|
||||||
status_code=404)))
|
status_code=404)))
|
||||||
]
|
]
|
||||||
|
|
||||||
# API for Backups
|
# API for Backups
|
||||||
# Get Content of a Backupconfig
|
# Get Content of a Backupconfig
|
||||||
@octoprint.plugin.BlueprintPlugin.route("/backup/<filename>", methods=["GET"])
|
@octoprint.plugin.BlueprintPlugin.route("/backup/<filename>", methods=["GET"])
|
||||||
@restricted_access
|
@restricted_access
|
||||||
|
@ -508,7 +506,7 @@ class KlipperPlugin(
|
||||||
backupfile = os.path.realpath(os.path.join(data_folder, "configs", filename))
|
backupfile = os.path.realpath(os.path.join(data_folder, "configs", filename))
|
||||||
return flask.jsonify(restored = cfgUtils.copy_cfg(self, backupfile, configpath))
|
return flask.jsonify(restored = cfgUtils.copy_cfg(self, backupfile, configpath))
|
||||||
|
|
||||||
# API for Configs
|
# API for Configs
|
||||||
# Get Content of a Configfile
|
# Get Content of a Configfile
|
||||||
@octoprint.plugin.BlueprintPlugin.route("/config/<filename>", methods=["GET"])
|
@octoprint.plugin.BlueprintPlugin.route("/config/<filename>", methods=["GET"])
|
||||||
@restricted_access
|
@restricted_access
|
||||||
|
@ -566,20 +564,19 @@ class KlipperPlugin(
|
||||||
@Permissions.PLUGIN_KLIPPER_CONFIG.require(403)
|
@Permissions.PLUGIN_KLIPPER_CONFIG.require(403)
|
||||||
def save_config(self):
|
def save_config(self):
|
||||||
data = flask.request.json
|
data = flask.request.json
|
||||||
|
|
||||||
filename = data.get("filename", [])
|
filename = data.get("filename", [])
|
||||||
Filecontent = data.get("DataToSave", [])
|
|
||||||
if filename == []:
|
if filename == []:
|
||||||
flask.abort(
|
flask.abort(
|
||||||
400,
|
400,
|
||||||
description="Invalid request, the filename is not set",
|
description="Invalid request, the filename is not set",
|
||||||
)
|
)
|
||||||
|
Filecontent = data.get("DataToSave", [])
|
||||||
saved = cfgUtils.save_cfg(self, Filecontent, filename)
|
saved = cfgUtils.save_cfg(self, Filecontent, filename)
|
||||||
if saved == True:
|
if saved == True:
|
||||||
util.send_message(self, "reload", "configlist", "", "")
|
util.send_message(self, "reload", "configlist", "", "")
|
||||||
return flask.jsonify(saved = saved)
|
return flask.jsonify(saved = saved)
|
||||||
|
# APIs end
|
||||||
|
|
||||||
# APIs end
|
|
||||||
|
|
||||||
def get_update_information(self):
|
def get_update_information(self):
|
||||||
return dict(
|
return dict(
|
||||||
|
|
|
@ -1,11 +1,19 @@
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
import glob
|
import glob
|
||||||
import os, time, sys
|
import os, time, sys
|
||||||
|
import flask
|
||||||
|
|
||||||
from . import util, logger
|
from . import util, logger
|
||||||
from octoprint.util import is_hidden_path
|
|
||||||
import flask
|
|
||||||
from flask_babel import gettext
|
from flask_babel import gettext
|
||||||
|
from shutil import copy, copyfile
|
||||||
|
|
||||||
|
try:
|
||||||
|
import configparser
|
||||||
|
except ImportError:
|
||||||
|
import ConfigParser as configparser
|
||||||
|
|
||||||
|
if sys.version_info[0] < 3:
|
||||||
|
import StringIO
|
||||||
|
|
||||||
def list_cfg_files(self, path: str) -> list:
|
def list_cfg_files(self, path: str) -> list:
|
||||||
"""Generate list of config files.
|
"""Generate list of config files.
|
||||||
|
@ -28,7 +36,6 @@ def list_cfg_files(self, path: str) -> list:
|
||||||
cfg_files = glob.glob(cfg_path)
|
cfg_files = glob.glob(cfg_path)
|
||||||
logger.log_debug(self, "list_cfg_files Path: " + cfg_path)
|
logger.log_debug(self, "list_cfg_files Path: " + cfg_path)
|
||||||
|
|
||||||
f_counter = 1
|
|
||||||
for f in cfg_files:
|
for f in cfg_files:
|
||||||
filesize = os.path.getsize(f)
|
filesize = os.path.getsize(f)
|
||||||
filemdate = time.localtime(os.path.getmtime(f))
|
filemdate = time.localtime(os.path.getmtime(f))
|
||||||
|
@ -41,8 +48,7 @@ def list_cfg_files(self, path: str) -> list:
|
||||||
+ "plugin/klipper/download/"
|
+ "plugin/klipper/download/"
|
||||||
+ os.path.basename(f),
|
+ os.path.basename(f),
|
||||||
))
|
))
|
||||||
logger.log_debug(self, "list_cfg_files " + str(f_counter) + ": " + f)
|
logger.log_debug(self, "list_cfg_files " + str(len(files)) + ": " + f)
|
||||||
f_counter += 1
|
|
||||||
return files
|
return files
|
||||||
|
|
||||||
def get_cfg(self, file):
|
def get_cfg(self, file):
|
||||||
|
@ -142,12 +148,6 @@ def check_cfg(self, data):
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if the data is valid. False if it is not.
|
bool: True if the data is valid. False if it is not.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
|
||||||
import configparser
|
|
||||||
except ImportError:
|
|
||||||
import ConfigParser as configparser
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
dataToValidated = configparser.RawConfigParser(strict=False)
|
dataToValidated = configparser.RawConfigParser(strict=False)
|
||||||
if sys.version_info[0] < 3:
|
if sys.version_info[0] < 3:
|
||||||
|
@ -156,45 +156,15 @@ def check_cfg(self, data):
|
||||||
dataToValidated.readfp(buf)
|
dataToValidated.readfp(buf)
|
||||||
else:
|
else:
|
||||||
dataToValidated.read_string(data)
|
dataToValidated.read_string(data)
|
||||||
|
is_float_ok(self, dataToValidated)
|
||||||
sections_search_list = ["bltouch",
|
|
||||||
"probe"]
|
|
||||||
value_search_list = [ "x_offset",
|
|
||||||
"y_offset",
|
|
||||||
"z_offset"]
|
|
||||||
try:
|
|
||||||
# cycle through sections and then values
|
|
||||||
for y in sections_search_list:
|
|
||||||
for x in value_search_list:
|
|
||||||
if dataToValidated.has_option(y, x):
|
|
||||||
a_float = dataToValidated.getfloat(y, x)
|
|
||||||
if a_float:
|
|
||||||
pass
|
|
||||||
except ValueError as error:
|
|
||||||
logger.log_error(
|
|
||||||
self,
|
|
||||||
"Error: Invalid Value for <b>"+x+"</b> in Section: <b>"+y+"</b>\n"
|
|
||||||
+ "{}".format(str(error))
|
|
||||||
)
|
|
||||||
util.send_message(
|
|
||||||
self,
|
|
||||||
"PopUp",
|
|
||||||
"warning",
|
|
||||||
"OctoKlipper: Invalid Config\n",
|
|
||||||
"Config got not saved!\n\n"
|
|
||||||
+ "Invalid Value for <b>"+x+"</b> in Section: <b>"+y+"</b>\n"
|
|
||||||
+ "{}".format(str(error))
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
except configparser.Error as error:
|
except configparser.Error as error:
|
||||||
if sys.version_info[0] < 3:
|
|
||||||
error.message = error.message.replace("\\n","")
|
error.message = error.message.replace("\\n","")
|
||||||
|
if sys.version_info[0] < 3:
|
||||||
error.message = error.message.replace("file: u","Klipper Configuration", 1)
|
error.message = error.message.replace("file: u","Klipper Configuration", 1)
|
||||||
error.message = error.message.replace("'","", 2)
|
error.message = error.message.replace("'","", 2)
|
||||||
error.message = error.message.replace("u'","'", 1)
|
error.message = error.message.replace("u'","'", 1)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
error.message = error.message.replace("\\n","")
|
|
||||||
error.message = error.message.replace("file:","Klipper Configuration", 1)
|
error.message = error.message.replace("file:","Klipper Configuration", 1)
|
||||||
error.message = error.message.replace("'","", 2)
|
error.message = error.message.replace("'","", 2)
|
||||||
logger.log_error(
|
logger.log_error(
|
||||||
|
@ -203,13 +173,45 @@ def check_cfg(self, data):
|
||||||
+ "{}".format(str(error))
|
+ "{}".format(str(error))
|
||||||
)
|
)
|
||||||
util.send_message(self, "PopUp", "warning", "OctoKlipper: Invalid Config data\n",
|
util.send_message(self, "PopUp", "warning", "OctoKlipper: Invalid Config data\n",
|
||||||
"Config got not saved!\n\n"
|
"\n"
|
||||||
+ str(error))
|
+ str(error))
|
||||||
|
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def is_float_ok(self, dataToValidated):
|
||||||
|
|
||||||
|
sections_search_list = ["bltouch",
|
||||||
|
"probe"]
|
||||||
|
value_search_list = ["x_offset",
|
||||||
|
"y_offset",
|
||||||
|
"z_offset"]
|
||||||
|
try:
|
||||||
|
# cycle through sections and then values
|
||||||
|
for y in sections_search_list:
|
||||||
|
for x in value_search_list:
|
||||||
|
if dataToValidated.has_option(y, x):
|
||||||
|
a_float = dataToValidated.getfloat(y, x)
|
||||||
|
except ValueError as error:
|
||||||
|
logger.log_error(
|
||||||
|
self,
|
||||||
|
"Error: Invalid Value for <b>" + x + "</b> in Section: <b>" + y + "</b>\n"
|
||||||
|
+ "{}".format(str(error))
|
||||||
|
)
|
||||||
|
util.send_message(
|
||||||
|
self,
|
||||||
|
"PopUp",
|
||||||
|
"warning",
|
||||||
|
"OctoKlipper: Invalid Config data\n",
|
||||||
|
"\n"
|
||||||
|
+ "Invalid Value for <b>" + x + "</b> in Section: <b>" + y + "</b>\n"
|
||||||
|
+ "{}".format(str(error))
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
return True
|
||||||
|
|
||||||
def copy_cfg(self, file, dst):
|
def copy_cfg(self, file, dst):
|
||||||
"""Copy the config file to the destination.
|
"""Copy the config file to the destination.
|
||||||
|
|
||||||
|
@ -220,7 +222,6 @@ def copy_cfg(self, file, dst):
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if the copy succeeded, False otherwise.
|
bool: True if the copy succeeded, False otherwise.
|
||||||
"""
|
"""
|
||||||
from shutil import copy
|
|
||||||
|
|
||||||
if os.path.isfile(file):
|
if os.path.isfile(file):
|
||||||
try:
|
try:
|
||||||
|
@ -249,9 +250,10 @@ def copy_cfg_to_backup(self, src):
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if the config file was copied successfully. False otherwise.
|
bool: True if the config file was copied successfully. False otherwise.
|
||||||
"""
|
"""
|
||||||
from shutil import copyfile
|
|
||||||
|
|
||||||
if os.path.isfile(src):
|
if not os.path.isfile(src):
|
||||||
|
return False
|
||||||
|
|
||||||
cfg_path = os.path.join(self.get_plugin_data_folder(), "configs", "")
|
cfg_path = os.path.join(self.get_plugin_data_folder(), "configs", "")
|
||||||
filename = os.path.basename(src)
|
filename = os.path.basename(src)
|
||||||
if not os.path.exists(cfg_path):
|
if not os.path.exists(cfg_path):
|
||||||
|
@ -265,7 +267,8 @@ def copy_cfg_to_backup(self, src):
|
||||||
|
|
||||||
dst = os.path.join(cfg_path, filename)
|
dst = os.path.join(cfg_path, filename)
|
||||||
logger.log_debug(self, "copy_cfg_to_backup:" + src + " to " + dst)
|
logger.log_debug(self, "copy_cfg_to_backup:" + src + " to " + dst)
|
||||||
if not src == dst:
|
if src == dst:
|
||||||
|
return False
|
||||||
try:
|
try:
|
||||||
copyfile(src, dst)
|
copyfile(src, dst)
|
||||||
except IOError:
|
except IOError:
|
||||||
|
@ -277,9 +280,6 @@ def copy_cfg_to_backup(self, src):
|
||||||
else:
|
else:
|
||||||
logger.log_debug(self, "CfgBackup " + dst + " writen")
|
logger.log_debug(self, "CfgBackup " + dst + " writen")
|
||||||
return True
|
return True
|
||||||
else:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue