🐛 add encoding for ISO-8859-1
- will try to encode with ISO-8859-1 after utf-8 failed.
This commit is contained in:
parent
e458226580
commit
7e1bdcba1c
|
@ -21,6 +21,7 @@ import octoprint.plugin.core
|
||||||
import glob
|
import glob
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import io
|
||||||
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, USER_GROUP
|
||||||
from .modules import KlipperLogAnalyzer
|
from .modules import KlipperLogAnalyzer
|
||||||
|
@ -141,9 +142,8 @@ class KlipperPlugin(
|
||||||
configpath = os.path.expanduser(
|
configpath = os.path.expanduser(
|
||||||
self._settings.get(["configuration", "configpath"])
|
self._settings.get(["configuration", "configpath"])
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f = open(configpath, "r")
|
with io.open(configpath, "r", encoding="utf8") as f:
|
||||||
data["config"] = f.read()
|
data["config"] = f.read()
|
||||||
f.close()
|
f.close()
|
||||||
except IOError:
|
except IOError:
|
||||||
|
@ -151,6 +151,24 @@ class KlipperPlugin(
|
||||||
"Error: Klipper config file not found at: {}".format(
|
"Error: Klipper config file not found at: {}".format(
|
||||||
configpath)
|
configpath)
|
||||||
)
|
)
|
||||||
|
except UnicodeDecodeError as e:
|
||||||
|
self.log_debug(
|
||||||
|
"Loading config with utf-8 failed. Trying to load config file with ISO-8859-1 now."
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
with io.open(configpath, "r", encoding="ISO-8859-1") as f:
|
||||||
|
data["config"] = f.read()
|
||||||
|
f.close()
|
||||||
|
except UnicodeDecodeError as e:
|
||||||
|
self.log_error(
|
||||||
|
"Error: Klipper config file cannot be decoded: {}".format(e)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.log_debug(
|
||||||
|
"Loading config with ISO-8859-1 finished."
|
||||||
|
)
|
||||||
|
self.send_message("reload", "config", "", data["config"])
|
||||||
|
# send the configdata to frontend to update ace editor
|
||||||
else:
|
else:
|
||||||
self.send_message("reload", "config", "", data["config"])
|
self.send_message("reload", "config", "", data["config"])
|
||||||
# send the configdata to frontend to update ace editor
|
# send the configdata to frontend to update ace editor
|
||||||
|
@ -183,11 +201,9 @@ class KlipperPlugin(
|
||||||
)
|
)
|
||||||
if self.file_exist(configpath) and (self._parsing_check_response or not check_parse):
|
if self.file_exist(configpath) and (self._parsing_check_response or not check_parse):
|
||||||
try:
|
try:
|
||||||
f = open(configpath, "w")
|
with io.open(configpath, "w", encoding="utf-8") as f:
|
||||||
f.write(data["config"])
|
f.write(data["config"])
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
|
|
||||||
self.log_debug("Writing Klipper config to {}".format(configpath))
|
self.log_debug("Writing Klipper config to {}".format(configpath))
|
||||||
except IOError:
|
except IOError:
|
||||||
self.log_error("Error: Couldn't write Klipper config file: {}".format(configpath))
|
self.log_error("Error: Couldn't write Klipper config file: {}".format(configpath))
|
||||||
|
@ -446,9 +462,8 @@ class KlipperPlugin(
|
||||||
configpath = os.path.expanduser(
|
configpath = os.path.expanduser(
|
||||||
self._settings.get(["configuration", "configpath"])
|
self._settings.get(["configuration", "configpath"])
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f = open(configpath, "r")
|
with io.open(configpath, "r", encoding="utf-8") as f:
|
||||||
data["config"] = f.read()
|
data["config"] = f.read()
|
||||||
f.close()
|
f.close()
|
||||||
except IOError:
|
except IOError:
|
||||||
|
@ -456,11 +471,29 @@ class KlipperPlugin(
|
||||||
"Error: Klipper config file not found at: {}".format(
|
"Error: Klipper config file not found at: {}".format(
|
||||||
configpath)
|
configpath)
|
||||||
)
|
)
|
||||||
|
except UnicodeDecodeError as e:
|
||||||
|
self.log_debug(
|
||||||
|
"Loading config with utf-8 failed. Trying to load config file with ISO-8859-1 now."
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
with io.open(configpath, "r", encoding="ISO-8859-1") as f:
|
||||||
|
data["config"] = f.read()
|
||||||
|
f.close()
|
||||||
|
except UnicodeDecodeError as e:
|
||||||
|
self.log_error(
|
||||||
|
"Error: Klipper config file cannot be decoded: {}".format(e)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.log_debug(
|
||||||
|
"Loading config with ISO-8859-1 finished."
|
||||||
|
)
|
||||||
|
self._settings.set(["config"], data["config"])
|
||||||
|
if sys.version_info[0] < 3:
|
||||||
|
data["config"] = data["config"].decode('utf-8')
|
||||||
|
return flask.jsonify(data=data["config"])
|
||||||
else:
|
else:
|
||||||
|
|
||||||
self._settings.set(["config"], data["config"])
|
self._settings.set(["config"], data["config"])
|
||||||
# self.send_message("reload", "config", "", data["config"])
|
|
||||||
# send the configdata to frontend to update ace editor
|
|
||||||
if sys.version_info[0] < 3:
|
if sys.version_info[0] < 3:
|
||||||
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"])
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -19,7 +19,7 @@ plugin_package = "octoprint_klipper"
|
||||||
|
|
||||||
plugin_name = "OctoKlipper"
|
plugin_name = "OctoKlipper"
|
||||||
|
|
||||||
plugin_version = "0.3.8.3"
|
plugin_version = "0.3.8.4"
|
||||||
|
|
||||||
plugin_description = """A plugin for OctoPrint to configure,control and monitor the Klipper 3D printer software."""
|
plugin_description = """A plugin for OctoPrint to configure,control and monitor the Klipper 3D printer software."""
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue