From 7fcc3551ca80e5711da09c32e84da3c8486ada8e Mon Sep 17 00:00:00 2001 From: Matthias Urlichs Date: Mon, 28 Jan 2019 11:29:46 +0100 Subject: [PATCH] We need text mode for JSON and YAML --- src/octoprint/__init__.py | 4 ++-- src/octoprint/access/groups.py | 2 +- src/octoprint/access/users.py | 2 +- src/octoprint/cli/client.py | 4 ++-- src/octoprint/filemanager/__init__.py | 2 +- src/octoprint/filemanager/storage.py | 4 ++-- src/octoprint/plugins/appkeys/__init__.py | 4 ++-- src/octoprint/plugins/backup/__init__.py | 6 +++--- 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/printer/profile.py | 2 +- src/octoprint/server/util/flask.py | 4 ++-- src/octoprint/settings.py | 2 +- tests/filemanager/test_localstorage.py | 2 +- tests/settings/test_settings.py | 4 ++-- 17 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/octoprint/__init__.py b/src/octoprint/__init__.py index 7d224f6db..d41fbdfc9 100644 --- a/src/octoprint/__init__.py +++ b/src/octoprint/__init__.py @@ -484,7 +484,7 @@ def get_plugin_blacklist(settings, connectivity_checker=None): if os.stat(path).st_mtime + ttl < time.time(): return None - with bom_aware_open(path, encoding="utf-8", mode="r") as f: + with bom_aware_open(path, encoding="utf-8", mode='rt') as f: result = yaml.safe_load(f) if isinstance(result, list): @@ -498,7 +498,7 @@ def get_plugin_blacklist(settings, connectivity_checker=None): if cache is not None: try: - with bom_aware_open(cache, encoding="utf-8", mode="w") as f: + with bom_aware_open(cache, encoding="utf-8", mode='wt') as f: yaml.safe_dump(result, f) except Exception as e: logger.info("Fetched plugin blacklist but couldn't write it to its cache file: %s", e) diff --git a/src/octoprint/access/groups.py b/src/octoprint/access/groups.py index b84f58e2b..a1470e7ed 100644 --- a/src/octoprint/access/groups.py +++ b/src/octoprint/access/groups.py @@ -242,7 +242,7 @@ class FilebasedGroupManager(GroupManager): data = dict(groups=groups, tracked=[x.key for x in Permissions.all()]) - with atomic_write(self._groupfile, mode="wb", permissions=0o600, max_permissions=0o666) as f: + with atomic_write(self._groupfile, mode='wt', permissions=0o600, max_permissions=0o666) as f: import yaml yaml.safe_dump(data, f, default_flow_style=False, indent=4, allow_unicode=True) self._dirty = False diff --git a/src/octoprint/access/users.py b/src/octoprint/access/users.py index 06f95db93..6b67d692c 100644 --- a/src/octoprint/access/users.py +++ b/src/octoprint/access/users.py @@ -503,7 +503,7 @@ class FilebasedUserManager(UserManager): "roles": user._roles } - with atomic_write(self._userfile, mode="wb", permissions=0o600, max_permissions=0o666) as f: + with atomic_write(self._userfile, mode='wt', permissions=0o600, max_permissions=0o666) as f: yaml.safe_dump(data, f, default_flow_style=False, indent=4, allow_unicode=True) self._dirty = False self._load() diff --git a/src/octoprint/cli/client.py b/src/octoprint/cli/client.py index 39023d6e0..285377129 100644 --- a/src/octoprint/cli/client.py +++ b/src/octoprint/cli/client.py @@ -150,11 +150,11 @@ def post_from_file(ctx, path, file_path, json_flag, yaml_flag, timeout): """POSTs JSON data to the specified server path, taking the data from the specified file.""" if json_flag or yaml_flag: if json_flag: - with io.open(file_path, 'rb') as fp: + with io.open(file_path, 'rt') as fp: data = json.load(fp) else: import yaml - with io.open(file_path, 'rb') as fp: + with io.open(file_path, 'rt') as fp: data = yaml.safe_load(fp) r = ctx.obj.client.post_json(path, data, timeout=timeout) diff --git a/src/octoprint/filemanager/__init__.py b/src/octoprint/filemanager/__init__.py index 46bd16df5..a2fe2cda1 100644 --- a/src/octoprint/filemanager/__init__.py +++ b/src/octoprint/filemanager/__init__.py @@ -625,7 +625,7 @@ class FileManager(object): pos=pos, date=time.time()) try: - with atomic_write(self._recovery_file, max_permissions=0o666) as f: + with atomic_write(self._recovery_file, mode='wt', max_permissions=0o666) as f: yaml.safe_dump(data, stream=f, default_flow_style=False, indent=2, allow_unicode=True) except Exception: self._logger.exception("Could not write recovery data to file {}".format(self._recovery_file)) diff --git a/src/octoprint/filemanager/storage.py b/src/octoprint/filemanager/storage.py index d82819465..196e9b7ed 100644 --- a/src/octoprint/filemanager/storage.py +++ b/src/octoprint/filemanager/storage.py @@ -1539,7 +1539,7 @@ class LocalFileStorage(StorageInterface): metadata_path = os.path.join(path, ".metadata.json") try: import json - with atomic_write(metadata_path) as f: + with atomic_write(metadata_path, mode='wt') as f: json.dump(metadata, f, indent=4, separators=(",", ": ")) except Exception: self._logger.exception("Error while writing .metadata.json to {path}".format(**locals())) @@ -1588,7 +1588,7 @@ class LocalFileStorage(StorageInterface): # looks invalid, ignore it return - with atomic_write(metadata_path_json) as f: + with atomic_write(metadata_path_json, mode='wt') as f: json.dump(metadata, f, indent=4, separators=(",", ": ")) # TODO 1.3.10 Remove ".metadata.yaml" files diff --git a/src/octoprint/plugins/appkeys/__init__.py b/src/octoprint/plugins/appkeys/__init__.py index 762865a5d..8b2e5301c 100644 --- a/src/octoprint/plugins/appkeys/__init__.py +++ b/src/octoprint/plugins/appkeys/__init__.py @@ -366,7 +366,7 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, return try: - with codecs.open(self._key_path, "rb", encoding="utf-8", errors="strict") as f: + with codecs.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)) @@ -387,7 +387,7 @@ class AppKeysPlugin(octoprint.plugin.AssetPlugin, to_persist[user_id] = [x.internal() for x in keys] try: - with atomic_write(self._key_path) as f: + with atomic_write(self._key_path, mode='wt',) as f: yaml.safe_dump(to_persist, f) except Exception: self._logger.exception("Could not write application keys to {}".format(self._key_path)) diff --git a/src/octoprint/plugins/backup/__init__.py b/src/octoprint/plugins/backup/__init__.py index f212d2af3..3a11e3ce4 100644 --- a/src/octoprint/plugins/backup/__init__.py +++ b/src/octoprint/plugins/backup/__init__.py @@ -485,7 +485,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="r", encoding="utf-8") as f: + with codecs.open(data_file, mode='rt', encoding="utf-8") as f: unknown_plugins = json.load(f) assert isinstance(unknown_plugins, list) @@ -807,7 +807,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin, on_log_progress(u"Unpacked") # install available plugins - with codecs.open(os.path.join(temp, "plugin_list.json"), "r") as f: + with codecs.open(os.path.join(temp, "plugin_list.json"), 'rt') as f: plugins = json.load(f) known_plugins = [] @@ -874,7 +874,7 @@ class BackupPlugin(octoprint.plugin.SettingsPlugin, unknown_plugins_path = os.path.join(datafolder, UNKNOWN_PLUGINS_FILE) try: - with codecs.open(unknown_plugins_path, mode="w", encoding="utf-8") as f: + with codecs.open(unknown_plugins_path, mode='wt', encoding="utf-8") as f: json.dump(unknown_plugins, f) except Exception: if callable(on_log_error): diff --git a/src/octoprint/plugins/cura/__init__.py b/src/octoprint/plugins/cura/__init__.py index 8d0414f8b..3736caf3a 100644 --- a/src/octoprint/plugins/cura/__init__.py +++ b/src/octoprint/plugins/cura/__init__.py @@ -474,7 +474,7 @@ class CuraPlugin(octoprint.plugin.SlicerPlugin, def _save_profile(self, path, profile, allow_overwrite=True): import yaml - with octoprint.util.atomic_write(path, mode='wb', max_permissions=0o666) as f: + with octoprint.util.atomic_write(path, mode='wt', max_permissions=0o666) as f: yaml.safe_dump(profile, f, default_flow_style=False, indent=2, allow_unicode=True) def _convert_to_engine(self, profile_path, printer_profile, pos_x=None, pos_y=None, used_extruders=1): diff --git a/src/octoprint/plugins/logging/__init__.py b/src/octoprint/plugins/logging/__init__.py index 8e7fbbb9f..fdb9a6314 100644 --- a/src/octoprint/plugins/logging/__init__.py +++ b/src/octoprint/plugins/logging/__init__.py @@ -189,7 +189,7 @@ class LoggingPlugin(octoprint.plugin.AssetPlugin, config["loggers"] = {k: v for k, v in config["loggers"].items() if len(v)} # save - with octoprint.util.atomic_write(self._get_logging_file(), mode="wb", max_permissions=0o666) as f: + with octoprint.util.atomic_write(self._get_logging_file(), mode='wt', max_permissions=0o666) as f: yaml.safe_dump(config, f, default_flow_style=False, indent=2, allow_unicode=True) # set runtime logging levels now diff --git a/src/octoprint/plugins/pluginmanager/__init__.py b/src/octoprint/plugins/pluginmanager/__init__.py index 622ccad04..c3c1e00bd 100644 --- a/src/octoprint/plugins/pluginmanager/__init__.py +++ b/src/octoprint/plugins/pluginmanager/__init__.py @@ -821,7 +821,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, try: import json - with octoprint.util.atomic_write(self._repository_cache_path, mode='wb') as f: + with octoprint.util.atomic_write(self._repository_cache_path, mode='wt') as f: json.dump(repo_data, f) except Exception as e: self._logger.exception("Error while saving repository data to {}: {}".format(self._repository_cache_path, str(e))) @@ -871,7 +871,7 @@ class PluginManagerPlugin(octoprint.plugin.SimpleApiPlugin, try: import json - with octoprint.util.atomic_write(self._notices_cache_path, mode="wb") as f: + with octoprint.util.atomic_write(self._notices_cache_path, mode="wt") as f: json.dump(notice_data, f) except Exception as e: self._logger.exception("Error while saving notices to {}: {}".format(self._notices_cache_path, str(e))) diff --git a/src/octoprint/plugins/softwareupdate/__init__.py b/src/octoprint/plugins/softwareupdate/__init__.py index 9ae6c69ac..d2d8a538e 100644 --- a/src/octoprint/plugins/softwareupdate/__init__.py +++ b/src/octoprint/plugins/softwareupdate/__init__.py @@ -253,7 +253,7 @@ class SoftwareUpdatePlugin(octoprint.plugin.BlueprintPlugin, octoprint_version = get_versions()["version"] self._version_cache["__version"] = octoprint_version - with atomic_write(self._version_cache_path, max_permissions=0o666) as file_obj: + with atomic_write(self._version_cache_path, mode='wt', max_permissions=0o666) as file_obj: yaml.safe_dump(self._version_cache, stream=file_obj, default_flow_style=False, indent=2, allow_unicode=True) self._version_cache_dirty = False diff --git a/src/octoprint/printer/profile.py b/src/octoprint/printer/profile.py index 33223b2ca..b8f549d27 100644 --- a/src/octoprint/printer/profile.py +++ b/src/octoprint/printer/profile.py @@ -461,7 +461,7 @@ class PrinterProfileManager(object): from octoprint.util import atomic_write import yaml try: - with atomic_write(path, mode='wb', max_permissions=0o666) as f: + with atomic_write(path, mode='wt', max_permissions=0o666) as f: yaml.safe_dump(profile, f, default_flow_style=False, indent=2, allow_unicode=True) except Exception as e: self._logger.exception("Error while trying to save profile %s" % profile["id"]) diff --git a/src/octoprint/server/util/flask.py b/src/octoprint/server/util/flask.py index 3e83f0d73..a604c6111 100644 --- a/src/octoprint/server/util/flask.py +++ b/src/octoprint/server/util/flask.py @@ -849,7 +849,7 @@ class PreemptiveCache(object): cache_data = None with self._lock: try: - with io.open(self.cachefile, "r") as f: + with io.open(self.cachefile, 'rt') as f: cache_data = yaml.safe_load(f) except IOError as e: import errno @@ -873,7 +873,7 @@ class PreemptiveCache(object): with self._lock: try: - with atomic_write(self.cachefile, "wb", max_permissions=0o666) as handle: + with atomic_write(self.cachefile, 'wt', max_permissions=0o666) as handle: yaml.safe_dump(data, handle,default_flow_style=False, indent=4, allow_unicode=True) except Exception: self._logger.exception("Error while writing {}".format(self.cachefile)) diff --git a/src/octoprint/settings.py b/src/octoprint/settings.py index e7216a364..7e4b25046 100644 --- a/src/octoprint/settings.py +++ b/src/octoprint/settings.py @@ -1348,7 +1348,7 @@ class Settings(object): return False try: - with atomic_write(self._configfile, mode='wb', prefix="octoprint-config-", suffix=".yaml", permissions=0o600, max_permissions=0o666) as configFile: + with atomic_write(self._configfile, mode='wt', prefix="octoprint-config-", suffix=".yaml", permissions=0o600, max_permissions=0o666) as configFile: yaml.safe_dump(self._config, configFile, default_flow_style=False, indent=4, allow_unicode=True) self._dirty = False except Exception: diff --git a/tests/filemanager/test_localstorage.py b/tests/filemanager/test_localstorage.py index 7bbe1a3c2..91e7ee244 100644 --- a/tests/filemanager/test_localstorage.py +++ b/tests/filemanager/test_localstorage.py @@ -653,7 +653,7 @@ class LocalStorageTest(unittest.TestCase): # prepare import yaml - with io.open(yaml_path, 'wb') as f: + with io.open(yaml_path, 'wt') as f: yaml.safe_dump(metadata, f) # migrate diff --git a/tests/settings/test_settings.py b/tests/settings/test_settings.py index 77329bc5e..109d48b5d 100644 --- a/tests/settings/test_settings.py +++ b/tests/settings/test_settings.py @@ -30,11 +30,11 @@ import octoprint.settings class TestSettings(unittest.TestCase): def _load_yaml(self, fname): - with io.open(fname, 'rt+', encoding='utf-8') as f: + with io.open(fname, 'rt', encoding='utf-8') as f: return yaml.safe_load(f) def _dump_yaml(self, fname, config): - with io.open(fname, 'wt+', encoding='utf-8') as f: + with io.open(fname, 'wt', encoding='utf-8') as f: yaml.safe_dump(config, f) def setUp(self):