From c623e90ebc59b90b4f4cf582ab8185b2872acff4 Mon Sep 17 00:00:00 2001 From: MirceaDan Date: Sat, 1 Dec 2018 17:43:19 -0800 Subject: [PATCH] chnaged teh default from binary to text mode seams like open was defaulting to text mode, since now is specified in clear i made sure it matches with teh previous default --- src/octoprint/__init__.py | 2 +- src/octoprint/_version.py | 2 +- src/octoprint/access/groups.py | 2 +- src/octoprint/access/users.py | 2 +- src/octoprint/daemon.py | 10 +++++----- src/octoprint/filemanager/__init__.py | 2 +- src/octoprint/filemanager/storage.py | 6 +++--- src/octoprint/plugin/core.py | 2 +- src/octoprint/plugins/cura/__init__.py | 2 +- src/octoprint/plugins/logging/__init__.py | 2 +- src/octoprint/plugins/pluginmanager/__init__.py | 4 ++-- src/octoprint/plugins/softwareupdate/__init__.py | 2 +- src/octoprint/plugins/virtual_printer/virtual.py | 4 ++-- src/octoprint/printer/profile.py | 2 +- src/octoprint/server/api/languages.py | 2 +- src/octoprint/server/util/flask.py | 2 +- src/octoprint/settings.py | 8 ++++---- src/octoprint/util/avr_isp/intelHex.py | 2 +- src/octoprint/util/pip.py | 2 +- src/octoprint/util/piptestballoon/setup.py | 2 +- tests/filemanager/test_localstorage.py | 2 +- tests/settings/test_settings.py | 4 ++-- tests/test_daemon.py | 16 ++++++++-------- 23 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/octoprint/__init__.py b/src/octoprint/__init__.py index 10ca2bc33..3e6d865c9 100644 --- a/src/octoprint/__init__.py +++ b/src/octoprint/__init__.py @@ -294,7 +294,7 @@ def init_logging(settings, use_logging_file=True, logging_file=None, default_con config_from_file = {} if os.path.exists(logging_file) and os.path.isfile(logging_file): import yaml - with io.open(logging_file, 'rb') as f: + with io.open(logging_file, 'rt', encoding='utf-8') as f: config_from_file = yaml.safe_load(f) # we merge that with the default config diff --git a/src/octoprint/_version.py b/src/octoprint/_version.py index d30901e18..0ef9f8186 100644 --- a/src/octoprint/_version.py +++ b/src/octoprint/_version.py @@ -131,7 +131,7 @@ def git_get_keywords(versionfile_abs): # _version.py. keywords = {} try: - f = io.open(versionfile_abs, 'rb') + f = io.open(versionfile_abs, 'rt', encoding='utf-8') for line in f.readlines(): if line.strip().startswith("git_refnames ="): mo = re.search(r'=\s*"(.*)"', line) diff --git a/src/octoprint/access/groups.py b/src/octoprint/access/groups.py index fa87cd884..efdd621bc 100644 --- a/src/octoprint/access/groups.py +++ b/src/octoprint/access/groups.py @@ -184,7 +184,7 @@ class FilebasedGroupManager(GroupManager): def _load(self): if os.path.exists(self._groupfile) and os.path.isfile(self._groupfile): try: - with io.open(self._groupfile, 'rb') as f: + with io.open(self._groupfile, 'rt', encoding='utf-8') as f: data = yaml.safe_load(f) if not "groups" in data: diff --git a/src/octoprint/access/users.py b/src/octoprint/access/users.py index 8fbc15107..6fdf53be9 100644 --- a/src/octoprint/access/users.py +++ b/src/octoprint/access/users.py @@ -433,7 +433,7 @@ class FilebasedUserManager(UserManager): def _load(self): if os.path.exists(self._userfile) and os.path.isfile(self._userfile): self._customized = True - with io.open(self._userfile, 'rb') as f: + with io.open(self._userfile, 'rt', encoding='utf-8') as f: data = yaml.safe_load(f) for name in data.keys(): attributes = data[name] diff --git a/src/octoprint/daemon.py b/src/octoprint/daemon.py index 9c230d5e9..f0158f847 100644 --- a/src/octoprint/daemon.py +++ b/src/octoprint/daemon.py @@ -61,9 +61,9 @@ class Daemon: # redirect standard file descriptors sys.stdout.flush() sys.stderr.flush() - si = io.open(os.devnull, 'rb') - so = io.open(os.devnull, 'a+') - se = io.open(os.devnull, 'a+') + si = io.open(os.devnull, 'rt', encoding='utf-8') + so = io.open(os.devnull, 'at+', encoding='utf-8') + se = io.open(os.devnull, 'at+', encoding='utf-8') os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(so.fileno(), sys.stdout.fileno()) @@ -143,7 +143,7 @@ class Daemon: def get_pid(self): """Get the pid from the pidfile.""" try: - with io.open(self.pidfile,'rb') as pf: + with io.open(self.pidfile,'rt', encoding='utf-8') as pf: pid = int(pf.read().strip()) except (IOError, ValueError): pid = None @@ -151,7 +151,7 @@ class Daemon: def set_pid(self, pid): """Write the pid to the pidfile.""" - with io.open(self.pidfile,'wb+') as f: + with io.open(self.pidfile,'wt+', encoding='utf-8') as f: f.write(str(pid) + '\n') def remove_pidfile(self): diff --git a/src/octoprint/filemanager/__init__.py b/src/octoprint/filemanager/__init__.py index a0e54a60b..5da5b076f 100644 --- a/src/octoprint/filemanager/__init__.py +++ b/src/octoprint/filemanager/__init__.py @@ -643,7 +643,7 @@ class FileManager(object): import yaml try: - with io.open(self._recovery_file, 'rb') as f: + with io.open(self._recovery_file, 'rt', encoding='utf-8') as f: data = yaml.safe_load(f) return data except: diff --git a/src/octoprint/filemanager/storage.py b/src/octoprint/filemanager/storage.py index 392c5ea6c..84ef8f983 100644 --- a/src/octoprint/filemanager/storage.py +++ b/src/octoprint/filemanager/storage.py @@ -500,7 +500,7 @@ class LocalFileStorage(StorageInterface): if os.path.exists(old_metadata_path): # load the old metadata file try: - with io.open(old_metadata_path, 'rb') as f: + with io.open(old_metadata_path, 'rt', encoding='utf-8') as f: import yaml self._old_metadata = yaml.safe_load(f) except: @@ -1522,7 +1522,7 @@ class LocalFileStorage(StorageInterface): metadata_path = os.path.join(path, ".metadata.json") if os.path.exists(metadata_path): - with io.open(metadata_path, 'rb') as f: + with io.open(metadata_path, 'rt', encoding='utf-8') as f: try: import json metadata = json.load(f) @@ -1577,7 +1577,7 @@ class LocalFileStorage(StorageInterface): # TODO 1.3.10 Remove ".metadata.yaml" files return - with io.open(metadata_path_yaml, 'rb') as f: + with io.open(metadata_path_yaml, 'rt', encoding='utf-8') as f: try: metadata = yaml.safe_load(f) except: diff --git a/src/octoprint/plugin/core.py b/src/octoprint/plugin/core.py index e9de9e726..d4f1dd3f8 100644 --- a/src/octoprint/plugin/core.py +++ b/src/octoprint/plugin/core.py @@ -1620,7 +1620,7 @@ def is_editable_install(install_dir, package, module, location): if os.path.isfile(package_link): expected_target = os.path.normcase(os.path.realpath(location)) try: - with io.open(package_link, 'rb') as f: + with io.open(package_link, 'rt', encoding='utf-8') as f: contents = f.readlines() for line in contents: target = os.path.normcase(os.path.realpath(os.path.join(line.strip(), module))) diff --git a/src/octoprint/plugins/cura/__init__.py b/src/octoprint/plugins/cura/__init__.py index a6d9b8e14..146b122a9 100644 --- a/src/octoprint/plugins/cura/__init__.py +++ b/src/octoprint/plugins/cura/__init__.py @@ -460,7 +460,7 @@ class CuraPlugin(octoprint.plugin.SlicerPlugin, def _load_profile(self, path): import yaml profile_dict = dict() - with io.open(path, 'rb') as f: + with io.open(path, 'rt', encoding='utf-8') as f: try: profile_dict = yaml.safe_load(f) except: diff --git a/src/octoprint/plugins/logging/__init__.py b/src/octoprint/plugins/logging/__init__.py index 92b602e17..d4415a56f 100644 --- a/src/octoprint/plugins/logging/__init__.py +++ b/src/octoprint/plugins/logging/__init__.py @@ -146,7 +146,7 @@ class LoggingPlugin(octoprint.plugin.AssetPlugin, config_from_file = {} if os.path.exists(logging_file) and os.path.isfile(logging_file): import yaml - with io.open(logging_file, 'rb') as f: + with io.open(logging_file, 'rt', encoding='utf-8') as f: config_from_file = yaml.safe_load(f) return config_from_file diff --git a/src/octoprint/plugins/pluginmanager/__init__.py b/src/octoprint/plugins/pluginmanager/__init__.py index c87d83e7f..295e27a76 100644 --- a/src/octoprint/plugins/pluginmanager/__init__.py +++ b/src/octoprint/plugins/pluginmanager/__init__.py @@ -759,7 +759,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, if mtime + self._repository_cache_ttl >= time.time() > mtime: try: import json - with io.open(self._repository_cache_path, 'rb') as f: + with io.open(self._repository_cache_path, 'rt', encoding='utf-8') as f: repo_data = json.load(f) self._logger.info("Loaded plugin repository data from disk, was still valid") except: @@ -833,7 +833,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, if mtime + self._notices_cache_ttl >= time.time() > mtime: try: import json - with io.open(self._notices_cache_path, 'rb') as f: + with io.open(self._notices_cache_path, 'rt', encoding='utf-8') as f: notice_data = json.load(f) self._logger.info("Loaded notice data from disk, was still valid") except: diff --git a/src/octoprint/plugins/softwareupdate/__init__.py b/src/octoprint/plugins/softwareupdate/__init__.py index 473b47115..e31fe4af0 100644 --- a/src/octoprint/plugins/softwareupdate/__init__.py +++ b/src/octoprint/plugins/softwareupdate/__init__.py @@ -188,7 +188,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, import yaml try: - with io.open(self._version_cache_path, 'rb') as f: + with io.open(self._version_cache_path, 'rt', encoding='utf-8') as f: data = yaml.safe_load(f) timestamp = os.stat(self._version_cache_path).st_mtime except: diff --git a/src/octoprint/plugins/virtual_printer/virtual.py b/src/octoprint/plugins/virtual_printer/virtual.py index 6e7a28640..ca3a7c4de 100644 --- a/src/octoprint/plugins/virtual_printer/virtual.py +++ b/src/octoprint/plugins/virtual_printer/virtual.py @@ -1107,7 +1107,7 @@ class VirtualPrinter(object): handle = None try: - handle = io.open(file, 'wb') + handle = io.open(file, 'wt', encoding='utf-8') except: self._output("error writing to file") if handle is not None: @@ -1134,7 +1134,7 @@ class VirtualPrinter(object): def _sdPrintingWorker(self): self._selectedSdFilePos = 0 try: - with io.open(self._selectedSdFile, 'rb') as f: + with io.open(self._selectedSdFile, 'rt', encoding='utf-8') as f: for line in iter(f.readline, ""): if self._killed or not self._sdPrinting: break diff --git a/src/octoprint/printer/profile.py b/src/octoprint/printer/profile.py index 2363101f2..d44cdc919 100644 --- a/src/octoprint/printer/profile.py +++ b/src/octoprint/printer/profile.py @@ -431,7 +431,7 @@ class PrinterProfileManager(object): return None import yaml - with io.open(path, 'rb') as f: + with io.open(path, 'rt', encoding='utf-8') as f: profile = yaml.safe_load(f) if profile is None or not isinstance(profile, dict): diff --git a/src/octoprint/server/api/languages.py b/src/octoprint/server/api/languages.py index 4eab898d2..48542f43f 100644 --- a/src/octoprint/server/api/languages.py +++ b/src/octoprint/server/api/languages.py @@ -50,7 +50,7 @@ def getInstalledLanguagePacks(): if os.path.isfile(meta_path): import yaml try: - with io.open(meta_path, 'rb') as f: + with io.open(meta_path, 'rt', encoding='utf-8') as f: meta = yaml.safe_load(f) except: pass diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index a6c02f7a5..137d99255 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -809,7 +809,7 @@ class PreemptiveCache(object): cache_data = None with self._lock: try: - with io.open(self.cachefile, 'rb') as f: + with io.open(self.cachefile, 'rt', encoding='utf-8') as f: cache_data = yaml.safe_load(f) except IOError as e: import errno diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index 301042129..782a443b5 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -868,7 +868,7 @@ class Settings(object): def load(self, migrate=False): if os.path.exists(self._configfile) and os.path.isfile(self._configfile): - with io.open(self._configfile, 'rb') as f: + with io.open(self._configfile, 'rt', encoding='utf-8') as f: try: self._config = yaml.safe_load(f) self._mtime = self.last_modified @@ -907,7 +907,7 @@ class Settings(object): if isinstance(overlay, basestring): if os.path.exists(overlay) and os.path.isfile(overlay): - with io.open(overlay, 'rb') as f: + with io.open(overlay, 'rt', encoding='utf-8') as f: config = yaml.safe_load(f) elif isinstance(overlay, dict): config = overlay @@ -1795,8 +1795,8 @@ def _validate_folder(folder, create=True, check_writable=True, deep_check_writab # to determine whether things are *actually* writable testfile = os.path.join(folder, ".testballoon.txt") try: - with io.open(testfile, 'wb') as f: - f.write("test") + with io.open(testfile, 'wt', encoding='utf-8') as f: + f.write("test".decode('utf-8')) os.remove(testfile) except: if log_error: diff --git a/src/octoprint/util/avr_isp/intelHex.py b/src/octoprint/util/avr_isp/intelHex.py index 85b893de3..fb965863a 100644 --- a/src/octoprint/util/avr_isp/intelHex.py +++ b/src/octoprint/util/avr_isp/intelHex.py @@ -5,7 +5,7 @@ from builtins import range def readHex(filename): data = [] extraAddr = 0 - f = io.open(filename, 'rb') + f = io.open(filename, 'rt', encoding='utf-8') for line in f: line = line.strip() if line[0] != ':': diff --git a/src/octoprint/util/pip.py b/src/octoprint/util/pip.py index 9fcb958a4..04075e858 100644 --- a/src/octoprint/util/pip.py +++ b/src/octoprint/util/pip.py @@ -370,7 +370,7 @@ class PipCaller(CommandlineCaller): return False, False, False, None data = dict() - with io.open(testballoon_output_file, 'rb') as f: + with io.open(testballoon_output_file, 'rt', encoding='utf-8') as f: for line in f: key, value = line.split("=", 2) data[key] = value diff --git a/src/octoprint/util/piptestballoon/setup.py b/src/octoprint/util/piptestballoon/setup.py index a884181a3..a55bd5f7c 100644 --- a/src/octoprint/util/piptestballoon/setup.py +++ b/src/octoprint/util/piptestballoon/setup.py @@ -40,7 +40,7 @@ path = os.environ.get("TESTBALLOON_OUTPUT", None) if path is not None: # environment variable set, write to a log path = os.path.abspath(path) - with io.open(path, 'w+b') as output: + with io.open(path, 'wb+') as output: produce_output(output) else: # write to stdout diff --git a/tests/filemanager/test_localstorage.py b/tests/filemanager/test_localstorage.py index e6459bda7..e5cd288a5 100644 --- a/tests/filemanager/test_localstorage.py +++ b/tests/filemanager/test_localstorage.py @@ -664,7 +664,7 @@ class LocalStorageTest(unittest.TestCase): self.assertTrue(os.path.exists(yaml_path)) # TODO 1.3.10 change to assertFalse import json - with io.open(json_path, 'rb') as f: + with io.open(json_path, 'rt', encoding='utf-8') as f: json_metadata = json.load(f) self.assertDictEqual(metadata, json_metadata) diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index dfcf87612..1b06cd87a 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -443,7 +443,7 @@ class TestSettings(unittest.TestCase): def test_effective_hash(self): with self.mocked_config(): hash = hashlib.md5() - hash.update(yaml.safe_dump(self.expected_effective)) + hash.update(yaml.safe_dump(self.expected_effective).encode('utf-8')) expected_effective_hash = hash.hexdigest() print(yaml.safe_dump(self.expected_effective)) @@ -456,7 +456,7 @@ class TestSettings(unittest.TestCase): def test_config_hash(self): with self.mocked_config(): hash = hashlib.md5() - hash.update(yaml.safe_dump(self.config)) + hash.update(yaml.safe_dump(self.config).encode('utf-8')) expected_config_hash = hash.hexdigest() settings = octoprint.settings.Settings() diff --git a/tests/test_daemon.py b/tests/test_daemon.py index 18fc29e25..b9219ef7e 100644 --- a/tests/test_daemon.py +++ b/tests/test_daemon.py @@ -128,9 +128,9 @@ class DaemonTest(unittest.TestCase): mock_stderr.flush.assert_called_once_with() self.assertListEqual(mock_open.mock_calls, - [mock.call(mock_devnull, "rb"), - mock.call(mock_devnull, "a+"), - mock.call(mock_devnull, "a+")]) + [mock.call(mock_devnull, 'rt', encoding='utf-8'), + mock.call(mock_devnull, 'at+', encoding='utf-8'), + mock.call(mock_devnull, 'at+', encoding='utf-8')]) self.assertListEqual(mock_dup2.mock_calls, [mock.call("new_stdin", "stdin"), mock.call("new_stdout", "stdout"), @@ -396,7 +396,7 @@ class DaemonTest(unittest.TestCase): # assert self.assertEqual(result, pid) - m.assert_called_once_with(self.pidfile, "rb") + m.assert_called_once_with(self.pidfile, 'rt', encoding='utf-8') def test_get_pid_ioerror(self): # setup @@ -409,7 +409,7 @@ class DaemonTest(unittest.TestCase): # assert self.assertIsNone(result) - m.assert_called_once_with(self.pidfile, "rb") + m.assert_called_once_with(self.pidfile, 'rt', encoding='utf-8') def test_get_pid_valueerror(self): # setup @@ -421,7 +421,7 @@ class DaemonTest(unittest.TestCase): # assert self.assertIsNone(result) - m.assert_called_once_with(self.pidfile, "rb") + m.assert_called_once_with(self.pidfile, 'rt', encoding='utf-8') def test_set_pid(self): # setup @@ -432,7 +432,7 @@ class DaemonTest(unittest.TestCase): self.daemon.set_pid(pid) # assert - m.assert_called_once_with(self.pidfile, "wb+") + m.assert_called_once_with(self.pidfile, 'wt+', encoding='utf-8') handle = m() handle.write.assert_called_once_with("{}\n".format(pid)) @@ -445,7 +445,7 @@ class DaemonTest(unittest.TestCase): self.daemon.set_pid(pid) # assert - m.assert_called_once_with(self.pidfile, "wb+") + m.assert_called_once_with(self.pidfile, 'wt+', encoding='utf-8') handle = m() handle.write.assert_called_once_with("{}\n".format(pid))