[fix] Improve config file path handling
This commit is contained in:
parent
129d5bb6fd
commit
665654ec75
|
@ -8,6 +8,7 @@ import glob
|
|||
import os
|
||||
from octoprint.util.comm import parse_firmware_line
|
||||
from .modules import KlipperLogAnalyzer
|
||||
from .modules.klipper.parser import Parser
|
||||
import flask
|
||||
|
||||
class KlipperPlugin(
|
||||
|
@ -59,45 +60,46 @@ class KlipperPlugin(
|
|||
)]
|
||||
),
|
||||
configuration = dict(
|
||||
path="/home/pi/printer.cfg",
|
||||
path="~/printer.cfg",
|
||||
reload_command="RESTART"
|
||||
)
|
||||
)
|
||||
|
||||
def on_settings_load(self):
|
||||
data = octoprint.plugin.SettingsPlugin.on_settings_load(self)
|
||||
f = open(self._settings.get(["configuration", "path"]), "r")
|
||||
if f:
|
||||
|
||||
filepath = os.path.expanduser(
|
||||
self._settings.get(["configuration", "path"])
|
||||
)
|
||||
try:
|
||||
f = open(filepath, "r")
|
||||
data["config"] = f.read()
|
||||
f.close()
|
||||
else:
|
||||
self._logger.info(
|
||||
"Error: Klipper config file not found at: {}".format(
|
||||
self._settings.get(["configuration", "path"])
|
||||
)
|
||||
except IOError:
|
||||
self._logger.error(
|
||||
"Error: Klipper config file not found at: {}".format(filepath)
|
||||
)
|
||||
return data
|
||||
|
||||
def on_settings_save(self, data):
|
||||
if "config" in data:
|
||||
f = open(self._settings.get(["configuration", "path"]), "w")
|
||||
if f:
|
||||
try:
|
||||
filepath = os.path.expanduser(
|
||||
self._settings.get(["configuration", "path"])
|
||||
)
|
||||
f = open(filepath)
|
||||
f.write(data["config"])
|
||||
f.close()
|
||||
self._logger.info(
|
||||
"Write Klipper config to {}".format(
|
||||
self._settings.get(["configuration", "path"])
|
||||
)
|
||||
"Write Klipper config to {}".format(filepath)
|
||||
)
|
||||
# Restart klipply to reload config
|
||||
self._printer.commands(self._settings.get(["configuration", "reload_command"]))
|
||||
self.logInfo("Reloading Klipper Configuration.")
|
||||
else:
|
||||
self._logger.info(
|
||||
"Error: Couldn't write Klipper config file: {}".format(
|
||||
self._settings.get(["configuration", "path"])
|
||||
)
|
||||
)
|
||||
except IOError:
|
||||
self._logger.error(
|
||||
"Error: Couldn't write Klipper config file: {}".format(filepath)
|
||||
)
|
||||
data.pop("config", None) # we dont want to write the klipper conf to the octoprint settings
|
||||
else:
|
||||
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
|
||||
|
@ -260,7 +262,8 @@ class KlipperPlugin(
|
|||
def get_api_commands(self):
|
||||
return dict(
|
||||
listLogFiles=[],
|
||||
getStats=["logFile"]
|
||||
getStats=["logFile"],
|
||||
loadConfig=["configFile"]
|
||||
)
|
||||
|
||||
def on_api_command(self, command, data):
|
||||
|
@ -278,7 +281,11 @@ class KlipperPlugin(
|
|||
if "logFile" in data:
|
||||
log_analyzer = KlipperLogAnalyzer.KlipperLogAnalyzer(data["logFile"])
|
||||
return flask.jsonify(log_analyzer.analyze())
|
||||
|
||||
elif command == "loadConfig":
|
||||
kc = Parser()
|
||||
sections = kc.load(data["configFile"])
|
||||
return flask.jsonify(sections)
|
||||
|
||||
def on_api_get(self, request):
|
||||
log_analyzer = KlipperLogAnalyzer.KlipperLogAnalyzer("/tmp/klippy.log.2018-08-06")
|
||||
|
||||
|
@ -320,8 +327,6 @@ class KlipperPlugin(
|
|||
def logError(self, error):
|
||||
self.sendMessage("log", "error", error)
|
||||
|
||||
|
||||
|
||||
__plugin_name__ = "Klipper"
|
||||
|
||||
def __plugin_load__():
|
||||
|
|
|
@ -30,33 +30,36 @@ class KlipperLogAnalyzer():
|
|||
mcu = "mcu"
|
||||
mcu_prefix = mcu + ":"
|
||||
apply_prefix = { p: 1 for p in self.APPLY_PREFIX }
|
||||
|
||||
f = open(logname, 'rb')
|
||||
out = []
|
||||
|
||||
for line in f:
|
||||
parts = line.split()
|
||||
if not parts or parts[0] not in ('Stats', 'INFO:root:Stats'):
|
||||
#if parts and parts[0] == 'INFO:root:shutdown:':
|
||||
# break
|
||||
continue
|
||||
prefix = ""
|
||||
keyparts = {}
|
||||
for p in parts[2:]:
|
||||
if '=' not in p:
|
||||
prefix = p
|
||||
if prefix == mcu_prefix:
|
||||
prefix = ''
|
||||
try:
|
||||
f = open(logname, 'rb')
|
||||
|
||||
for line in f:
|
||||
parts = line.split()
|
||||
if not parts or parts[0] not in ('Stats', 'INFO:root:Stats'):
|
||||
#if parts and parts[0] == 'INFO:root:shutdown:':
|
||||
# break
|
||||
continue
|
||||
name, val = p.split('=', 1)
|
||||
if name in apply_prefix:
|
||||
name = prefix + name
|
||||
keyparts[name] = val
|
||||
if keyparts.get('bytes_write', '0') == '0':
|
||||
continue
|
||||
keyparts['#sampletime'] = float(parts[1][:-1])
|
||||
out.append(keyparts)
|
||||
f.close()
|
||||
prefix = ""
|
||||
keyparts = {}
|
||||
for p in parts[2:]:
|
||||
if '=' not in p:
|
||||
prefix = p
|
||||
if prefix == mcu_prefix:
|
||||
prefix = ''
|
||||
continue
|
||||
name, val = p.split('=', 1)
|
||||
if name in apply_prefix:
|
||||
name = prefix + name
|
||||
keyparts[name] = val
|
||||
if keyparts.get('bytes_write', '0') == '0':
|
||||
continue
|
||||
keyparts['#sampletime'] = float(parts[1][:-1])
|
||||
out.append(keyparts)
|
||||
f.close()
|
||||
except IOError:
|
||||
print("Couldn't open log file")
|
||||
return out
|
||||
|
||||
def find_print_restarts(self, data):
|
||||
|
|
Loading…
Reference in New Issue