Skip to content

Commit

Permalink
allow ReferenceFileSystem to hold dicts, which are treated as JSON fi…
Browse files Browse the repository at this point in the history
…les (#1562)
  • Loading branch information
bendichter authored May 30, 2024
1 parent 59a2d64 commit 463e2ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions fsspec/implementations/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,10 @@ def _process_references(self, references, template_overrides=None):

def _process_references0(self, references):
"""Make reference dict for Spec Version 0"""
references = {
key: json.dumps(val) if isinstance(val, dict) else val
for key, val in references.items()
}
self.references = references

def _process_references1(self, references, template_overrides=None):
Expand Down Expand Up @@ -964,6 +968,8 @@ def _render_jinja(u):
else:
u = _render_jinja(u)
self.references[k] = [u] if len(v) == 1 else [u, v[1], v[2]]
elif isinstance(v, dict):
self.references[k] = json.dumps(v)
else:
self.references[k] = v
self.references.update(self._process_gen(references.get("gen", [])))
Expand Down
25 changes: 25 additions & 0 deletions fsspec/implementations/tests/test_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_simple(server): # noqa: F811
"b": (realfile, 0, 5),
"c": (realfile, 1, 5),
"d": b"base64:aGVsbG8=",
"e": {"key": "value"},
}
h = fsspec.filesystem("http")
fs = fsspec.filesystem("reference", fo=refs, fs=h)
Expand All @@ -27,6 +28,30 @@ def test_simple(server): # noqa: F811
assert fs.cat("b") == data[:5]
assert fs.cat("c") == data[1 : 1 + 5]
assert fs.cat("d") == b"hello"
assert fs.cat("e") == b'{"key": "value"}'
with fs.open("d", "rt") as f:
assert f.read(2) == "he"


def test_simple_ver1(server): # noqa: F811
in_data = {
"version": 1,
"refs": {
"a": b"data",
"b": (realfile, 0, 5),
"c": (realfile, 1, 5),
"d": b"base64:aGVsbG8=",
"e": {"key": "value"},
},
}
h = fsspec.filesystem("http")
fs = fsspec.filesystem("reference", fo=in_data, fs=h)

assert fs.cat("a") == b"data"
assert fs.cat("b") == data[:5]
assert fs.cat("c") == data[1 : 1 + 5]
assert fs.cat("d") == b"hello"
assert fs.cat("e") == b'{"key": "value"}'
with fs.open("d", "rt") as f:
assert f.read(2) == "he"

Expand Down

0 comments on commit 463e2ce

Please sign in to comment.