codekingpro/portable-devtools
115k
1import base642import io3from urllib.parse import unquote4 5from fsspec import AbstractFileSystem6 7 8class DataFileSystem(AbstractFileSystem):9 """A handy decoder for data-URLs10 11 Example12 -------13 >>> with fsspec.open("data:,Hello%2C%20World%21") as f:14 ... print(f.read())15 b"Hello, World!"16 17 See https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URLs18 """19 20 protocol = "data"21 22 def __init__(self, **kwargs):23 """No parameters for this filesystem"""24 super().__init__(**kwargs)25 26 def cat_file(self, path, start=None, end=None, **kwargs):27 pref, data = path.split(",", 1)28 if pref.endswith("base64"):29 return base64.b64decode(data)[start:end]30 return unquote(data).encode()[start:end]31 32 def info(self, path, **kwargs):33 pref, name = path.split(",", 1)34 data = self.cat_file(path)35 mime = pref.split(":", 1)[1].split(";", 1)[0]36 return {"name": name, "size": len(data), "type": "file", "mimetype": mime}37 38 def _open(39 self,40 path,41 mode="rb",42 block_size=None,43 autocommit=True,44 cache_options=None,45 **kwargs,46 ):47 if "r" not in mode:48 raise ValueError("Read only filesystem")49 return io.BytesIO(self.cat_file(path))50 51 @staticmethod52 def encode(data: bytes, mime: str | None = None):53 """Format the given data into data-URL syntax54 55 This version always base64 encodes, even when the data is ascii/url-safe.56 """57 return f"data:{mime or ''};base64,{base64.b64encode(data).decode()}"58 