Source code for fsspec_rs.local

from __future__ import annotations

import os
from collections.abc import Sequence

from fsspec.spec import AbstractBufferedFile

from fsspec_rs._conditions import reject_unsupported_conditions
from fsspec_rs._files import FileSystem
from fsspec_rs._ranges import cat_ranges as _cat_ranges
from fsspec_rs.fsspec_rs import IoCapabilities, Match, Missing, RangeReadResult, ReadResult, RustLocalFs, WriteResult

_WINDOWS = os.sep == "\\"


def _posix(path: str) -> str:
    """Use "/" in paths, as fsspec's local filesystem does on Windows."""
    return path.replace("\\", "/") if _WINDOWS else path


def _posix_info(info: dict) -> dict:
    info["name"] = _posix(info["name"])
    return info


[docs] @reject_unsupported_conditions class LocalFileSystem(FileSystem): """Local filesystem backed by the Rust ``LocalFs`` implementation. The protocol is registered as ``("file-rs", "local-rs")`` so it can coexist with the pure-Python ``fsspec.implementations.local.LocalFileSystem`` which owns the ``("file", "local")`` protocol names. """ protocol = ("file-rs", "local-rs") local_file = True @classmethod def _strip_protocol(cls, path): """Strip ``file-rs://`` or ``local-rs://`` prefix.""" for proto in cls.protocol: prefix = f"{proto}://" if path.startswith(prefix): path = path[len(prefix) :] break return _posix(path)
[docs] def unstrip_protocol(self, path): return f"{self.protocol[0]}://{path}"
def __init__(self, auto_mkdir: bool = False, **storage_options): super().__init__(**storage_options) self._rust = RustLocalFs(auto_mkdir=auto_mkdir) self.auto_mkdir = auto_mkdir # Core primitives — delegated to Rust
[docs] def ls(self, path: str, detail: bool = True, **kwargs): entries = self._rust.ls(self._strip_protocol(path), detail=detail) return [_posix_info(entry) for entry in entries] if detail else [_posix(name) for name in entries]
[docs] def info(self, path: str, **kwargs): return _posix_info(self._rust.info(self._strip_protocol(path)))
def _open( self, path: str, mode: str = "rb", block_size=None, autocommit: bool = True, cache_options=None, cache_type=None, **kwargs, ): """Return a Rust-backed file object.""" if "condition" in kwargs: raise NotImplementedError("conditions are supported by read/write, not open") return LocalFile( self, self._strip_protocol(path), mode=mode, cache_type=cache_type, block_size=block_size, )
[docs] def mkdir(self, path: str, create_parents: bool = True, **kwargs): return self._rust.mkdir(self._strip_protocol(path), create_parents=create_parents)
[docs] def makedirs(self, path: str, exist_ok: bool = False): return self._rust.makedirs(self._strip_protocol(path), exist_ok=exist_ok)
[docs] def rmdir(self, path: str): return self._rust.rmdir(self._strip_protocol(path))
[docs] def rm_file(self, path: str): return self._rust.rm_file(self._strip_protocol(path))
[docs] def rm(self, path, recursive: bool = False, maxdepth=None): if isinstance(path, str): return self._rust.rm(self._strip_protocol(path), recursive=recursive) for p in path: self._rust.rm(self._strip_protocol(p), recursive=recursive)
[docs] def cp_file(self, path1: str, path2: str, **kwargs): return self._rust.cp_file(self._strip_protocol(path1), self._strip_protocol(path2))
[docs] def copy(self, path1: str, path2: str, recursive: bool = False, **kwargs): return self._rust.copy(self._strip_protocol(path1), self._strip_protocol(path2), recursive=recursive)
[docs] def mv(self, path1: str, path2: str, **kwargs): return self._rust.mv(self._strip_protocol(path1), self._strip_protocol(path2))
# Higher-level helpers — delegated to Rust for speed
[docs] def exists(self, path: str, **kwargs) -> bool: return self._rust.exists(self._strip_protocol(path))
[docs] def isdir(self, path: str) -> bool: return self._rust.isdir(self._strip_protocol(path))
[docs] def isfile(self, path: str) -> bool: return self._rust.isfile(self._strip_protocol(path))
[docs] def size(self, path: str) -> int: return self._rust.size(self._strip_protocol(path))
[docs] def read(self, path: str, start=None, end=None, condition: Match | Missing | None = None) -> ReadResult: return self._rust.read(self._strip_protocol(path), start=start, end=end, condition=condition)
[docs] def write(self, path: str, data: bytes, condition: Match | Missing | None = None) -> WriteResult: return self._rust.write(self._strip_protocol(path), data, condition=condition)
[docs] def read_ranges( self, path: str, ranges: Sequence[tuple[int | None, int | None]], *, condition: Match | Missing | None = None, max_gap: int = 65536, max_request_size: int = 8388608, max_concurrency: int = 8, ) -> RangeReadResult: return self._rust.read_ranges( self._strip_protocol(path), ranges, condition=condition, max_gap=max_gap, max_request_size=max_request_size, max_concurrency=max_concurrency, )
[docs] def cat_ranges(self, paths, starts, ends, max_gap=None, on_error="return", **kwargs): return _cat_ranges(self, paths, starts, ends, max_gap=max_gap, on_error=on_error, **kwargs)
[docs] def io_capabilities(self) -> IoCapabilities: return self._rust.io_capabilities()
[docs] def cat_file(self, path: str, start=None, end=None, condition: Match | Missing | None = None, **kwargs) -> bytes: if condition is not None: return self.read(path, start=start, end=end, condition=condition).data return bytes(self._rust.cat_file(self._strip_protocol(path), start=start, end=end))
[docs] def pipe_file(self, path: str, value: bytes, condition: Match | Missing | None = None, **kwargs): if condition is not None: self.write(path, value, condition=condition) return None return self._rust.pipe_file(self._strip_protocol(path), value)
[docs] def head(self, path: str, size: int = 1024) -> bytes: return bytes(self._rust.head(self._strip_protocol(path), size))
[docs] def tail(self, path: str, size: int = 1024) -> bytes: return bytes(self._rust.tail(self._strip_protocol(path), size))
[docs] def touch(self, path: str, truncate: bool = True, **kwargs): return self._rust.touch(self._strip_protocol(path), truncate=truncate)
[docs] def walk(self, path: str, maxdepth=None, topdown: bool = True, **kwargs): entries = self._rust.walk(self._strip_protocol(path), max_depth=maxdepth, topdown=topdown) for dirpath, dirnames, filenames in entries: yield _posix(dirpath), dirnames, filenames
[docs] def find(self, path: str, maxdepth=None, withdirs: bool = False, **kwargs): return [_posix(name) for name in self._rust.find(self._strip_protocol(path), max_depth=maxdepth, with_dirs=withdirs)]
[docs] def du(self, path: str, total: bool = True, maxdepth=None, **kwargs): result = self._rust.du(self._strip_protocol(path), total=total) return result if total else {_posix(name): size for name, size in result.items()}
[docs] def read_text(self, path: str, encoding=None, errors=None, newline=None, **kwargs) -> str: return self._rust.read_text(self._strip_protocol(path))
[docs] def write_text(self, path: str, value: str, encoding=None, errors=None, newline=None, **kwargs): return self._rust.write_text(self._strip_protocol(path), value)
[docs] def get_file(self, rpath: str, lpath: str, **kwargs): return self._rust.get_file(self._strip_protocol(rpath), lpath)
[docs] def put_file(self, lpath: str, rpath: str, **kwargs): return self._rust.put_file(lpath, self._strip_protocol(rpath))
[docs] class LocalFile(AbstractBufferedFile): """File-like wrapper delegating to the Rust ``RustLocalFile``.""" def __init__(self, fs: LocalFileSystem, path: str, mode: str = "rb", cache_type=None, block_size=None, **kwargs): # Open the Rust file handle right away self._rust_file = None # set early so __del__/closed don't blow up if "condition" in kwargs: raise NotImplementedError("conditions are supported by read/write, not open") open_kwargs = {} if cache_type is not None: open_kwargs["cache_type"] = cache_type if block_size is not None: open_kwargs["block_size"] = block_size self._rust_file = fs._rust.open(path, mode, **open_kwargs) self.path = path self.mode = mode self.fs = fs # Do NOT call super().__init__() — we are a thin wrapper, and # AbstractBufferedFile's __init__ tries to call size() etc. # which does not apply for write-mode files. Instead we just # set the minimum attributes that __repr__ et al. need. self.blocksize = 0 self.loc = 0 # io.RawIOBase-like interface
[docs] def read(self, length: int = -1) -> bytes: data = bytes(self._rust_file.read(length)) self.loc += len(data) return data
[docs] def write(self, data: bytes) -> int: n = self._rust_file.write(data) self.loc += n return n
[docs] def seek(self, loc: int, whence: int = 0) -> int: pos = self._rust_file.seek(loc, whence) self.loc = pos return pos
[docs] def tell(self) -> int: return self._rust_file.tell()
[docs] def flush(self, force: bool = False): self._rust_file.flush()
[docs] def close(self): self._rust_file.close()
[docs] def discard(self): """Discard pending writes and close; direct local writes cannot be undone.""" self._rust_file.discard()
def __del__(self): if not self.closed: self.discard() @property def closed(self) -> bool: return self._rust_file is None or self._rust_file.closed
[docs] def readable(self) -> bool: return "r" in self.mode
[docs] def writable(self) -> bool: return "w" in self.mode or "a" in self.mode or "x" in self.mode
[docs] def seekable(self) -> bool: return True
def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): if exc_type is None: self.close() else: self.discard() def __repr__(self) -> str: status = "closed" if self.closed else "open" return f"LocalFile('{self.path}', mode='{self.mode}', {status})"