Debian Codegraph @

src/sphinxcontrib/towncrier/_towncrier.py

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