Skip to content

Commit 08d1e49

Browse files
authored
Cope with zarr3 Buffers in referenceFS (fsspec#1784)
1 parent fe59f48 commit 08d1e49

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

fsspec/implementations/reference.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,14 @@ def __setitem__(self, key, value):
394394
self.write(field, record)
395395
else:
396396
# metadata or top-level
397-
self._items[key] = value
398-
new_value = json.loads(
399-
value.decode() if isinstance(value, bytes) else value
400-
)
397+
if hasattr(value, "to_bytes"):
398+
val = value.to_bytes().decode()
399+
elif isinstance(value, bytes):
400+
val = value.decode()
401+
else:
402+
val = value
403+
self._items[key] = val
404+
new_value = json.loads(val)
401405
self.zmetadata[key] = {**self.zmetadata.get(key, {}), **new_value}
402406

403407
@staticmethod
@@ -606,6 +610,7 @@ class ReferenceFileSystem(AsyncFileSystem):
606610
"""
607611

608612
protocol = "reference"
613+
cachable = False
609614

610615
def __init__(
611616
self,
@@ -762,6 +767,11 @@ def __init__(
762767
for k, f in self.fss.items():
763768
if not f.async_impl:
764769
self.fss[k] = AsyncFileSystemWrapper(f)
770+
elif self.asynchronous ^ f.asynchronous:
771+
raise ValueError(
772+
"Reference-FS's target filesystem must have same value"
773+
"of asynchronous"
774+
)
765775

766776
def _cat_common(self, path, start=None, end=None):
767777
path = self._strip_protocol(path)
@@ -772,6 +782,8 @@ def _cat_common(self, path, start=None, end=None):
772782
raise FileNotFoundError(path) from exc
773783
if isinstance(part, str):
774784
part = part.encode()
785+
if hasattr(part, "to_bytes"):
786+
part = part.to_bytes()
775787
if isinstance(part, bytes):
776788
logger.debug(f"Reference: {path}, type bytes")
777789
if part.startswith(b"base64:"):
@@ -1073,7 +1085,7 @@ def _dircache_from_items(self):
10731085
self.dircache = {"": []}
10741086
it = self.references.items()
10751087
for path, part in it:
1076-
if isinstance(part, (bytes, str)):
1088+
if isinstance(part, (bytes, str)) or hasattr(part, "to_bytes"):
10771089
size = len(part)
10781090
elif len(part) == 1:
10791091
size = None
@@ -1104,6 +1116,7 @@ def _open(self, path, mode="rb", block_size=None, cache_options=None, **kwargs):
11041116
return io.BytesIO(data)
11051117

11061118
def ls(self, path, detail=True, **kwargs):
1119+
logger.debug("list %s", path)
11071120
path = self._strip_protocol(path)
11081121
if isinstance(self.references, LazyReferenceMapper):
11091122
try:

0 commit comments

Comments
 (0)