class MemoryFileSystem(BaseFileSystem): scheme = "local" PARAM_CHECKSUM = "md5" def __init__(self, **kwargs): from fsspec.implementations.memory import MemoryFileSystem as MemFS super().__init__(**kwargs) self.fs = MemFS() def exists(self, path_info) -> bool: return self.fs.exists(path_info.fspath) def open(self, path_info, mode="r", encoding=None, **kwargs): return self.fs.open(path_info.fspath, mode=mode, encoding=encoding, **kwargs) def info(self, path_info): return self.fs.info(path_info.fspath) def stat(self, path_info): import os info = self.fs.info(path_info.fspath) return os.stat_result((0, 0, 0, 0, 0, 0, info["size"], 0, 0, 0)) def walk_files(self, path_info, **kwargs): raise NotImplementedError
def test_move(): fs = MemoryFileSystem() with fs.open("/myfile", "wb") as f: f.write(b"I had a nice big cabbage") fs.move("/myfile", "/otherfile") assert not fs.exists("/myfile") assert fs.info("/otherfile") assert isinstance(fs.ukey("/otherfile"), str)
def test_move(): fs = MemoryFileSystem() with fs.open('/myfile', 'wb') as f: f.write(b'I had a nice big cabbage') fs.move('/myfile', '/otherfile') assert not fs.exists('/myfile') assert fs.info('/otherfile') assert isinstance(fs.ukey('/otherfile'), str)
def test_read_block_delimiter(): fs = MemoryFileSystem() with fs.open("/myfile", "wb") as f: f.write(b"some\n" b"lines\n" b"of\n" b"text") assert fs.read_block("/myfile", 0, 2, b"\n") == b"some\n" assert fs.read_block("/myfile", 2, 6, b"\n") == b"lines\n" assert fs.read_block("/myfile", 6, 2, b"\n") == b"" assert fs.read_block("/myfile", 2, 9, b"\n") == b"lines\nof\n" assert fs.read_block("/myfile", 12, 6, b"\n") == b"text" assert fs.read_block("/myfile", 0, None) == fs.cat("/myfile")
def test_read_block_delimiter(): fs = MemoryFileSystem() with fs.open('/myfile', 'wb') as f: f.write(b'some\n' b'lines\n' b'of\n' b'text') assert fs.read_block('/myfile', 0, 2, b'\n') == b'some\n' assert fs.read_block('/myfile', 2, 6, b'\n') == b'lines\n' assert fs.read_block('/myfile', 6, 2, b'\n') == b'' assert fs.read_block('/myfile', 2, 9, b'\n') == b'lines\nof\n' assert fs.read_block('/myfile', 12, 6, b'\n') == b'text' assert fs.read_block('/myfile', 0, None) == fs.cat('/myfile')
def test_open_text(): fs = MemoryFileSystem() with fs.open("/myfile", "wb") as f: f.write(b"some\n" b"lines\n" b"of\n" b"text") f = fs.open("/myfile", "r", encoding="latin1") assert f.encoding == "latin1"
def test_head_tail(): fs = MemoryFileSystem() with fs.open("/myfile", "wb") as f: f.write(b"I had a nice big cabbage") assert fs.head("/myfile", 5) == b"I had" assert fs.tail("/myfile", 7) == b"cabbage"
def test_head_tail(): fs = MemoryFileSystem() with fs.open('/myfile', 'wb') as f: f.write(b'I had a nice big cabbage') assert fs.head('/myfile', 5) == b'I had' assert fs.tail('/myfile', 7) == b'cabbage'