Implement Configpath and Logpath Validation

Added JS popup handler. Added configpath and logpath validation when settings are saved.
Add logpath validation


Change file checking method


Check new values instead of old settings on save
This commit is contained in:
Alice Grey 2020-09-07 11:37:01 -05:00
parent a2291b1a3d
commit e553a69ff5
2 changed files with 179 additions and 134 deletions

View File

@ -81,39 +81,53 @@ class KlipperPlugin(
def on_settings_load(self):
data = octoprint.plugin.SettingsPlugin.on_settings_load(self)
filepath = os.path.expanduser(
configpath = os.path.expanduser(
self._settings.get(["configuration", "configpath"])
)
logpath = os.path.expanduser(
self._settings.get(["configuration", "logpath"])
)
try:
f = open(filepath, "r")
f = open(configpath, "r")
data["config"] = f.read()
f.close()
except IOError:
self._logger.error(
"Error: Klipper config file not found at: {}".format(filepath)
"Error: Klipper config file not found at: {}".format(configpath)
)
return data
def on_settings_save(self, data):
if self.keyexists(data, "configuration", "configpath"):
configpath = os.path.expanduser(
data["configuration"]["configpath"]
)
self.checkFile("config",configpath)
if self.keyexists(data, "configuration", "logpath"):
logpath = os.path.expanduser(
data["configuration"]["logpath"]
)
self.checkFile("log",logpath)
if "config" in data:
try:
filepath = os.path.expanduser(
self._settings.get(["configuration", "configpath"])
)
data["config"] = data["config"].encode('utf-8')
f = open(filepath, "w")
f = open(configpath, "w")
f.write(data["config"])
f.close()
self._logger.info(
"Writing Klipper config to {}".format(filepath)
self._logger.error(
"Writing Klipper config to {}".format(configpath)
)
# Restart klipply to reload config
self._printer.commands(self._settings.get(["configuration", "reload_command"]))
self.logInfo("Reloading Klipper Configuration.")
except IOError:
self._logger.error(
"Error: Couldn't write Klipper config file: {}".format(filepath)
"Error: Couldn't write Klipper config file: {}".format(configpath)
)
data.pop("config", None) # we dont want to write the klipper conf to the octoprint settings
else:
@ -324,13 +338,14 @@ class KlipperPlugin(
)
#-- Helpers
def sendMessage(self, type, subtype, payload):
def sendMessage(self, type, subtype, title, payload):
self._plugin_manager.send_plugin_message(
self._identifier,
dict(
time=datetime.datetime.now().strftime("%H:%M:%S"),
type=type,
subtype=subtype,
title=title,
payload=payload
)
)
@ -339,13 +354,25 @@ class KlipperPlugin(
self._printer.commands("STATUS")
def updateStatus(self, type, status):
self.sendMessage("status", type, status)
self.sendMessage("status", type, status, status)
def logInfo(self, message):
self.sendMessage("log", "info", message)
self.sendMessage("log", "info", message, message)
def logError(self, error):
self.sendMessage("log", "error", error)
self.sendMessage("log", "error", error, error)
def checkFile(self,filetype,filepath):
if not os.path.isfile(filepath):
self.sendMessage("errorPopUp","warning", "OctoKlipper Settings", "Klipper " + filepath + " does not exist!")
def keyexists(self, dict, key1, key2):
try:
dict[key1][key2]
except KeyError:
return False
else:
return True
__plugin_name__ = "OctoKlipper"
__plugin_pythoncompat__ = ">=2.7,<4"

View File

@ -26,6 +26,16 @@ $(function() {
self.shortStatus = ko.observable();
self.logMessages = ko.observableArray();
self.showPopUp = function(popupType, popupTitle, message){
var title = popupType.toUpperCase() + ": " + popupTitle;
new PNotify({
title: title,
text: message,
type: popupType,
hide: false
});
};
self.showLevelingDialog = function() {
var dialog = $("#klipper_leveling_dialog");
dialog.modal({
@ -34,7 +44,7 @@ $(function() {
keyboard: false
});
self.levelingViewModel.initView();
}
};
self.showPidTuningDialog = function() {
var dialog = $("#klipper_pid_tuning_dialog");
@ -43,7 +53,7 @@ $(function() {
backdrop: 'static',
keyboard: false
});
}
};
self.showOffsetDialog = function() {
var dialog = $("#klipper_offset_dialog");
@ -51,7 +61,7 @@ $(function() {
show: 'true',
backdrop: 'static'
});
}
};
self.showGraphDialog = function() {
var dialog = $("#klipper_graph_dialog");
@ -60,7 +70,7 @@ $(function() {
minHeight: "500px",
maxHeight: "600px"
});
}
};
self.executeMacro = function(macro) {
var paramObjRegex = /{(.*?)}/g;
@ -80,11 +90,11 @@ $(function() {
backdrop: 'static'
});
}
}
};
self.onGetStatus = function() {
OctoPrint.control.sendGcode("Status")
}
};
self.onRestartFirmware = function() {
OctoPrint.control.sendGcode("FIRMWARE_RESTART")
@ -96,17 +106,25 @@ $(function() {
self.onAfterBinding = function() {
self.connectionState.selectedPort(self.settings.settings.plugins.klipper.connection.port());
}
};
self.onDataUpdaterPluginMessage = function(plugin, message) {
self.onDataUpdaterPluginMessage = function(plugin, data) {
if(plugin == "klipper") {
if(message["type"] == "status") {
self.shortStatus(message["payload"]);
if ("warningPopUp" == data.type){
self.showPopUp(data.subtype, data.title, data.payload);
return;
}
if ("errorPopUp" == data.type){
self.showPopUp(data.subtype, data.title, data.payload);
return;
}
if(data.type == "status") {
self.shortStatus(data.payload);
} else {
self.logMessage(message["time"], message["subtype"], message["payload"]);
}
self.logMessage(data.time, data.subtype, data.payload);
}
}
};
self.logMessage = function(timestamp, type, message) {
self.logMessages.push({
@ -114,7 +132,7 @@ $(function() {
type: type,
msg: message.replace(/\n/gi, "<br>")}
);
}
};
self.onClearLog = function() {
self.logMessages.removeAll();
@ -122,7 +140,7 @@ $(function() {
self.isActive = function() {
return self.connectionState.isOperational() && self.loginState.isUser();
}
};
}
OCTOPRINT_VIEWMODELS.push({