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.fsspec
- class datalad_fuse.fsspec.DatasetAdapter(path, caching, mode_transparent=False)[source]
Read 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/fsspec/, to be reused by subsequent reads (for a week after they were first cached). 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.
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 file 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
- 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 fsspec file objects.
- 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 none of its candidate URLs could be opened.
- class datalad_fuse.fsspec.FsspecAdapter(root, caching, mode_transparent=False)[source]
Read 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.
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.fsspec.FileState[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.
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]
A 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]
A (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)[source]
Bases:
Operationsmfusepy file system exposing a dataset, as used by
datalad fusefsFiles are read via an
FsspecAdapter, 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/.
Examples
Mount a dataset with extra FUSE options:
from mfusepy import FUSE FUSE(DataLadFUSE("/abs/path/to/ds", caching=False), "/mnt/point", foreground=True, ro=True)