Skip to content

Instantly share code, notes, and snippets.

@foosel
Last active March 7, 2022 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foosel/77bb2a37bbac66d7fbb9cc5096fb4035 to your computer and use it in GitHub Desktop.
Save foosel/77bb2a37bbac66d7fbb9cc5096fb4035 to your computer and use it in GitHub Desktop.
OctoPrint plugin that backports the fix of #4392 from OctoPrint 1.8.0+ to earlier versions. See also https://github.com/OctoPrint/OctoPrint/issues/4392. Install via Plugin Manager > Get More using URL https://gist.github.com/foosel/77bb2a37bbac66d7fbb9cc5096fb4035/raw/fix_4392.py
import octoprint.plugin
FIXED_VERSION = "1.8.0"
class Fix4392Plugin(octoprint.plugin.StartupPlugin):
def on_startup(self, *args, **kwargs):
import octoprint.util.commandline
from past.builtins import unicode
import re
_ANSI_CSI_PATTERN = octoprint.util.commandline._ANSI_CSI_PATTERN
_ANSI_OSC_PATTERN = octoprint.util.commandline._ANSI_OSC_PATTERN
_ANSI_PATTERN = b"|".join([_ANSI_CSI_PATTERN, _ANSI_OSC_PATTERN])
_ANSI_REGEX = {
"bytes": re.compile(_ANSI_PATTERN),
"unicode": re.compile(_ANSI_PATTERN.decode("utf-8")),
}
def fixed_clean_ansi(line):
if isinstance(line, unicode):
return _ANSI_REGEX["unicode"].sub("", line)
return _ANSI_REGEX["bytes"].sub(b"", line)
self._logger.info("Monkey-patching octoprint.util.commandline.clean_ansi to fix unicode bug...")
octoprint.util.commandline.clean_ansi = fixed_clean_ansi
__plugin_name__ = "Backported fix for #4392"
__plugin_version__ = "1.0.0"
__plugin_description__ = "Fixes the UnicodeEncodeError bug #4392 by monkey-patching the affected function"
__plugin_url__ = "https://github.com/OctoPrint/OctoPrint/issues/4392"
__plugin_pythoncompat__ = ">=2.7,<4"
__plugin_implementation__ = Fix4392Plugin()
def __plugin_check__():
from octoprint.util.version import is_octoprint_compatible
import logging
compatible = is_octoprint_compatible("<{}".format(FIXED_VERSION))
if not compatible:
logging.getLogger(__name__).info("Plugin is not needed in OctoPrint versions >= {}".format(FIXED_VERSION))
return compatible
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment