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