We need text mode for JSON and YAML
This commit is contained in:
1 parent
f3fdac7e5a
commit
7fcc3551ca
17 files changed
+26
-26
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)))
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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"])
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in new issue
Block a user