Debian Codegraph @

packaging/pep517_backend/_transformers.py

raw
1 """Data conversion helpers for the in-tree PEP 517 build backend."""
2
3 from itertools import chain
4 from re import sub as _substitute_with_regexp
5 from typing import Dict, Iterable, Iterator, List, Mapping, Tuple, Union
6
7
8 def _emit_opt_pairs(opt_pair: Tuple[str, Union[str, Dict[str, str]]]) -> Iterator[str]:
9 flag, flag_value = opt_pair
10 flag_opt = f"--{flag!s}"
11 if isinstance(flag_value, dict):
12 sub_pairs: Iterable[Tuple[str, ...]] = flag_value.items()
13 else:
14 sub_pairs = ((flag_value,),)
15
16 yield from ("=".join(map(str, (flag_opt,) + pair)) for pair in sub_pairs)
17
18
19 def get_cli_kwargs_from_config(
20 kwargs_map: Mapping[str, Union[str, Dict[str, str]]],
21 ) -> List[str]:
22 """Make a list of options with values from config."""
23 return list(chain.from_iterable(map(_emit_opt_pairs, kwargs_map.items())))
24
25
26 def get_enabled_cli_flags_from_config(flags_map: Mapping[str, bool]) -> List[str]:
27 """Make a list of enabled boolean flags from config."""
28 return [f"--{flag}" for flag, is_enabled in flags_map.items() if is_enabled]
29
30
31 def sanitize_rst_roles(rst_source_text: str) -> str:
32 """Replace RST roles with inline highlighting."""
33 pep_role_regex = r"""(?x)
34 :pep:`(?P<pep_number>\d+)`
35 """
36 pep_substitution_pattern = (
37 r"`PEP \g<pep_number> <https://peps.python.org/pep-\g<pep_number>>`__"
38 )
39
40 user_role_regex = r"""(?x)
41 :user:`(?P<github_username>[^`]+)(?:\s+(.*))?`
42 """
43 user_substitution_pattern = (
44 r"`@\g<github_username> "
45 r"<https://github.com/sponsors/\g<github_username>>`__"
46 )
47
48 issue_role_regex = r"""(?x)
49 :issue:`(?P<issue_number>[^`]+)(?:\s+(.*))?`
50 """
51 issue_substitution_pattern = (
52 r"`#\g<issue_number> "
53 r"<https://github.com/aio-libs/frozenlist/issues/\g<issue_number>>`__"
54 )
55
56 pr_role_regex = r"""(?x)
57 :pr:`(?P<pr_number>[^`]+)(?:\s+(.*))?`
58 """
59 pr_substitution_pattern = (
60 r"`PR #\g<pr_number> "
61 r"<https://github.com/aio-libs/frozenlist/pull/\g<pr_number>>`__"
62 )
63
64 commit_role_regex = r"""(?x)
65 :commit:`(?P<commit_sha>[^`]+)(?:\s+(.*))?`
66 """
67 commit_substitution_pattern = (
68 r"`\g<commit_sha> "
69 r"<https://github.com/aio-libs/frozenlist/commit/\g<commit_sha>>`__"
70 )
71
72 gh_role_regex = r"""(?x)
73 :gh:`(?P<gh_slug>[^`]+)(?:\s+(.*))?`
74 """
75 gh_substitution_pattern = (
76 r"`GitHub: \g<gh_slug> <https://github.com/\g<gh_slug>>`__"
77 )
78
79 meth_role_regex = r"""(?x)
80 (?::py)?:meth:`~?(?P<rendered_text>[^`<]+)(?:\s+([^`]*))?`
81 """
82 meth_substitution_pattern = r"``\g<rendered_text>()``"
83
84 role_regex = r"""(?x)
85 (?::\w+)?:\w+:`(?P<rendered_text>[^`<]+)(?:\s+([^`]*))?`
86 """
87 substitution_pattern = r"``\g<rendered_text>``"
88
89 substitutions = (
90 (pep_role_regex, pep_substitution_pattern),
91 (user_role_regex, user_substitution_pattern),
92 (issue_role_regex, issue_substitution_pattern),
93 (pr_role_regex, pr_substitution_pattern),
94 (commit_role_regex, commit_substitution_pattern),
95 (gh_role_regex, gh_substitution_pattern),
96 (meth_role_regex, meth_substitution_pattern),
97 (role_regex, substitution_pattern),
98 )
99
100 rst_source_normalized_text = rst_source_text
101 for regex, substitution in substitutions:
102 rst_source_normalized_text = _substitute_with_regexp(
103 regex,
104 substitution,
105 rst_source_normalized_text,
106 )
107
108 return rst_source_normalized_text