Debian Codegraph @

src/sphinxcontrib/towncrier/_fragment_discovery.py

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 """Changelog fragment discovery helpers."""
2
3
4 from functools import lru_cache
5 from pathlib import Path
6 from typing import Optional, Set
7
8 from sphinx.util import logging
9
10
11 try:
12 # pylint: disable=no-name-in-module
13 from towncrier.build import find_fragments # noqa: WPS433
14 except ImportError:
15 # pylint: disable=import-self,no-name-in-module
16 from towncrier import ( # type: ignore[attr-defined] # noqa: WPS433,WPS440
17 find_fragments,
18 )
19
20 from ._towncrier import get_towncrier_config # noqa: WPS436
21
22
23 logger = logging.getLogger(__name__)
24
25
26 def _resolve_spec_config(
27 base: Path, spec_name: Optional[str] = None,
28 ) -> Optional[Path]:
29 return base / spec_name if spec_name is not None else None
30
31
32 # pylint: disable=fixme
33 # FIXME: consider consolidating this logic upstream in towncrier
34 def _find_config_file(base: Path) -> Path:
35 """Find the best config file."""
36 candidate_names = 'towncrier.toml', 'pyproject.toml'
37 candidates = list(map(base.joinpath, candidate_names))
38 extant = filter(Path.is_file, candidates)
39 return next(extant, candidates[-1])
40
41
42 # pylint: disable=fixme
43 # FIXME: refactor `lookup_towncrier_fragments` to drop noqas
44 @lru_cache(maxsize=1, typed=True) # noqa: WPS210
45 def lookup_towncrier_fragments( # noqa: WPS210
46 working_dir: Optional[str] = None,
47 config_path: Optional[str] = None,
48 ) -> Set[Path]:
49 """Emit RST-formatted Towncrier changelog fragment paths."""
50 project_path = Path.cwd() if working_dir is None else Path(working_dir)
51
52 final_config_path = (
53 _resolve_spec_config(project_path, config_path)
54 or _find_config_file(project_path)
55 )
56
57 try:
58 towncrier_config = get_towncrier_config(
59 project_path,
60 final_config_path,
61 )
62 except KeyError as key_err:
63 # NOTE: The error is missing key 'towncrier' or similar
64 logger.warning(
65 f'Missing key {key_err!s} in file {final_config_path!s}',
66 )
67 return set()
68
69 fragment_directory: Optional[str] = 'newsfragments'
70 try:
71 fragment_base_directory = project_path / towncrier_config['directory']
72 except KeyError:
73 assert fragment_directory is not None
74 fragment_base_directory = project_path / fragment_directory
75 else:
76 fragment_directory = None
77
78 _fragments, fragment_filenames = find_fragments(
79 str(fragment_base_directory),
80 towncrier_config['sections'],
81 fragment_directory,
82 towncrier_config['types'],
83 )
84 return set(fragment_filenames)