Skip to content

Commit

Permalink
deprecate asdf.util.filepath_to_url
Browse files Browse the repository at this point in the history
  • Loading branch information
braingram committed Jan 17, 2024
1 parent c0eae06 commit b535285
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 25 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------

Expand Down
4 changes: 3 additions & 1 deletion asdf/_tests/_block/test_external.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import numpy as np
import pytest

Expand All @@ -11,7 +13,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()}{os.sep}"
data = cache.load(base_uri, "test.asdf")
np.testing.assert_array_equal(data, arr)
assert cache.load(base_uri, "test.asdf") is data
Expand Down
5 changes: 5 additions & 0 deletions asdf/_tests/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
22 changes: 11 additions & 11 deletions asdf/_tests/test_generic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -116,21 +116,21 @@ 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():
# Must open with mode=rw in order to get memmapped data
# 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:
Expand All @@ -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")
Expand All @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions asdf/_tests/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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: .*"):
Expand All @@ -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: .*"):
Expand All @@ -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: .*"):
Expand Down Expand Up @@ -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": "#"}}

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion asdf/generic_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions asdf/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
2 changes: 2 additions & 0 deletions docs/asdf/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
===========

Expand Down
8 changes: 3 additions & 5 deletions pytest_asdf/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -225,17 +225,15 @@ 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,
)

# 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:
Expand Down

0 comments on commit b535285

Please sign in to comment.