config(name, path=None, directory=None, vcs=True)
Read pyproject.toml
configuration file.
The following paths are checked in order (first found used):
path
(section [tool.{name}]
or data in the whole file), if provided. .{name}.toml
in the current directory pyproject.toml
in the current directory .{name}.toml
in the project root (if vcs=True
, which is the default) as defined by git
, hg
, or svn
pyproject.toml
in the project root (if vcs=True
, which is the default) as defined by git
, hg
, or svn
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 |
path | Path | str | None | Explicitly provided path to the configuration file, if any. If not provided, loadfig will try to guess based on directory and vcs . | None |
directory | Path | str | None | The directory to search for the configuration file. If not provided, the current working directory is used. | None |
vcs | bool | Whether the version control system directories should be searched for when localizing the project root (default: True ). Note: This will search for .git , .hg , or .svn directories upwards from the directory until the root is reached. | True |
Raises:
Type | Description |
ConfigMissingError | If the path is specified, but the file does not exist. |
TomlDecodeError | If any of the files were found, but could not be read. |
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,
path: pathlib.Path | str | None = None,
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):
- `path` (section `[tool.{name}]` or data in the whole file),
__if provided__.
- `.{name}.toml` in the current directory
- `pyproject.toml` in the current directory
- `.{name}.toml` in the project root (if `vcs=True`, which is the default)
as defined by `git`, `hg`, or `svn`
- `pyproject.toml` in the project root (if `vcs=True`, which is the default)
as defined by `git`, `hg`, or `svn`
__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.
path:
Explicitly provided path to the configuration file, if any.
If not provided, `loadfig` will try to guess based on `directory`
and `vcs`.
directory:
The directory to search for the configuration file.
If not provided, the current working directory is used.
vcs:
Whether the version control system directories should be
searched for when localizing the project root (default: `True`).
Note: This will search for `.git`, `.hg`, or `.svn` directories
upwards from the `directory` until the root is reached.
Raises:
ConfigMissingError:
If the `path` is specified, but the file does not exist.
TomlDecodeError:
If any of the files were found, but could not be read.
Returns:
Configuration dictionary of the tool or an empty dictionary
if no configuration was found.
"""
if (cfg := _load.specified_path(name, path)) is not None:
return cfg
if directory is None:
directory = pathlib.Path.cwd().resolve()
files_getters = {
f".{name}.toml": _file_getter,
"pyproject.toml": _pyproject_getter,
}
for file, getter in files_getters.items():
path = _load.project_root(file, vcs, start=directory)
if path is not None:
with path.open("rb") as handle:
return getter(tomllib.load(handle), name)
return {}
|