-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0883bf5
commit 1017e12
Showing
6 changed files
with
57 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import base64 | ||
import io | ||
from urllib.parse import unquote | ||
|
||
from fsspec import AbstractFileSystem | ||
|
||
|
||
class DataFileSystem(AbstractFileSystem): | ||
"""A handy decoder for data-URLs | ||
Example | ||
------- | ||
>>> with fsspec.open("data:,Hello%2C%20World%21") as f: | ||
... print(f.read()) | ||
b"Hello, World!" | ||
""" | ||
|
||
protocol = "data" | ||
|
||
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 _open( | ||
self, | ||
path, | ||
mode="rb", | ||
block_size=None, | ||
autocommit=True, | ||
cache_options=None, | ||
**kwargs, | ||
): | ||
if "r" not in mode: | ||
raise ValueError("Read only filesystem") | ||
return io.BytesIO(self.cat_file(path)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import fsspec | ||
|
||
|
||
def test_1(): | ||
with fsspec.open("data:text/plain;base64,SGVsbG8sIFdvcmxkIQ==") as f: | ||
assert f.read() == b"Hello, World!" | ||
|
||
with fsspec.open("data:,Hello%2C%20World%21") as f: | ||
assert f.read() == b"Hello, World!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters