Skip to content

Commit c7f763f

Browse files
committed
Add prefix on open
1 parent 6afff88 commit c7f763f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

fsspec/implementations/prefix.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,15 @@
44
import fsspec
55
from fsspec import AbstractFileSystem
66
from fsspec.core import split_protocol
7+
from fsspec.spec import AbstractBufferedFile
78
from fsspec.utils import stringify_path
89

910

11+
class PrefixBufferedFile(AbstractBufferedFile):
12+
def _fetch_range(self, start, end):
13+
pass
14+
15+
1016
class PrefixFileSystem(AbstractFileSystem):
1117
def __init__(
1218
self,
@@ -148,3 +154,10 @@ def cat(
148154

149155
def __repr__(self) -> str:
150156
return f"{self.__class__.__qualname__}(prefix='{self.prefix}', filesystem={self.filesystem})"
157+
158+
def open(
159+
self,
160+
path,
161+
**kwargs,
162+
):
163+
return self.filesystem.open(self._add_fs_prefix(path), **kwargs)

fsspec/implementations/tests/test_prefix.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os.path
55
import tempfile
66
from contextlib import contextmanager
7+
from pathlib import Path
78

89
import pytest
910

@@ -29,8 +30,9 @@
2930

3031

3132
csv_files = {
32-
".test.fakedata.1.csv": (b"a,b\n" b"1,2\n"),
33-
".test.fakedata.2.csv": (b"a,b\n" b"3,4\n"),
33+
".test.fakedata.1.csv": b"a,b\n1,2\n",
34+
".test.fakedata.2.csv": b"a,b\n3,4\n",
35+
"a/b/c/.test.fakedata.3.csv": b"a,b\n3,4,5\n",
3436
}
3537
odir = os.getcwd()
3638

@@ -50,6 +52,11 @@ def filetexts(d, open=open, mode="t"):
5052
try:
5153
os.chdir(dirname)
5254
for filename, text in d.items():
55+
filename = Path(filename)
56+
57+
if not filename.parent.exists():
58+
filename.parent.mkdir(parents=True, exist_ok=True)
59+
5360
f = open(filename, "w" + mode)
5461
try:
5562
f.write(text)
@@ -71,6 +78,13 @@ def filetexts(d, open=open, mode="t"):
7178
os.chdir(odir)
7279

7380

81+
def test_open():
82+
with filetexts(csv_files, mode="b"):
83+
fs = PrefixFileSystem(prefix="a", filesystem=fsspec.filesystem("file"))
84+
with fs.open("b/c/.test.fakedata.3.csv") as f:
85+
assert f.read() == b"a,b\n3,4,5\n"
86+
87+
7488
def test_cats():
7589
with filetexts(csv_files, mode="b"):
7690
fs = PrefixFileSystem(prefix=".", filesystem=fsspec.filesystem("file"))

0 commit comments

Comments
 (0)