From 71795ba238c5235e155286bc36deba02a70f9d1d Mon Sep 17 00:00:00 2001 From: Brett Date: Wed, 17 Jan 2024 14:35:52 -0500 Subject: [PATCH] deprecate asdf.util.filepath_to_url --- CHANGES.rst | 2 ++ asdf/_tests/_block/test_external.py | 2 +- asdf/_tests/test_deprecated.py | 5 +++++ asdf/_tests/test_generic_io.py | 22 +++++++++++----------- asdf/_tests/test_reference.py | 14 +++++++------- asdf/generic_io.py | 2 +- asdf/util.py | 2 ++ docs/asdf/deprecations.rst | 2 ++ pytest_asdf/plugin.py | 8 +++----- 9 files changed, 34 insertions(+), 25 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index f59a27eaa..9067dcb60 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -25,6 +25,8 @@ The ASDF Standard is at v1.6.0 of an ASDF file (with the option ``tagged`` to load the contents as a tree of ``asdf.tagged.Tagged`` instances to preserve tags) [#1700] +- Deprecate ``asdf.util.filepath_to_url`` use ``pathlib.Path.to_uri`` [#1735] + 3.0.1 (2023-10-30) ------------------ diff --git a/asdf/_tests/_block/test_external.py b/asdf/_tests/_block/test_external.py index a7cb6f9c5..f407ec2b6 100644 --- a/asdf/_tests/_block/test_external.py +++ b/asdf/_tests/_block/test_external.py @@ -11,7 +11,7 @@ def test_cache(tmp_path): asdf.AsdfFile({"data": arr}).write_to(efn) cache = external.ExternalBlockCache() - base_uri = asdf.util.filepath_to_url(f"{tmp_path}/") + base_uri = f"{tmp_path.as_uri()}/" data = cache.load(base_uri, "test.asdf") np.testing.assert_array_equal(data, arr) assert cache.load(base_uri, "test.asdf") is data diff --git a/asdf/_tests/test_deprecated.py b/asdf/_tests/test_deprecated.py index 5a749d291..727a93d6d 100644 --- a/asdf/_tests/test_deprecated.py +++ b/asdf/_tests/test_deprecated.py @@ -72,3 +72,8 @@ def test_find_references_during_open_deprecation(tmp_path): def test_asdf_util_is_primitive_deprecation(): with pytest.warns(AsdfDeprecationWarning, match="asdf.util.is_primitive is deprecated"): asdf.util.is_primitive(1) + + +def test_asdf_util_filepath_to_url_deprecation(tmp_path): + with pytest.warns(AsdfDeprecationWarning, match="asdf.util.filepath_to_url is deprecated"): + asdf.util.filepath_to_url(str(tmp_path)) diff --git a/asdf/_tests/test_generic_io.py b/asdf/_tests/test_generic_io.py index 921305950..5eae23811 100644 --- a/asdf/_tests/test_generic_io.py +++ b/asdf/_tests/test_generic_io.py @@ -9,7 +9,7 @@ import pytest import asdf -from asdf import exceptions, generic_io, util +from asdf import exceptions, generic_io from asdf.config import config_context from . import _helpers as helpers @@ -91,19 +91,19 @@ def test_open(tmp_path, small_tree): def test_path(tree, tmp_path): - path = os.path.join(str(tmp_path), "test.asdf") + path = tmp_path / "test.asdf" def get_write_fd(): f = generic_io.get_file(path, mode="w") assert isinstance(f, generic_io.RealFile) - assert f._uri == util.filepath_to_url(path) + assert f._uri == path.as_uri() return f def get_read_fd(): # Must open with mode=rw in order to get memmapped data f = generic_io.get_file(path, mode="rw") assert isinstance(f, generic_io.RealFile) - assert f._uri == util.filepath_to_url(path) + assert f._uri == path.as_uri() # This is to check for a "feature" in Python 3.x that reading zero # bytes from a socket causes it to stop. We have code in generic_io.py # to workaround it. @@ -116,13 +116,13 @@ def get_read_fd(): def test_open2(tree, tmp_path): - path = os.path.join(str(tmp_path), "test.asdf") + path = tmp_path / "test.asdf" def get_write_fd(): # cannot use context manager here because it closes the file f = generic_io.get_file(open(path, "wb"), mode="w", close=True) assert isinstance(f, generic_io.RealFile) - assert f._uri == util.filepath_to_url(path) + assert f._uri == path.as_uri() return f def get_read_fd(): @@ -130,7 +130,7 @@ def get_read_fd(): # cannot use context manager here because it closes the file f = generic_io.get_file(open(path, "r+b"), mode="rw", close=True) assert isinstance(f, generic_io.RealFile) - assert f._uri == util.filepath_to_url(path) + assert f._uri == path.as_uri() return f with _roundtrip(tree, get_write_fd, get_read_fd) as ff: @@ -140,7 +140,7 @@ def get_read_fd(): @pytest.mark.parametrize("mode", ["r", "w", "rw"]) def test_open_not_binary_fail(tmp_path, mode): - path = os.path.join(str(tmp_path), "test.asdf") + path = tmp_path / "test.asdf" with open(path, "w") as fd: fd.write("\n\n\n") @@ -154,20 +154,20 @@ def test_open_not_binary_fail(tmp_path, mode): def test_io_open(tree, tmp_path): - path = os.path.join(str(tmp_path), "test.asdf") + path = tmp_path / "test.asdf" def get_write_fd(): # cannot use context manager here because it closes the file f = generic_io.get_file(open(path, "wb"), mode="w", close=True) assert isinstance(f, generic_io.RealFile) - assert f._uri == util.filepath_to_url(path) + assert f._uri == path.as_uri() return f def get_read_fd(): # cannot use context manager here because it closes the file f = generic_io.get_file(open(path, "r+b"), mode="rw", close=True) assert isinstance(f, generic_io.RealFile) - assert f._uri == util.filepath_to_url(path) + assert f._uri == path.as_uri() return f with _roundtrip(tree, get_write_fd, get_read_fd) as ff: diff --git a/asdf/_tests/test_reference.py b/asdf/_tests/test_reference.py index 1b733db81..5a06d47b7 100644 --- a/asdf/_tests/test_reference.py +++ b/asdf/_tests/test_reference.py @@ -6,7 +6,7 @@ from numpy.testing import assert_array_equal import asdf -from asdf import reference, util +from asdf import reference from asdf.exceptions import AsdfDeprecationWarning from asdf.tags.core import ndarray @@ -79,7 +79,7 @@ def do_asserts(ff): assert_array_equal(ff.tree["internal"], exttree["cool_stuff"]["a"]) - with asdf.AsdfFile({}, uri=util.filepath_to_url(os.path.join(str(tmp_path), "main.asdf"))) as ff: + with asdf.AsdfFile({}, uri=(tmp_path / "main.asdf").as_uri()) as ff: # avoid passing tree to AsdfFile to avoid the deprecation warning, this can be updated # when automatic find_references on AsdfFile.__init__ is removed ff.tree = tree @@ -143,7 +143,7 @@ def test_external_reference_invalid(tmp_path): # avoid passing tree to AsdfFile to avoid the deprecation warning, this can be updated # when automatic find_references on AsdfFile.__init__ is removed - ff = asdf.AsdfFile({}, uri=util.filepath_to_url(os.path.join(str(tmp_path), "main.asdf"))) + ff = asdf.AsdfFile({}, uri=(tmp_path / "main.asdf").as_uri()) ff.tree = tree ff.find_references() with pytest.raises(IOError, match=r"No such file or directory: .*"): @@ -160,7 +160,7 @@ def test_external_reference_invalid_fragment(tmp_path): # avoid passing tree to AsdfFile to avoid the deprecation warning, this can be updated # when automatic find_references on AsdfFile.__init__ is removed - with asdf.AsdfFile({}, uri=util.filepath_to_url(os.path.join(str(tmp_path), "main.asdf"))) as ff: + with asdf.AsdfFile({}, uri=(tmp_path / "main.asdf").as_uri()) as ff: ff.tree = tree ff.find_references() with pytest.raises(ValueError, match=r"Unresolvable reference: .*"): @@ -170,7 +170,7 @@ def test_external_reference_invalid_fragment(tmp_path): # avoid passing tree to AsdfFile to avoid the deprecation warning, this can be updated # when automatic find_references on AsdfFile.__init__ is removed - with asdf.AsdfFile({}, uri=util.filepath_to_url(os.path.join(str(tmp_path), "main.asdf"))) as ff: + with asdf.AsdfFile({}, uri=(tmp_path / "main.asdf").as_uri()) as ff: ff.tree = tree ff.find_references() with pytest.raises(ValueError, match=r"Unresolvable reference: .*"): @@ -203,7 +203,7 @@ def test_make_reference(tmp_path): def test_internal_reference(tmp_path): - testfile = os.path.join(str(tmp_path), "test.asdf") + testfile = tmp_path / "test.asdf" tree = {"foo": 2, "bar": {"$ref": "#"}} @@ -215,7 +215,7 @@ def test_internal_reference(tmp_path): assert ff.tree["bar"]["foo"] == 2 tree = {"foo": 2} - ff = asdf.AsdfFile(tree, uri=util.filepath_to_url(os.path.abspath(testfile))) + ff = asdf.AsdfFile(tree, uri=testfile.as_uri()) ff.tree["bar"] = ff.make_reference([]) buff = io.BytesIO() ff.write_to(buff) diff --git a/asdf/generic_io.py b/asdf/generic_io.py index 5edc6b44d..d7d04431e 100644 --- a/asdf/generic_io.py +++ b/asdf/generic_io.py @@ -754,7 +754,7 @@ def __init__(self, fd, mode, close=False, uri=None): super().__init__(fd, mode, close=close, uri=uri) if uri is None and hasattr(fd, "name") and isinstance(fd.name, str): - self._uri = util.filepath_to_url(os.path.abspath(fd.name)) + self._uri = pathlib.Path(fd.name).absolute().as_uri() def write_array(self, arr): if isinstance(arr, np.memmap) and getattr(arr, "fd", None) is self: diff --git a/asdf/util.py b/asdf/util.py index bca67ea3c..81ae96f2d 100644 --- a/asdf/util.py +++ b/asdf/util.py @@ -155,6 +155,8 @@ def filepath_to_url(path): """ For a given local file path, return a file:// url. """ + msg = "asdf.util.filepath_to_url is deprecated. Please use pathlib.Path.as_uri" + warnings.warn(msg, exceptions.AsdfDeprecationWarning) return _patched_urllib_parse.urljoin("file:", pathname2url(path)) diff --git a/docs/asdf/deprecations.rst b/docs/asdf/deprecations.rst index 547ac9296..1071c7d8c 100644 --- a/docs/asdf/deprecations.rst +++ b/docs/asdf/deprecations.rst @@ -20,6 +20,8 @@ Automatic calling of ``AsdfFile.find_references`` during calls to ``AsdfFile.__init__`` and ``asdf.open``. Call ``AsdfFile.find_references`` to find references. +``asdf.util.filepath_to_url`` is deprecated. Please use ``pathlib.Path.to_uri``. + Version 3.0 =========== diff --git a/pytest_asdf/plugin.py b/pytest_asdf/plugin.py index f556cd5bd..5eafeda14 100644 --- a/pytest_asdf/plugin.py +++ b/pytest_asdf/plugin.py @@ -216,7 +216,7 @@ def from_parent( return result def runtest(self): - from asdf import AsdfFile, _block, generic_io, util + from asdf import AsdfFile, _block, generic_io from asdf._tests import _helpers as helpers from asdf.exceptions import AsdfDeprecationWarning @@ -225,7 +225,7 @@ def runtest(self): buff = helpers.yaml_to_asdf("example: " + self.example.example.strip(), standard_version=self.example.version) ff = AsdfFile( - uri=util.filepath_to_url(os.path.abspath(self.filename)), + uri=pathlib.Path(self.filename).absolute().as_uri(), ignore_unrecognized_tag=self.ignore_unrecognized_tag, ignore_version_mismatch=self.ignore_version_mismatch, ) @@ -233,9 +233,7 @@ def runtest(self): # Fake an external file ff2 = AsdfFile({"data": np.empty((1024 * 1024 * 8), dtype=np.uint8)}) - ff._external_asdf_by_uri[ - util.filepath_to_url(os.path.abspath(os.path.join(os.path.dirname(self.filename), "external.asdf"))) - ] = ff2 + ff._external_asdf_by_uri[(pathlib.Path(self.filename).absolute().parent / "external.asdf").as_uri()] = ff2 wb = _block.writer.WriteBlock(np.zeros(1024 * 1024 * 8, dtype=np.uint8)) with generic_io.get_file(buff, mode="rw") as f: