| 1 |
"""Towncrier related shims.""" |
| 2 |
|
| 3 |
from contextlib import suppress as _suppress_exception |
| 4 |
from pathlib import Path |
| 5 |
from typing import Any, Dict, Union |
| 6 |
|
| 7 |
|
| 8 |
with _suppress_exception(ImportError): |
| 9 |
# NOTE: This will not raise an exception under Python >= 3.7, and is only |
| 10 |
# NOTE: needed for Towncrier >= 22.12.0rc1 which doesn't support Python 3.6 |
| 11 |
from dataclasses import asdict as _dataclass_to_dict # noqa: WPS433 |
| 12 |
|
| 13 |
|
| 14 |
try: |
| 15 |
# Towncrier >= 22.8.0rc1 |
| 16 |
# pylint: disable=import-error,no-name-in-module |
| 17 |
from towncrier._settings.load import ( # noqa: WPS433, WPS436 |
| 18 |
load_config_from_file, |
| 19 |
) |
| 20 |
except ImportError: |
| 21 |
# pylint: disable=import-error,no-name-in-module |
| 22 |
from towncrier._settings import ( # noqa: WPS433, WPS436, WPS440 |
| 23 |
load_config_from_file, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
def get_towncrier_config( |
| 28 |
project_path: Path, |
| 29 |
final_config_path: Union[Path, None], |
| 30 |
) -> Dict[str, Any]: # FIXME: add a better type # pylint: disable=fixme |
| 31 |
"""Return the towncrier config dictionary.""" |
| 32 |
try: |
| 33 |
# Towncrier >= 19.9.0 |
| 34 |
config = load_config_from_file( |
| 35 |
str(project_path), str(final_config_path), |
| 36 |
) |
| 37 |
except TypeError: |
| 38 |
# Towncrier < 19.9.0 |
| 39 |
|
| 40 |
if final_config_path is None or not final_config_path.exists(): |
| 41 |
# Towncrier < 19.9.0 |
| 42 |
raise FileNotFoundError( |
| 43 |
f"[Errno 2] No such file or directory: '{final_config_path}'", |
| 44 |
) from None |
| 45 |
|
| 46 |
return load_config_from_file(str(final_config_path)) |
| 47 |
|
| 48 |
if isinstance(config, dict): |
| 49 |
# Towncrier < 22.12.0rc1 |
| 50 |
return config |
| 51 |
|
| 52 |
return _dataclass_to_dict(config) |