eradiate.data.FileResolver

class eradiate.data.FileResolver(paths=NOTHING)[source]

Bases: object

This class resolves paths relative to a list of locations on disk. Locations are looked up in order upon calling the resolve() method. If a lookup is successful, the resolved absolute path is returned; otherwise, the input path is returned unchanged.

Parameters:

paths (list of path-likes, optional) – A list of search directories. Each entry must exist.

Notes

  • By default, the unique file resolver instance eradiate.fresolver is initialized with the installation path of the asset manager (see AssetManager) and, when in dev mode (see SOURCE_DIR), the path to the location of test files.

  • The current working directory is not appended to the file resolver by default. It can however optionally be added temporarily when calling resolve() (and wrappers such as load_dataset()). This is useful when using the strict mode, which raises if the requested path cannot be resolved to an existing file.

Examples

A single instance of the file resolver is available as eradiate.fresolver:

>>> from eradiate import fresolver

To add a path to the file resolver, use the append() or prepend() methods, e.g.:

>>> fresolver.append("some/path/on/the/drive")

To resolve a relative path, use the resolve() method:

>>> fresolver.resolve("srf/sentinel_2a-msi-3.nc")

If the path is expected to point to an existing dataset file, the load_dataset() method can be used to load it immediately:

>>> fresolver.load_dataset("srf/sentinel_2a-msi-3.nc")
append(path, avoid_duplicates=True)[source]

Append an entry to the end of the list of search paths.

Parameters:
  • path (path-like) – Path to append. The location must exist on disk.

  • avoid_duplicates (bool, optional) – If True, do not append again a path that is already registered.

See also

prepend()

clear()[source]

Clear the list of search paths.

info(show=False)[source]

Collect information about the file resolver.

Parameters:

show (bool) – If True, display information to the terminal. Otherwise, return it as a dictionary.

Returns:

dict or None

load_dataset(path, strict=False, cwd=False)[source]

Chain resolve() and xarray.load_dataset().

Parameters:
  • path (path-like) – Path to be resolved.

  • strict (bool, default: False) – If True, resolution failure will raise.

  • cwd (bool, default: False) – If True, check first if a file relative to the current working directory exists.

Returns:

Dataset

prepend(path, avoid_duplicates=True)[source]

Prepend an entry at the beginning of the list of search paths.

Parameters:
  • path (path-like) – Path to prepend. The location must exist on disk.

  • avoid_duplicates (bool, optional) – If True, do not prepend again a path that is already registered.

See also

append()

resolve(path, strict=False, cwd=False)[source]

Resolve a path: search all registered locations in order. If no file is found in any of the registered location, path is returned unchanged.

Parameters:
  • path (path-like) – Path to be resolved.

  • strict (bool, default: False) – If True, resolution failure will raise.

  • cwd (bool, default: False) – If True, check first if a file relative to the current working directory exists.

Returns:

Path – Resolved path

Raises:

FileNotFoundError – If strict is True and path was not found in one of the registered directories.