Python API reference
See Python usage for an introduction with examples.
DataLad commands
The commands added to DataLad by datalad-fuse, available from
datalad.api and as methods of datalad.api.Dataset.
|
FUSE File system providing transparent access to files under DataLad control |
|
Show leading lines/bytes of an annexed file by fetching its data from a remote URL |
|
Clear fsspec cache |
Opening files: datalad_fuse.adapter
- class datalad_fuse.adapter.DatasetAdapter(path, caching, mode_transparent=False, backends=None)[source]
Bases:
objectRead access to the files of a single dataset.
Files that are not annexed, and annexed files whose content is present locally, are opened from disk. Annexed files without local content are opened from one of the http(s) URLs found for their git-annex key (see
get_urls()), reading only the needed parts of the file.- Parameters:
path (str or Path) – Top directory of the dataset (any git or git-annex repository).
caching (bool) – If true, keep the data fetched from remote URLs in a sparse on-disk cache under
<path>/.git/datalad/cache/(one subdirectory per backend), to be reused by subsequent reads (for up to a week with thefsspecbackend). If false, data are only buffered in memory while a file is open.mode_transparent (bool) – If true, paths of key files under
.git/annex/objects/(the targets of annexed symlinks) are opened as annexed content, fetched from a remote URL if not present locally.backends (str, optional) – Comma-separated, priority-ordered backends to try, e.g.
"remfile,fsspec". Defaults to thedatalad.fusefs.backendsconfiguration option, or toDEFAULT_BACKENDS.
Notes
Call
close()(or usecontextlib.closing()) when done, to stop thegit annexprocesses started for the dataset.- get_file_state(relpath)[source]
Determine whether a file is annexed and has its content present
Results are cached (for the most recently queried files).
- get_urls(key)[source]
Yield candidate http(s) URLs for the content of an annex key
URLs are yielded in the order in which they are tried by
open():http(s) URLs recorded in git-annex for the key, as reported by
git annex whereis(e.g. those of thewebspecial remote);annex/objects/...locations on the http(s) git remotes thatgit annex whereislists as having the key, including theannex/objectsendpoint of Forgejo-aneksajo instances.
URLs on S3 special remotes with
exporttree=yesare not included;open()falls back to them viaget_exporttree_urls().
- get_exporttree_urls(relpath, key)[source]
Yield versioned URLs for relpath on S3 exporttree remotes.
Workaround for datasets lacking proper versioned URLs in git-annex metadata. Constructs URLs from the remote’s
publicurl+fileprefixand resolves the correct S3 object version by matchingkey.size.
- open(relpath, mode='rb', encoding='utf-8', errors=None)[source]
Open a file of the dataset for reading
Annexed files without local content are opened by the first configured backend that can handle them (see
can_handle()), trying each candidate URL in turn; if none of them works, the next backend is tried.- Parameters:
relpath (str) – Path of the file relative to the top directory of the dataset.
mode (str) –
"rb"(default) to get a binary file object,"r"or"rt"to get a text file object.encoding (str) – Encoding to use in text mode.
errors (str, optional) – How to handle encoding errors in text mode, as for
open().
- Returns:
A seekable, read-only file object. Files read from disk are regular Python file objects; files read from a URL are whatever the backend that opened them returns (an fsspec file object for the
fsspecbackend, aRemfileWrapperforremfile).- Return type:
file object
- Raises:
NotImplementedError – If
modeis not one of the supported read modes.IOError – If the content of an annexed file is not present locally and no backend could open any of its candidate URLs.
- class datalad_fuse.adapter.RemoteFilesystemAdapter(root, caching, mode_transparent=False, backends=None)[source]
Bases:
objectRead access to the files of a dataset and its installed subdatasets.
Each path is mapped to the (sub)dataset containing it, and a
DatasetAdapteris created for that dataset on first use. Use it as a context manager, so that thegit annexprocesses started for the datasets are stopped on exit.- Parameters:
root (str or Path) – Top directory of the (super)dataset.
caching (bool) – Passed to each
DatasetAdapter.mode_transparent (bool) – Passed to each
DatasetAdapter.backends (str, optional) – Passed to each
DatasetAdapter.
Notes
Use an absolute
root, and absolute paths under it for the methods. Paths relative torootor to the current directory are not supported.- resolve_dataset(filepath)[source]
Return the adapter for the dataset containing
filepath- Returns:
The adapter, and the path of
filepathrelative to the dataset’s top directory.- Return type:
tuple of (DatasetAdapter, str)
- Parameters:
- open(filepath, mode='rb', encoding='utf-8', errors=None)[source]
Open a file for reading; see
DatasetAdapter.open()
- get_file_state(filepath)[source]
Return state and key of a file; see
DatasetAdapter.get_file_state
- class datalad_fuse.adapter.FileState(*values)[source]
Bases:
EnumState of a file in a dataset, as returned by
get_file_state()- NOT_ANNEXED = 1
The file is not annexed (e.g. committed to git directly); it is read from disk.
- NO_CONTENT = 2
The file is annexed but its content is not present locally; it is read from a remote URL.
- HAS_CONTENT = 3
The file is annexed and its content is present locally; it is read from disk.
- datalad_fuse.adapter.resolve_backends(backends=None, config=None)[source]
Resolve backends spec from backends argument, config, or default.
config is a dataset’s
ConfigManager, so that a per-datasetdatalad.fusefs.backendsin.git/configor.datalad/configis honored; it inherits the global overrides (-c), so it is a superset of the globaldatalad.cfg. Falls back to the global config when not given.Returns
(spec, explicit)whereexplicitis True when the user (or config) supplied the spec and False when falling back toDEFAULT_BACKENDS. Callers useexplicitto decide whether a missing backend is a warning (explicit) or a silent skip (default).
- datalad_fuse.adapter.create_backends(spec, path, caching, explicit=True)[source]
Instantiate backends from a comma-separated spec.
Backends that cannot be imported are skipped; missing backends requested via an explicit spec (user argument or config) are logged as warnings, while those missing from the default spec are logged at debug level only. Raises
ValueErrorif no usable backend remains.
Note
Up to 0.6.0 these lived in datalad_fuse.fsspec, and
RemoteFilesystemAdapter was called FsspecAdapter. Importing
FsspecAdapter, DatasetAdapter, FileState or is_http_url
from datalad_fuse.fsspec still works, with a
DeprecationWarning.
Backends: datalad_fuse.backends
Backends do the actual reading of remote files; see Backends.
- class datalad_fuse.backends.Backend[source]
Bases:
ABCBase class for remote file access backends.
- abstractmethod can_handle(key, mode, relpath=None)[source]
Return True if this backend should be used for key in mode.
relpath is the path within the dataset, for backends that dispatch on the file name when the annex key carries no suffix (URL/VURL keys).
- datalad_fuse.backends.DEFAULT_BACKENDS = 'remfile,fsspec'
Backends used when neither
--backendsnor thedatalad.fusefs.backendsconfiguration option is set.
- class datalad_fuse.fsspec.FsspecBackend(path, caching)[source]
Bases:
BackendBackend using fsspec’s HTTPFileSystem (optionally with disk caching).
- can_handle(key, mode, relpath=None)[source]
Return True if this backend should be used for key in mode.
relpath is the path within the dataset, for backends that dispatch on the file name when the annex key carries no suffix (URL/VURL keys).
- class datalad_fuse.remfile.RemfileBackend(path, caching)[source]
Bases:
BackendBackend using remfile for HDF5-structured files (.nwb, .h5, etc.).
- PROBE_TIMEOUT = 10.0
Timeout, in seconds, for the size probe in
open_url().
- can_handle(key, mode, relpath=None)[source]
Return True if this backend should be used for key in mode.
relpath is the path within the dataset, for backends that dispatch on the file name when the annex key carries no suffix (URL/VURL keys).
- class datalad_fuse.remfile.RemfileWrapper(remfile_obj, url)[source]
Bases:
objectWraps
remfile.Fileto satisfy the contracts expected by datalad-fuse.Adds context manager protocol, line iteration (for
fsspec_head), and aninfo()method compatible withfile_getattrin fuse_.py.- Parameters:
remfile_obj (Any)
url (str)
- info()[source]
Minimal info dict matching the fsspec convention.
DataLadFUSE.getattr(path, fh)reaches this for everyfstat()on an open handle, so it must not hit the network: a HEAD request would fail outright against presigned S3 GET URLs, which reject HEAD. remfile already determined the size when the file was opened.
git-annex helpers: datalad_fuse.utils
- class datalad_fuse.utils.AnnexKey(backend, name, size=None, mtime=None, chunk_size=None, chunk_number=None, suffix=None)[source]
Bases:
objectA git-annex key, parsed into its fields
See <https://git-annex.branchable.com/internals/key_format/>.
str()of an instance gives back the key.Examples
>>> k = AnnexKey.parse("SHA256E-s1024--0123abcd.nwb") >>> k.backend, k.size, k.name, k.suffix ('SHA256E', 1024, '0123abcd', '.nwb') >>> str(k) 'SHA256E-s1024--0123abcd.nwb'
- Parameters:
- classmethod parse(s)[source]
Parse a key; raises
ValueErrorifsis not a valid key
- class datalad_fuse.utils.AnnexDir(topdir)[source]
Bases:
objectA (hashing) directory under
.git/annex/objects/of a repository- Parameters:
topdir (str)
FUSE file system: datalad_fuse.fuse_
- class datalad_fuse.fuse_.DataLadFUSE(root, caching, mode_transparent=False, backends=None)[source]
Bases:
Operationsfusepy file system exposing a dataset, as used by
datalad fusefsFiles are read via a
RemoteFilesystemAdapter, so annexed files without local content are read from their remote URLs. Unlessmode_transparentis set, annexed files appear as regular files. For files without local content, the size is taken from their annex key and the modification time is the date of theHEADcommit. Files cannot be written to.- Parameters:
root (str) – Absolute path, without symbolic links (see
os.path.realpath), to the top directory of the dataset to expose.caching (bool) – Whether to cache remote data on disk; see
DatasetAdapter.mode_transparent (bool) – Whether to expose the
.gitdirectories of the datasets (hidden by default). Annexed files without local content then appear as symlinks into.git/annex/objects/.backends (str, optional) – Comma-separated, priority-ordered backends to read remote files with; see
DatasetAdapter.
Examples
Mount a dataset with extra FUSE options:
from fuse import FUSE FUSE(DataLadFUSE("/abs/path/to/ds", caching=False), "/mnt/point", foreground=True, ro=True)