diff --git a/exdir/core/attribute.py b/exdir/core/attribute.py index 479b70d..b528b86 100644 --- a/exdir/core/attribute.py +++ b/exdir/core/attribute.py @@ -166,12 +166,9 @@ def _set_data(self, attrs): attribute_data_quoted = attrs with self.filename.open("w", encoding="utf-8") as attribute_file: - yaml.dump( + yaml.YAML(typ="rt", pure=True).dump( attribute_data_quoted, attribute_file, - default_flow_style=False, - allow_unicode=True, - Dumper=yaml.RoundTripDumper ) # TODO only needs filename, make into free function @@ -180,7 +177,7 @@ def _open_or_create(self): attrs = {} if self.filename.exists(): # NOTE str for Python 3.5 support with self.filename.open("r", encoding="utf-8") as meta_file: - attrs = yaml.safe_load(meta_file) + attrs = yaml.YAML(typ="safe", pure=True).load(meta_file) return attrs def __iter__(self): diff --git a/exdir/core/exdir_object.py b/exdir/core/exdir_object.py index a4b4bcf..3f42037 100644 --- a/exdir/core/exdir_object.py +++ b/exdir/core/exdir_object.py @@ -62,7 +62,10 @@ def _create_object_directory(directory, metadata): version=1 ) else: - metadata_string = yaml.dump(metadata) + from io import StringIO + with StringIO() as buf: + yaml.YAML(typ="safe", pure=True).dump(metadata, buf) + metadata_string = buf.getvalue() try: meta_file.write(metadata_string) @@ -103,7 +106,7 @@ def is_nonraw_object_directory(directory): if not meta_filename.exists(): return False with meta_filename.open("r", encoding="utf-8") as meta_file: - meta_data = yaml.safe_load(meta_file) + meta_data = yaml.YAML(typ="safe", pure=True).load(meta_file) if not isinstance(meta_data, dict): return False @@ -140,7 +143,7 @@ def root_directory(path): meta_filename = path / META_FILENAME with meta_filename.open("r", encoding="utf-8") as meta_file: - meta_data = yaml.safe_load(meta_file) + meta_data = yaml.YAML(typ="safe", pure=True).load(meta_file) if EXDIR_METANAME not in meta_data: path = path.parent continue diff --git a/exdir/core/group.py b/exdir/core/group.py index 239e419..51c135a 100644 --- a/exdir/core/group.py +++ b/exdir/core/group.py @@ -402,7 +402,7 @@ def __getitem__(self, name): meta_filename = directory / exob.META_FILENAME with meta_filename.open("r", encoding="utf-8") as meta_file: - meta_data = yaml.safe_load(meta_file) + meta_data = yaml.YAML(typ="safe", pure=True).load(meta_file) if meta_data[exob.EXDIR_METANAME][exob.TYPE_METANAME] == exob.DATASET_TYPENAME: return self._dataset(name) elif meta_data[exob.EXDIR_METANAME][exob.TYPE_METANAME] == exob.GROUP_TYPENAME: diff --git a/tests/test_help_functions.py b/tests/test_help_functions.py index dffec01..2a79942 100644 --- a/tests/test_help_functions.py +++ b/tests/test_help_functions.py @@ -114,7 +114,7 @@ def test_create_object_directory(setup_teardown_folder): } with file_path.open("r", encoding="utf-8") as meta_file: - metadata = yaml.safe_load(meta_file) + metadata = yaml.YAML(typ="safe", pure=True).load(meta_file) assert metadata == compare_metadata @@ -141,10 +141,9 @@ def test_is_nonraw_object_directory(setup_teardown_folder): exob.EXDIR_METANAME: { exob.VERSION_METANAME: 1} } - yaml.safe_dump(metadata, - meta_file, - default_flow_style=False, - allow_unicode=True) + yaml.YAML(typ="safe", pure=True).dump( + metadata, + meta_file) result = exob.is_nonraw_object_directory(setup_teardown_folder[2]) assert result is False @@ -156,10 +155,9 @@ def test_is_nonraw_object_directory(setup_teardown_folder): exob.TYPE_METANAME: "wrong_typename", exob.VERSION_METANAME: 1} } - yaml.safe_dump(metadata, - meta_file, - default_flow_style=False, - allow_unicode=True) + yaml.YAML(typ="safe", pure=True).dump( + metadata, + meta_file) result = exob.is_nonraw_object_directory(setup_teardown_folder[2]) assert result is False @@ -171,10 +169,9 @@ def test_is_nonraw_object_directory(setup_teardown_folder): exob.TYPE_METANAME: exob.DATASET_TYPENAME, exob.VERSION_METANAME: 1} } - yaml.safe_dump(metadata, - meta_file, - default_flow_style=False, - allow_unicode=True) + yaml.YAML(typ="safe", pure=True).dump( + metadata, + meta_file) result = exob.is_nonraw_object_directory(setup_teardown_folder[2]) assert result is True diff --git a/tests/test_object.py b/tests/test_object.py index 31f7859..edeed91 100644 --- a/tests/test_object.py +++ b/tests/test_object.py @@ -60,7 +60,7 @@ def test_object_attrs(setup_teardown_file): obj.attrs = "test value" with (setup_teardown_file[1] / "test_object" / ATTRIBUTES_FILENAME).open("r", encoding="utf-8") as meta_file: - meta_data = yaml.safe_load(meta_file) + meta_data = yaml.YAML(typ="safe", pure=True).load(meta_file) assert meta_data == "test value"