Skip to content

Commit

Permalink
Doc and info for dataFS (#1421)
Browse files Browse the repository at this point in the history
  • Loading branch information
martindurant authored Nov 9, 2023
1 parent 4f70f1b commit 453d8c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions fsspec/implementations/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,22 @@ class DataFileSystem(AbstractFileSystem):

protocol = "data"

def __init__(self, **kwargs):
"""No parameters for this filesystem"""
super().__init__(**kwargs)

def cat_file(self, path, start=None, end=None, **kwargs):
pref, data = path.split(",", 1)
if pref.endswith("base64"):
return base64.b64decode(data)[start:end]
return unquote(data).encode()[start:end]

def info(self, path, **kwargs):
pref, name = path.split(",", 1)
data = self.cat_file(path)
mime = pref.split(":", 1)[1].split(";", 1)[0]
return {"name": name, "size": len(data), "type": "file", "mimetype": mime}

def _open(
self,
path,
Expand Down
11 changes: 11 additions & 0 deletions fsspec/implementations/tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ def test_1():

with fsspec.open("data:,Hello%2C%20World%21") as f:
assert f.read() == b"Hello, World!"


def test_info():
fs = fsspec.filesystem("data")
info = fs.info("data:text/html,%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E")
assert info == {
"name": "%3Ch1%3EHello%2C%20World%21%3C%2Fh1%3E",
"size": 22,
"type": "file",
"mimetype": "text/html",
}

0 comments on commit 453d8c5

Please sign in to comment.