Spaces:
Runtime error
Runtime error
File size: 6,510 Bytes
63deadc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
import os
import pickle
import platform
import sys
import uuid
import pytest
import fsspec
from fsspec.implementations.local import LocalFileSystem
from fsspec.implementations.memory import MemoryFileSystem
def test_mapping_prefix(tmpdir):
tmpdir = str(tmpdir)
os.makedirs(os.path.join(tmpdir, "afolder"))
open(os.path.join(tmpdir, "afile"), "w").write("test")
open(os.path.join(tmpdir, "afolder", "anotherfile"), "w").write("test2")
m = fsspec.get_mapper(f"file://{tmpdir}")
assert "afile" in m
assert m["afolder/anotherfile"] == b"test2"
fs = fsspec.filesystem("file")
m2 = fs.get_mapper(tmpdir)
m3 = fs.get_mapper(f"file://{tmpdir}")
assert m == m2 == m3
def test_getitems_errors(tmpdir):
tmpdir = str(tmpdir)
os.makedirs(os.path.join(tmpdir, "afolder"))
open(os.path.join(tmpdir, "afile"), "w").write("test")
open(os.path.join(tmpdir, "afolder", "anotherfile"), "w").write("test2")
m = fsspec.get_mapper(f"file://{tmpdir}")
assert m.getitems(["afile", "bfile"], on_error="omit") == {"afile": b"test"}
with pytest.raises(KeyError):
m.getitems(["afile", "bfile"])
out = m.getitems(["afile", "bfile"], on_error="return")
assert isinstance(out["bfile"], KeyError)
m = fsspec.get_mapper(f"file://{tmpdir}", missing_exceptions=())
assert m.getitems(["afile", "bfile"], on_error="omit") == {"afile": b"test"}
with pytest.raises(FileNotFoundError):
m.getitems(["afile", "bfile"])
def test_ops():
MemoryFileSystem.store.clear()
m = fsspec.get_mapper("memory://")
assert not m
assert list(m) == []
with pytest.raises(KeyError):
m["hi"]
assert m.pop("key", 0) == 0
m["key0"] = b"data"
assert list(m) == ["key0"]
assert m["key0"] == b"data"
m.clear()
assert list(m) == []
def test_pickle():
m = fsspec.get_mapper("memory://")
assert isinstance(m.fs, MemoryFileSystem)
m["key"] = b"data"
m2 = pickle.loads(pickle.dumps(m))
assert list(m) == list(m2)
assert m.missing_exceptions == m2.missing_exceptions
def test_keys_view():
# https://github.com/fsspec/filesystem_spec/issues/186
m = fsspec.get_mapper("memory://")
m["key"] = b"data"
keys = m.keys()
assert len(keys) == 1
# check that we don't consume the keys
assert len(keys) == 1
m.clear()
def test_multi():
m = fsspec.get_mapper("memory:///")
data = {"a": b"data1", "b": b"data2"}
m.setitems(data)
assert m.getitems(list(data)) == data
m.delitems(list(data))
assert not list(m)
def test_setitem_types():
import array
m = fsspec.get_mapper("memory://")
m["a"] = array.array("i", [1])
if sys.byteorder == "little":
assert m["a"] == b"\x01\x00\x00\x00"
else:
assert m["a"] == b"\x00\x00\x00\x01"
m["b"] = bytearray(b"123")
assert m["b"] == b"123"
m.setitems({"c": array.array("i", [1]), "d": bytearray(b"123")})
if sys.byteorder == "little":
assert m["c"] == b"\x01\x00\x00\x00"
else:
assert m["c"] == b"\x00\x00\x00\x01"
assert m["d"] == b"123"
def test_setitem_numpy():
m = fsspec.get_mapper("memory://")
np = pytest.importorskip("numpy")
m["c"] = np.array(1, dtype="<i4") # scalar
assert m["c"] == b"\x01\x00\x00\x00"
m["c"] = np.array([1, 2], dtype="<i4") # array
assert m["c"] == b"\x01\x00\x00\x00\x02\x00\x00\x00"
m["c"] = np.array(
np.datetime64("2000-01-01T23:59:59.999999999"), dtype="<M8[ns]"
) # datetime64 scalar
assert m["c"] == b"\xff\xff\x91\xe3c\x9b#\r"
m["c"] = np.array(
[
np.datetime64("1900-01-01T23:59:59.999999999"),
np.datetime64("2000-01-01T23:59:59.999999999"),
],
dtype="<M8[ns]",
) # datetime64 array
assert m["c"] == b"\xff\xff}p\xf8fX\xe1\xff\xff\x91\xe3c\x9b#\r"
m["c"] = np.array(
np.timedelta64(3155673612345678901, "ns"), dtype="<m8[ns]"
) # timedelta64 scalar
assert m["c"] == b"5\x1c\xf0Rn4\xcb+"
m["c"] = np.array(
[
np.timedelta64(450810516049382700, "ns"),
np.timedelta64(3155673612345678901, "ns"),
],
dtype="<m8[ns]",
) # timedelta64 scalar
assert m["c"] == b',M"\x9e\xc6\x99A\x065\x1c\xf0Rn4\xcb+'
def test_empty_url():
m = fsspec.get_mapper()
assert isinstance(m.fs, LocalFileSystem)
def test_fsmap_access_with_root_prefix(tmp_path):
# "/a" and "a" are the same for LocalFileSystem
tmp_path.joinpath("a").write_bytes(b"data")
m = fsspec.get_mapper(f"file://{tmp_path}")
assert m["/a"] == m["a"] == b"data"
# "/a" and "a" differ for MemoryFileSystem
m = fsspec.get_mapper(f"memory://{uuid.uuid4()}")
m["/a"] = b"data"
assert m["/a"] == b"data"
with pytest.raises(KeyError):
_ = m["a"]
@pytest.mark.parametrize(
"key",
[
pytest.param(b"k", id="bytes"),
pytest.param(1234, id="int"),
pytest.param((1,), id="tuple"),
pytest.param([""], id="list"),
],
)
def test_fsmap_non_str_keys(key):
m = fsspec.get_mapper()
# Once the deprecation period passes
# FSMap.__getitem__ should raise TypeError for non-str keys
# with pytest.raises(TypeError):
# _ = m[key]
with pytest.warns(DeprecationWarning):
with pytest.raises(KeyError):
_ = m[key]
def test_fsmap_error_on_protocol_keys():
root = uuid.uuid4()
m = fsspec.get_mapper(f"memory://{root}", create=True)
m["a"] = b"data"
assert m["a"] == b"data"
with pytest.raises(KeyError):
_ = m[f"memory://{root}/a"]
def test_fsmap_access_with_suffix(tmp_path):
tmp_path.joinpath("b").mkdir()
tmp_path.joinpath("b", "a").write_bytes(b"data")
if platform.system() == "Windows":
# on Windows opening a directory will raise PermissionError
# see: https://bugs.python.org/issue43095
missing_exceptions = (
FileNotFoundError,
IsADirectoryError,
NotADirectoryError,
PermissionError,
)
else:
missing_exceptions = None
m = fsspec.get_mapper(f"file://{tmp_path}", missing_exceptions=missing_exceptions)
with pytest.raises(KeyError):
_ = m["b/"]
assert m["b/a/"] == b"data"
def test_fsmap_dirfs():
m = fsspec.get_mapper("memory://")
fs = m.dirfs
assert isinstance(fs, fsspec.implementations.dirfs.DirFileSystem)
assert fs.path == m.root
|