|
1 | 1 | """Load configuration.""" |
2 | 2 |
|
3 | 3 | import logging |
4 | | -import pathlib |
5 | | -from typing import Optional |
| 4 | +from pathlib import Path |
| 5 | +from typing import Any |
6 | 6 |
|
7 | 7 | import tomllib |
8 | 8 |
|
9 | 9 | logger = logging.getLogger(__name__) |
10 | 10 |
|
11 | 11 | PATHS = ( |
12 | | - ".config/griffe2md.toml", |
13 | | - "config/griffe2md.toml", |
| 12 | + Path(".config/griffe2md.toml"), |
| 13 | + Path("config/griffe2md.toml"), |
| 14 | + Path("pyproject.toml"), |
14 | 15 | ) |
15 | 16 |
|
16 | | -PYPROJECT = "pyproject.toml" |
17 | 17 |
|
18 | | - |
19 | | -def _locate_config_file() -> Optional[pathlib.Path]: |
| 18 | +def _locate_config_file() -> Path | None: |
20 | 19 | for path in PATHS: |
21 | | - path_ = pathlib.Path(path) |
22 | | - if path_.is_file(): |
23 | | - return path_ |
24 | | - |
25 | | - pyproj_path = pathlib.Path(PYPROJECT) |
26 | | - if pyproj_path.is_file(): |
27 | | - return pyproj_path |
28 | | - |
| 20 | + if path.is_file(): |
| 21 | + return path |
29 | 22 | return None |
30 | 23 |
|
31 | 24 |
|
32 | | -def load_config() -> Optional[dict]: |
33 | | - """Load the configuration if config file or config entry in pyproject.toml exists. If neither config file was found or pyproject.toml file doesn't have [tool.griffe2md] section, returns None.""" |
34 | | - config_path = _locate_config_file() |
35 | | - if config_path is None: |
| 25 | +def load_config() -> dict[str, Any] | None: |
| 26 | + """Load the configuration if config file or config entry in pyproject.toml exists. |
| 27 | +
|
| 28 | + If neither config file was found or pyproject.toml file doesn't have |
| 29 | + a `[tool.griffe2md]` section, None is returned. |
| 30 | + """ |
| 31 | + if not (config_path := _locate_config_file()): |
36 | 32 | return None |
37 | 33 |
|
38 | 34 | logger.debug("Loading config from %s", config_path) |
39 | 35 |
|
40 | | - with open(config_path, "rb") as f: |
| 36 | + with config_path.open("rb") as f: |
41 | 37 | config = tomllib.load(f) |
42 | 38 |
|
43 | | - if str(config_path) == PYPROJECT: |
44 | | - return (config.get("tool") or {}).get("griffe2md") |
| 39 | + if config_path.name == "pyproject.toml": |
| 40 | + return config.get("tool", {}).get("griffe2md", None) |
45 | 41 | return config |
0 commit comments