import json import os import pytest import fsspec from fsspec.implementations.local import LocalFileSystem from fsspec.implementations.reference import ( LazyReferenceMapper, ReferenceFileSystem, ReferenceNotReachable, ) from fsspec.tests.conftest import data, realfile, reset_files, server, win # noqa: F401 def test_simple(server): # noqa: F811 refs = { "a": b"data", "b": (realfile, 0, 5), "c": (realfile, 1, 5), "d": b"base64:aGVsbG8=", } h = fsspec.filesystem("http") fs = fsspec.filesystem("reference", fo=refs, 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" with fs.open("d", "rt") as f: assert f.read(2) == "he" def test_target_options(m): m.pipe("data/0", b"hello") refs = {"a": ["memory://data/0"]} fn = "memory://refs.json.gz" with fsspec.open(fn, "wt", compression="gzip") as f: json.dump(refs, f) fs = fsspec.filesystem("reference", fo=fn, target_options={"compression": "gzip"}) assert fs.cat("a") == b"hello" def test_ls(server): # noqa: F811 refs = {"a": b"data", "b": (realfile, 0, 5), "c/d": (realfile, 1, 6)} h = fsspec.filesystem("http") fs = fsspec.filesystem("reference", fo=refs, fs=h) assert fs.ls("", detail=False) == ["a", "b", "c"] assert {"name": "c", "type": "directory", "size": 0} in fs.ls("", detail=True) assert fs.find("") == ["a", "b", "c/d"] assert fs.find("", withdirs=True) == ["a", "b", "c", "c/d"] assert fs.find("c", detail=True) == { "c/d": {"name": "c/d", "size": 6, "type": "file"} } def test_nested_dirs_ls(): # issue #1430 refs = {"a": "A", "B/C/b": "B", "B/C/d": "d", "B/_": "_"} fs = fsspec.filesystem("reference", fo=refs) assert len(fs.ls("")) == 2 assert {e["name"] for e in fs.ls("")} == {"a", "B"} assert len(fs.ls("B")) == 2 assert {e["name"] for e in fs.ls("B")} == {"B/C", "B/_"} def test_info(server): # noqa: F811 refs = { "a": b"data", "b": (realfile, 0, 5), "c/d": (realfile, 1, 6), "e": (realfile,), } h = fsspec.filesystem("http", headers={"give_length": "true", "head_ok": "true"}) fs = fsspec.filesystem("reference", fo=refs, fs=h) assert fs.size("a") == 4 assert fs.size("b") == 5 assert fs.size("c/d") == 6 assert fs.info("e")["size"] == len(data) def test_mutable(server, m): refs = { "a": b"data", "b": (realfile, 0, 5), "c/d": (realfile, 1, 6), "e": (realfile,), } h = fsspec.filesystem("http", headers={"give_length": "true", "head_ok": "true"}) fs = fsspec.filesystem("reference", fo=refs, fs=h) fs.rm("a") assert not fs.exists("a") bin_data = b"bin data" fs.pipe("aa", bin_data) assert fs.cat("aa") == bin_data fs.save_json("memory://refs.json") assert m.exists("refs.json") fs = fsspec.filesystem("reference", fo="memory://refs.json", remote_protocol="http") assert not fs.exists("a") assert fs.cat("aa") == bin_data def test_put_get(tmpdir): d1 = f"{tmpdir}/d1" os.mkdir(d1) with open(f"{d1}/a", "wb") as f: f.write(b"1") with open(f"{d1}/b", "wb") as f: f.write(b"2") d2 = f"{tmpdir}/d2" fs = fsspec.filesystem("reference", fo={}, remote_protocol="file") fs.put(d1, "out", recursive=True) fs.get("out", d2, recursive=True) assert open(f"{d2}/a", "rb").read() == b"1" assert open(f"{d2}/b", "rb").read() == b"2" def test_put_get_single(tmpdir): d1 = f"{tmpdir}/f1" d2 = f"{tmpdir}/f2" with open(d1, "wb") as f: f.write(b"1") # skip instance cache since this is the same kwargs as previous test fs = fsspec.filesystem( "reference", fo={}, remote_protocol="file", skip_instance_cache=True ) fs.put_file(d1, "out") fs.get_file("out", d2) assert open(d2, "rb").read() == b"1" fs.pipe({"hi": b"data"}) assert fs.cat("hi") == b"data" def test_defaults(server): # noqa: F811 refs = {"a": b"data", "b": (None, 0, 5)} fs = fsspec.filesystem( "reference", fo=refs, target_protocol="http", target=realfile, remote_protocol="http", ) assert fs.cat("a") == b"data" assert fs.cat("b") == data[:5] jdata = """{ "metadata": { ".zattrs": { "Conventions": "UGRID-0.9.0" }, ".zgroup": { "zarr_format": 2 }, "adcirc_mesh/.zarray": { "chunks": [ 1 ], "dtype": "