lair.config.get_data_dir#

lair.config.get_data_dir(env_var, path=None)[source]#

Resolve a data directory.

Parameters:
  • env_var (str) – Environment variable to fall back to, e.g. 'LAIR_INVENTORY_DIR'.

  • path (str | os.PathLike, optional) – Explicit directory. Takes precedence over the environment variable.

Returns:

Path – The data directory.

Raises:

ValueError – If neither path nor the environment variable is set.

Return type:

Path

See also

get_cache_dir

The cache folder, which has a default.

Examples

An explicit folder wins over the variable:

>>> import os
>>> from lair.config import get_data_dir
>>> os.environ["LAIR_INVENTORY_DIR"] = "/data/inventories"
>>> get_data_dir("LAIR_INVENTORY_DIR").as_posix()
'/data/inventories'
>>> get_data_dir("LAIR_INVENTORY_DIR", "/elsewhere").as_posix()
'/elsewhere'

With neither, the error names the variable:

>>> del os.environ["LAIR_INVENTORY_DIR"]
>>> get_data_dir("LAIR_INVENTORY_DIR")
Traceback (most recent call last):
    ...
ValueError: No data directory given: pass it explicitly or set the LAIR_INVENTORY_DIR environment variable.