Skip to content

Loadfig

Official loadfig documentation.

Minimalistic library for loading tool-specific configuration from configuration files (either .<tool>.toml or pyproject.toml).

loadfig.__version__ module-attribute

__version__ = version('loadfig')

Current loadfig version.

loadfig.config

config(name, directory=None, vcs=True)

Load tool config from dedicated TOML files or pyproject.toml.

The following paths are checked in order (first found used):

  • .{name}.toml in the start directory or a VCS-marked parent directory
  • .config/{name}.toml in the start directory or a VCS-marked parent directory
  • pyproject.toml in the start directory or a VCS-marked parent directory
Note

Parsed TOML files are loaded once and cached for subsequent usage.

Example:

Assume the following pyproject.toml file at the root of your project:

[tool.mytool]
name = "My Tool"
version = "1.0.0"

You can load the configuration for mytool using:

import loadfig

config = loadfig.config("mytool")
config["name"]  # "My Tool"
config["version"]  # "1.0.0"

[!IMPORTANT] Automatically returns only the relevant configuration, not the content of the whole file.

[!WARNING] Empty dictionaries are returned if no configuration was found, client code should handle this case (and have a config with default values to use in such cases).

Parameters:

Name Type Description Default
name str

The name of the tool to search for in the configuration file.

required
directory Path | str | None

The directory where lookup starts. If not provided, the current working directory is used.

None
vcs bool

Whether VCS-marked parent directories are considered. Note: This searches for .git, .hg, or .svn directories upwards from directory.

True

Raises:

Type Description
TOMLDecodeError

If a found TOML file cannot be decoded.

Returns:

Type Description
dict[Any, Any]

Configuration dictionary of the tool or an empty dictionary

dict[Any, Any]

if no configuration was found.

Source code in src/loadfig/_config.py
def config(
    name: str,
    directory: pathlib.Path | str | None = None,
    vcs: bool = True,  # noqa: FBT001, FBT002
) -> dict[typing.Any, typing.Any]:
    """Load tool config from dedicated TOML files or `pyproject.toml`.

    The following paths are checked in order (first found used):

    - `.{name}.toml` in the start directory or a VCS-marked parent directory
    - `.config/{name}.toml` in the start directory or a VCS-marked parent
        directory
    - `pyproject.toml` in the start directory or a VCS-marked parent directory

    Note:
        Parsed TOML files are loaded once and cached for subsequent usage.

    __Example:__

    Assume the following `pyproject.toml` file at the root of your project:

    ```toml
    [tool.mytool]
    name = "My Tool"
    version = "1.0.0"
    ```

    You can load the configuration for `mytool` using:

    ```python
    import loadfig

    config = loadfig.config("mytool")
    config["name"]  # "My Tool"
    config["version"]  # "1.0.0"
    ```

    > [!IMPORTANT]
    > Automatically returns __only__ the relevant configuration,
    > __not the content of the whole file__.

    > [!WARNING]
    > Empty dictionaries are returned if no configuration was found,
    > client code should handle this case (and have a config with
    > default values to use in such cases).

    Args:
        name:
            The name of the tool to search for in the configuration file.
        directory:
            The directory where lookup starts.
            If not provided, the current working directory is used.
        vcs:
            Whether VCS-marked parent directories are considered.
            Note: This searches for `.git`, `.hg`, or `.svn` directories
            upwards from `directory`.

    Raises:
        tomllib.TOMLDecodeError:
            If a found TOML file cannot be decoded.

    Returns:
        Configuration dictionary of the tool or an empty dictionary
        if no configuration was found.

    """
    if directory is None:
        directory = pathlib.Path.cwd().resolve()

    files = (f".{name}.toml", f".config/{name}.toml")

    for file in files:
        path = _file.find(file, vcs, start=directory)
        if path is not None:
            return _load.toml(path)

    return _pyproject.pyproject(directory, vcs).get("tool", {}).get(name, {})

loadfig.pyproject

pyproject(directory=None, vcs=True)

Read pyproject.toml configuration file.

The following paths are checked in order (first found used):

  • pyproject.toml in the directory (if not specified, cwd is used)
  • pyproject.toml up the tree if vcs is true
Note

pyproject.toml is loaded once and cached for subsequent usage.

Example:

Assume the following pyproject.toml file at the root of your project:

pyproject = loadfig.pyproject()

# Get all pyproject dependencies
pyproject["project"]["dependencies"]

Parameters:

Name Type Description Default
directory Path | str | None

The directory where lookup starts. If not provided, the current working directory is used.

None
vcs bool

Whether VCS-marked parent directories are considered. Note: This searches for .git, .hg, or .svn directories upwards from directory.

True

Raises:

Type Description
TOMLDecodeError

If a found pyproject.toml file cannot be decoded.

Returns:

Type Description
dict[Any, Any]

pyproject.toml content or an empty dictionary

dict[Any, Any]

if no pyproject.toml was found.

Source code in src/loadfig/_pyproject.py
def pyproject(
    directory: pathlib.Path | str | None = None,
    vcs: bool = True,  # noqa: FBT001, FBT002
) -> dict[typing.Any, typing.Any]:
    """Read `pyproject.toml` configuration file.

    The following paths are checked in order (first found used):

    - `pyproject.toml` in the `directory` (if not specified, `cwd` is used)
    - `pyproject.toml` up the tree if `vcs` is `true`

    Note:
        `pyproject.toml` is loaded once and cached for subsequent usage.

    __Example:__

    Assume the following `pyproject.toml` file at the root of your project:

    ```python
    pyproject = loadfig.pyproject()

    # Get all pyproject dependencies
    pyproject["project"]["dependencies"]
    ```

    Args:
        directory:
            The directory where lookup starts.
            If not provided, the current working directory is used.
        vcs:
            Whether VCS-marked parent directories are considered.
            Note: This searches for `.git`, `.hg`, or `.svn` directories
            upwards from `directory`.

    Raises:
        tomllib.TOMLDecodeError:
            If a found `pyproject.toml` file cannot be decoded.

    Returns:
        `pyproject.toml` content or an empty dictionary
        if no `pyproject.toml` was found.

    """
    if directory is None:
        directory = pathlib.Path.cwd().resolve()

    path = _file.find("pyproject.toml", vcs, start=directory)
    if path is not None:
        return _load.toml(path)

    return {}