augur.config module

Helpers for YAML-based configuration files.

augur.config.resolve_filepath(path, search_paths)

Resolve a filepath by searching through multiple directories.

Parameters:
  • path (Path) -- The filepath to resolve. May be either an absolute path or a path relative to one of the directories in search_paths.

  • search_paths (list[Path]) -- Directories to search, in order, when path is relative. Ignored when path is absolute.

Return type:

Path

Examples

If the path is already absolute, verify it exists and return it.

>>> import tempfile
>>> tmpdir1 = Path(tempfile.mkdtemp()).resolve()
>>> tmpdir2 = Path(tempfile.mkdtemp()).resolve()
>>> absolute_path = tmpdir1 / "file.txt"
>>> with open(absolute_path, "w") as f: _ = f.write("test")
>>> resolve_filepath(absolute_path, []) == absolute_path
True

Otherwise, try resolving it relative to each directory in search_paths, in order. Return the first path that exists.

>>> with open(tmpdir2 / "file.txt", "w") as f: _ = f.write("test")
>>> result = resolve_filepath(Path("file.txt"), [tmpdir1, tmpdir2])
>>> result == tmpdir1 / "file.txt"
True

If an absolute path doesn’t exist, raise an error.

>>> resolve_filepath(Path("/nonexistent/file.txt"), [tmpdir1, tmpdir2])
Traceback (most recent call last):
  ...
augur.errors.AugurError: File '/nonexistent/file.txt' does not exist.

If the relative path doesn’t exist anywhere, raise an error.

>>> resolve_filepath(Path("nonexistent.txt"), [tmpdir1, tmpdir2])
Traceback (most recent call last):
  ...
augur.errors.AugurError: File 'nonexistent.txt' not resolvable from any of the following paths:

  ...