Skip to content

Commit 5477bd2

Browse files
committed
add relativize paths util, and use it to fix chunks_initialized
1 parent d1e805f commit 5477bd2

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/zarr/core/array.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
get_pipeline_class,
115115
)
116116
from zarr.storage._common import StorePath, ensure_no_existing_node, make_store_path
117+
from zarr.storage._utils import _relativize_path
117118

118119
if TYPE_CHECKING:
119120
from collections.abc import Iterator, Sequence
@@ -3727,7 +3728,12 @@ async def chunks_initialized(
37273728
store_contents = [
37283729
x async for x in array.store_path.store.list_prefix(prefix=array.store_path.path)
37293730
]
3730-
return tuple(chunk_key for chunk_key in array._iter_chunk_keys() if chunk_key in store_contents)
3731+
store_contents_relative = [
3732+
_relativize_path(key, array.store_path.path) for key in store_contents
3733+
]
3734+
return tuple(
3735+
chunk_key for chunk_key in array._iter_chunk_keys() if chunk_key in store_contents_relative
3736+
)
37313737

37323738

37333739
def _build_parents(

src/zarr/storage/_utils.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,60 @@ def _join_paths(paths: Iterable[str]) -> str:
7474
"""
7575
Filter out instances of '' and join the remaining strings with '/'.
7676
77-
Because the root node of a zarr hierarchy is represented by an empty string,
77+
Parameters
78+
----------
79+
paths : Iterable[str]
80+
81+
Returns
82+
-------
83+
str
84+
85+
Examples
86+
--------
87+
>>> _join_paths(["", "a", "b"])
88+
'a/b'
89+
>>> _join_paths(["a", "b", "c"])
90+
'a/b/c'
7891
"""
7992
return "/".join(filter(lambda v: v != "", paths))
8093

8194

95+
def _relativize_path(path: str, prefix: str) -> str:
96+
"""
97+
Make a "\"-delimited path relative to some prefix. If the prefix is '', then the path is
98+
returned as-is. Otherwise, the prefix is removed from the path as well as the separator
99+
string "\".
100+
101+
If ``prefix`` is not the empty string and``path`` does not start with ``prefix``
102+
followed by a "/" character, then an error is raised.
103+
104+
Parameters
105+
----------
106+
path : str
107+
The path to make relative to the prefix.
108+
prefix : str
109+
The prefix to make relative to.
110+
111+
Returns
112+
-------
113+
str
114+
115+
Examples
116+
--------
117+
>>> _relativize_paths("", "a/b")
118+
'a/b'
119+
>>> _relativize_paths("a/b", "a/b/c")
120+
'c'
121+
"""
122+
if prefix == "":
123+
return path
124+
else:
125+
_prefix = prefix + "/"
126+
if not path.startswith(_prefix):
127+
raise ValueError(f"The first component of {path} does not start with {prefix}.")
128+
return path.removeprefix(f"{prefix}/")
129+
130+
82131
def _normalize_paths(paths: Iterable[str]) -> tuple[str, ...]:
83132
"""
84133
Normalize the input paths according to the normalization scheme used for zarr node paths.

0 commit comments

Comments
 (0)