♻ codecs.open -> io.open
The general consensus regarding codecs.open appears to be that it's only left in Python 3 for backwards compatibility (which I find kind funny but I digress...) and that io.open should be used instead for Py2.6+ since it's available and behaving the same in both versions. So that's what we do here.
This commit is contained in:
1 parent
09cc7cf5f7
commit
32a8646135
7 files changed
+37
-24
No files matched your search
@@ -9,7 +9,7 @@ __copyright__ = "Copyright (C) 2016 The OctoPrint Project - Released under terms
|
||||
import octoprint.plugin
|
||||
|
||||
import calendar
|
||||
import codecs
|
||||
import io
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
@@ -388,7 +388,7 @@ class AnnouncementPlugin(octoprint.plugin.AssetPlugin,
|
||||
|
||||
response = r.text
|
||||
channel_path = self._get_channel_cache_path(key)
|
||||
with codecs.open(channel_path, mode="w", encoding="utf-8") as f:
|
||||
with io.open(channel_path, mode="wt", encoding="utf-8") as f:
|
||||
f.write(response)
|
||||
return feedparser.parse(response)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import flask
|
||||
import threading
|
||||
import os
|
||||
import yaml
|
||||
import codecs
|
||||
import io
|
||||
import time
|
||||
from binascii import hexlify
|
||||
from collections import defaultdict
|
||||
@@ -384,7 +384,7 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin,
|
||||
return
|
||||
|
||||
try:
|
||||
with codecs.open(self._key_path, 'rt', encoding="utf-8", errors="strict") as f:
|
||||
with io.open(self._key_path, 'rt', encoding="utf-8", errors="strict") as f:
|
||||
persisted = yaml.safe_load(f)
|
||||
except Exception:
|
||||
self._logger.exception("Could not load application keys from {}".format(self._key_path))
|
||||
@@ -405,8 +405,8 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin,
|
||||
to_persist[user_id] = [x.internal() for x in keys]
|
||||
|
||||
try:
|
||||
with atomic_write(self._key_path, mode='wt',) as f:
|
||||
yaml.safe_dump(to_persist, f)
|
||||
with atomic_write(self._key_path, mode='wt') as f:
|
||||
yaml.safe_dump(to_persist, f, allow_unicode=True)
|
||||
except Exception:
|
||||
self._logger.exception("Could not write application keys to {}".format(self._key_path))
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@ except ImportError:
|
||||
|
||||
from flask_babel import gettext
|
||||
|
||||
import codecs
|
||||
import io
|
||||
import flask
|
||||
import logging
|
||||
import os
|
||||
@@ -490,7 +490,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
|
||||
data_file = os.path.join(self.get_plugin_data_folder(), UNKNOWN_PLUGINS_FILE)
|
||||
if os.path.exists(data_file):
|
||||
try:
|
||||
with codecs.open(data_file, mode='rt', encoding="utf-8") as f:
|
||||
with io.open(data_file, mode='rb', encoding="utf-8") as f:
|
||||
unknown_plugins = json.load(f)
|
||||
|
||||
assert isinstance(unknown_plugins, list)
|
||||
@@ -797,7 +797,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
|
||||
|
||||
import yaml
|
||||
|
||||
with codecs.open(configfile) as f:
|
||||
with io.open(configfile, "rt", encoding="utf-8") as f:
|
||||
configdata = yaml.safe_load(f)
|
||||
|
||||
if configdata.get("accessControl", dict()).get("enabled", True):
|
||||
@@ -813,7 +813,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
|
||||
on_log_progress("Unpacked")
|
||||
|
||||
# install available plugins
|
||||
with codecs.open(os.path.join(temp, "plugin_list.json"), 'rt') as f:
|
||||
with io.open(os.path.join(temp, "plugin_list.json"), 'rb') as f:
|
||||
plugins = json.load(f)
|
||||
|
||||
known_plugins = []
|
||||
@@ -880,7 +880,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin,
|
||||
|
||||
unknown_plugins_path = os.path.join(datafolder, UNKNOWN_PLUGINS_FILE)
|
||||
try:
|
||||
with codecs.open(unknown_plugins_path, mode='wt', encoding="utf-8") as f:
|
||||
with io.open(unknown_plugins_path, mode='wb') as f:
|
||||
json.dump(unknown_plugins, f)
|
||||
except Exception:
|
||||
if callable(on_log_error):
|
||||
|
||||
@@ -195,8 +195,8 @@ def _rescue_changes(git_executable, folder):
|
||||
if returncode is None or returncode != 0:
|
||||
raise RuntimeError("Could not update, installation directory was dirty and state could not be persisted as a patch to {}".format(patch))
|
||||
|
||||
import codecs
|
||||
with codecs.open(patch, "w", encoding="utf-8", errors="replace") as f:
|
||||
import io
|
||||
with io.open(patch, "wt", encoding="utf-8", errors="replace") as f:
|
||||
for line in stdout:
|
||||
f.write(line)
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ __copyright__ = "Copyright (C) 2015 The OctoPrint Project - Released under terms
|
||||
|
||||
import os
|
||||
import datetime
|
||||
import codecs
|
||||
import io
|
||||
|
||||
from past.builtins import basestring
|
||||
|
||||
@@ -1012,7 +1012,7 @@ def _get_translations(locale, domain):
|
||||
|
||||
def messages_from_po(path, locale, domain):
|
||||
messages = dict()
|
||||
with codecs.open(path, encoding="utf-8") as f:
|
||||
with io.open(path, mode="rt", encoding="utf-8") as f:
|
||||
catalog = read_po(f, locale=locale, domain=domain)
|
||||
|
||||
for message in catalog:
|
||||
|
||||
@@ -1057,6 +1057,9 @@ def temppath(prefix=None, suffix=""):
|
||||
|
||||
def bom_aware_open(filename, encoding="ascii", mode="r", **kwargs):
|
||||
import codecs
|
||||
import io
|
||||
|
||||
assert "b" not in mode, "binary mode not support by bom_aware_open"
|
||||
|
||||
codec = codecs.lookup(encoding)
|
||||
encoding = codec.name
|
||||
@@ -1069,15 +1072,13 @@ def bom_aware_open(filename, encoding="ascii", mode="r", **kwargs):
|
||||
# these encodings might have a BOM, so let's see if there is one
|
||||
bom = getattr(codecs, potential_bom_attribute)
|
||||
|
||||
with io.open(filename, 'rb') as f:
|
||||
with io.open(filename, mode='rb') as f:
|
||||
header = f.read(4)
|
||||
|
||||
if header.startswith(bom):
|
||||
encoding += "-sig"
|
||||
|
||||
# Py3 doesn't like 't'
|
||||
mode = mode.replace('t','')
|
||||
return codecs.open(filename, encoding=encoding, mode=mode, **kwargs)
|
||||
return io.open(filename, encoding=encoding, mode=mode, **kwargs)
|
||||
|
||||
|
||||
def is_hidden_path(path):
|
||||
|
||||
@@ -67,14 +67,26 @@ class BomAwareOpenTest(unittest.TestCase):
|
||||
except UnicodeDecodeError:
|
||||
pass
|
||||
|
||||
def test_bom_aware_open_parameters(self):
|
||||
"""Tests that the parameters are propagated properly."""
|
||||
def test_bom_aware_open_parameters_text_mode(self):
|
||||
"""Tests that the parameters are propagated properly in text mode."""
|
||||
|
||||
with mock.patch("codecs.open") as mock_open:
|
||||
with octoprint.util.bom_aware_open(self.filename_utf8_without_bom, mode="rb", encoding="utf-8", errors="ignore") as f:
|
||||
with mock.patch("io.open", wraps=io.open) as mock_open:
|
||||
with octoprint.util.bom_aware_open(self.filename_utf8_without_bom, mode="rt", encoding="utf-8", errors="ignore") as f:
|
||||
f.readlines()
|
||||
|
||||
mock_open.assert_called_once_with(self.filename_utf8_without_bom, encoding="utf-8", mode="rb", errors="ignore")
|
||||
calls = [mock.call(self.filename_utf8_without_bom, mode="rb"),
|
||||
mock.call(self.filename_utf8_without_bom, encoding="utf-8", mode="rt", errors="ignore")]
|
||||
mock_open.assert_has_calls(calls)
|
||||
|
||||
def test_bom_aware_open_parameters_binary_mode(self):
|
||||
"""Tests that binary mode raises an AssertionError."""
|
||||
self.assertRaises(AssertionError,
|
||||
octoprint.util.bom_aware_open,
|
||||
self.filename_utf8_without_bom,
|
||||
mode="rb",
|
||||
encoding="utf-8",
|
||||
errors="ignore")
|
||||
|
||||
|
||||
class TestAtomicWrite(unittest.TestCase):
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user