API reference¶
Local filesystem backed by the Rust |
|
File-like wrapper delegating to the Rust |
|
S3 filesystem backed by the Rust |
|
File-like wrapper delegating to the Rust |
|
Python-visible file metadata, analogous to the dict returned by fsspec's info(). |
|
Python-visible enum for file types. |
Filesystems¶
- class fsspec_rs.LocalFileSystem(*args, **kwargs)[source]¶
Bases:
AbstractFileSystemLocal filesystem backed by the Rust
LocalFsimplementation.The protocol is registered as
("file-rs", "local-rs")so it can coexist with the pure-Pythonfsspec.implementations.local.LocalFileSystemwhich owns the("file", "local")protocol names.- protocol: ClassVar[str | tuple[str, ...]] = ('file-rs', 'local-rs')¶
- local_file = True¶
- ls(path: str, detail: bool = True, **kwargs)[source]¶
List objects at path.
This should include subdirectories and files at that location. The difference between a file and a directory must be clear when details are requested.
The specific keys, or perhaps a FileInfo class, or similar, is TBD, but must be consistent across implementations. Must include:
full path to the entry (without protocol)
size of the entry, in bytes. If the value cannot be determined, will be
None.type of entry, “file”, “directory” or other
Additional information may be present, appropriate to the file-system, e.g., generation, checksum, etc.
May use refresh=True|False to allow use of self._ls_from_cache to check for a saved listing and avoid calling the backend. This would be common where listing may be expensive.
- Parameters:
path (str)
detail (bool) – if True, gives a list of dictionaries, where each is the same as the result of
info(path). If False, gives a list of paths (str).kwargs (may have additional backend-specific options, such as version) – information
- Returns:
List of strings if detail is False, or list of directory information
dicts if detail is True.
- info(path: str, **kwargs)[source]¶
Give details of entry at path
Returns a single dictionary, with exactly the same information as
lswould withdetail=True.The default implementation calls ls and could be overridden by a shortcut. kwargs are passed on to
`ls().Some file systems might not be able to measure the file’s size, in which case, the returned dict will include
'size': None.- Returns:
dict with keys (name (full path in the FS), size (in bytes), type (file,)
directory, or something else) and other FS-specific keys.
- mkdir(path: str, create_parents: bool = True, **kwargs)[source]¶
Create directory entry at path
For systems that don’t have true directories, may create an for this instance only and not touch the real filesystem
- Parameters:
path (str) – location
create_parents (bool) – if True, this is equivalent to
makedirskwargs – may be permissions, etc.
- makedirs(path: str, exist_ok: bool = False)[source]¶
Recursively make directories
Creates directory at path and any intervening required directories. Raises exception if, for instance, the path already exists but is a file.
- Parameters:
path (str) – leaf directory name
exist_ok (bool (False)) – If False, will error if the target already exists
- rm(path, recursive: bool = False, maxdepth=None)[source]¶
Delete files.
- Parameters:
path (str or list of str) – File(s) to delete.
recursive (bool) – If file(s) are directories, recursively delete contents and then also remove the directory
maxdepth (int or None) – Depth to pass to walk for finding files to delete, if recursive. If None, there will be no limit and infinite recursion may be possible.
- copy(path1: str, path2: str, recursive: bool = False, **kwargs)[source]¶
Copy within two locations in the filesystem
- on_error“raise”, “ignore”
If raise, any not-found exceptions will be raised; if ignore any not-found exceptions will cause the path to be skipped; defaults to raise unless recursive is true, where the default is ignore
- cat_file(path: str, start=None, end=None, **kwargs) bytes[source]¶
Get the content of a file
- Parameters:
path (URL of file on this filesystems)
start (int) – Bytes limits of the read. If negative, backwards from end, like usual python slices. Either can be None for start or end of file, respectively
end (int) – Bytes limits of the read. If negative, backwards from end, like usual python slices. Either can be None for start or end of file, respectively
kwargs (passed to
open().)
- touch(path: str, truncate: bool = True, **kwargs)[source]¶
Create empty file, or update timestamp
- Parameters:
path (str) – file location
truncate (bool) – If True, always set file size to 0; if False, update timestamp and leave file unchanged, if backend allows this
- walk(path: str, maxdepth=None, topdown: bool = True, **kwargs)[source]¶
Return all files under the given path.
List all files, recursing into subdirectories; output is iterator-style, like
os.walk(). For a simple list of files,find()is available.When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False has no effect. (see os.walk)
Note that the “files” outputted will include anything that is not a directory, such as links.
- Parameters:
path (str) – Root to recurse into
maxdepth (int) – Maximum recursion depth. None means limitless, but not recommended on link-based file-systems.
topdown (bool (True)) – Whether to walk the directory tree from the top downwards or from the bottom upwards.
on_error ("omit", "raise", a callable) – if omit (default), path with exception will simply be empty; If raise, an underlying exception will be raised; if callable, it will be called with a single OSError instance as argument
kwargs (passed to
ls)
- find(path: str, maxdepth=None, withdirs: bool = False, **kwargs)[source]¶
List all files below path.
Like posix
findcommand without conditions- Parameters:
path (str)
maxdepth (int or None) – If not None, the maximum number of levels to descend
withdirs (bool) – Whether to include directory paths in the output. This is True when used by glob, but users usually only want files.
ls. (kwargs are passed to)
- du(path: str, total: bool = True, maxdepth=None, **kwargs)[source]¶
Space used by files and optionally directories within a path
Directory size does not include the size of its contents.
- Parameters:
path (str)
total (bool) – Whether to sum all the file sizes
maxdepth (int or None) – Maximum number of directory levels to descend, None for unlimited.
withdirs (bool) – Whether to include directory paths in the output.
kwargs (passed to
find)
- Returns:
Dict of {path (size} if total=False, or int otherwise, where numbers)
refer to bytes used.
- read_text(path: str, encoding=None, errors=None, newline=None, **kwargs) str[source]¶
Get the contents of the file as a string.
- Parameters:
path (str) – URL of file on this filesystems
encoding (same as open.)
errors (same as open.)
newline (same as open.)
- write_text(path: str, value: str, encoding=None, errors=None, newline=None, **kwargs)[source]¶
Write the text to the given file.
An existing file will be overwritten.
- Parameters:
path (str) – URL of file on this filesystems
value (str) – Text to write.
encoding (same as open.)
errors (same as open.)
newline (same as open.)
- class fsspec_rs.S3FileSystem(*args, **kwargs)[source]¶
Bases:
AbstractFileSystemS3 filesystem backed by the Rust
S3Fsimplementation.The protocol is registered as
"s3-rs"so it can coexist with the pure-Pythons3fs.S3FileSystemwhich owns the"s3"protocol.- Credential resolution (matching s3fs conventions):
Explicit keyword arguments (
key,secret,endpoint_url, …)Values from
fsspec.config.conf["s3"](populated fromFSSPEC_S3_*environment variables)Falls through to object_store’s own credential chain (IAM roles, etc.)
- protocol: ClassVar[str | tuple[str, ...]] = ('s3-rs',)¶
- ls(path: str, detail: bool = True, **kwargs)[source]¶
List objects at path.
This should include subdirectories and files at that location. The difference between a file and a directory must be clear when details are requested.
The specific keys, or perhaps a FileInfo class, or similar, is TBD, but must be consistent across implementations. Must include:
full path to the entry (without protocol)
size of the entry, in bytes. If the value cannot be determined, will be
None.type of entry, “file”, “directory” or other
Additional information may be present, appropriate to the file-system, e.g., generation, checksum, etc.
May use refresh=True|False to allow use of self._ls_from_cache to check for a saved listing and avoid calling the backend. This would be common where listing may be expensive.
- Parameters:
path (str)
detail (bool) – if True, gives a list of dictionaries, where each is the same as the result of
info(path). If False, gives a list of paths (str).kwargs (may have additional backend-specific options, such as version) – information
- Returns:
List of strings if detail is False, or list of directory information
dicts if detail is True.
- info(path: str, **kwargs)[source]¶
Give details of entry at path
Returns a single dictionary, with exactly the same information as
lswould withdetail=True.The default implementation calls ls and could be overridden by a shortcut. kwargs are passed on to
`ls().Some file systems might not be able to measure the file’s size, in which case, the returned dict will include
'size': None.- Returns:
dict with keys (name (full path in the FS), size (in bytes), type (file,)
directory, or something else) and other FS-specific keys.
- mkdir(path: str, create_parents: bool = True, **kwargs)[source]¶
Create directory entry at path
For systems that don’t have true directories, may create an for this instance only and not touch the real filesystem
- Parameters:
path (str) – location
create_parents (bool) – if True, this is equivalent to
makedirskwargs – may be permissions, etc.
- rm(path, recursive: bool = False, maxdepth=None)[source]¶
Delete files.
- Parameters:
path (str or list of str) – File(s) to delete.
recursive (bool) – If file(s) are directories, recursively delete contents and then also remove the directory
maxdepth (int or None) – Depth to pass to walk for finding files to delete, if recursive. If None, there will be no limit and infinite recursion may be possible.
- cat_file(path: str, start=None, end=None, **kwargs) bytes[source]¶
Get the content of a file
- Parameters:
path (URL of file on this filesystems)
start (int) – Bytes limits of the read. If negative, backwards from end, like usual python slices. Either can be None for start or end of file, respectively
end (int) – Bytes limits of the read. If negative, backwards from end, like usual python slices. Either can be None for start or end of file, respectively
kwargs (passed to
open().)
- walk(path: str, maxdepth=None, topdown: bool = True, **kwargs)[source]¶
Return all files under the given path.
List all files, recursing into subdirectories; output is iterator-style, like
os.walk(). For a simple list of files,find()is available.When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False has no effect. (see os.walk)
Note that the “files” outputted will include anything that is not a directory, such as links.
- Parameters:
path (str) – Root to recurse into
maxdepth (int) – Maximum recursion depth. None means limitless, but not recommended on link-based file-systems.
topdown (bool (True)) – Whether to walk the directory tree from the top downwards or from the bottom upwards.
on_error ("omit", "raise", a callable) – if omit (default), path with exception will simply be empty; If raise, an underlying exception will be raised; if callable, it will be called with a single OSError instance as argument
kwargs (passed to
ls)
- find(path: str, maxdepth=None, withdirs: bool = False, **kwargs)[source]¶
List all files below path.
Like posix
findcommand without conditions- Parameters:
path (str)
maxdepth (int or None) – If not None, the maximum number of levels to descend
withdirs (bool) – Whether to include directory paths in the output. This is True when used by glob, but users usually only want files.
ls. (kwargs are passed to)
Open files¶
- class fsspec_rs.LocalFile(fs: LocalFileSystem, path: str, mode: str = 'rb', cache_type=None, block_size=None, **kwargs)[source]¶
Bases:
AbstractBufferedFileFile-like wrapper delegating to the Rust
RustLocalFile.- read(length: int = -1) bytes[source]¶
Return data from cache, or fetch pieces as necessary
- Parameters:
length (int (-1)) – Number of bytes to read; if <0, all remaining bytes.
- write(data: bytes) int[source]¶
Write data to buffer.
Buffer only sent on flush() or if buffer is greater than or equal to blocksize.
- Parameters:
data (bytes) – Set of bytes to be written.
- seek(loc: int, whence: int = 0) int[source]¶
Set current file location
- Parameters:
loc (int) – byte location
whence ({0, 1, 2}) – from start of file, current location or end of file, resp.
- flush(force: bool = False)[source]¶
Write buffered data to backend store.
Writes the current buffer, if it is larger than the block-size, or if the file is being closed.
- Parameters:
force (bool) – When closing, write the last block even if it is smaller than blocks are allowed to be. Disallows further writing to this file.
- property closed: bool¶
- class fsspec_rs.S3File(fs: S3FileSystem, path: str, mode: str = 'rb', cache_type=None, block_size=None, **kwargs)[source]¶
Bases:
AbstractBufferedFileFile-like wrapper delegating to the Rust
RustS3File.- read(length: int = -1) bytes[source]¶
Return data from cache, or fetch pieces as necessary
- Parameters:
length (int (-1)) – Number of bytes to read; if <0, all remaining bytes.
- write(data: bytes) int[source]¶
Write data to buffer.
Buffer only sent on flush() or if buffer is greater than or equal to blocksize.
- Parameters:
data (bytes) – Set of bytes to be written.
- seek(loc: int, whence: int = 0) int[source]¶
Set current file location
- Parameters:
loc (int) – byte location
whence ({0, 1, 2}) – from start of file, current location or end of file, resp.
- flush(force: bool = False)[source]¶
Write buffered data to backend store.
Writes the current buffer, if it is larger than the block-size, or if the file is being closed.
- Parameters:
force (bool) – When closing, write the last block even if it is smaller than blocks are allowed to be. Disallows further writing to this file.
- property closed: bool¶
Metadata types¶
- class fsspec_rs.FileInfo(name, size=0, file_type=None)¶
Bases:
objectPython-visible file metadata, analogous to the dict returned by fsspec’s info().
- file_type¶
The file type as a string (“file”, “directory”, “other”).
- is_dir()¶
Whether this is a directory.
- is_file()¶
Whether this is a regular file.
- name¶
The full path name.
- size¶
Size in bytes.
- to_dict()¶
Convert to a Python dict matching fsspec’s info() format.
- class fsspec_rs.FileType¶
Bases:
objectPython-visible enum for file types.
- as_str()¶
Return the string representation used by fsspec (“file”, “directory”, “other”).
- static directory()¶
Create a FileType representing a directory.
- static file()¶
Create a FileType representing a regular file.
- static other()¶
Create a FileType representing an other entry.