forked from hynek/hatch-fancy-pypi-readme
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdm.py
52 lines (40 loc) · 1.54 KB
/
pdm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# SPDX-FileCopyrightText: 2023 Hynek Schlawack <[email protected]>
#
# SPDX-License-Identifier: MIT
from __future__ import annotations
from typing import TYPE_CHECKING, Any
from ._builder import build_text
from ._config import load_and_validate_config
if TYPE_CHECKING:
from pdm.backend.hooks import Context
class FancyReadmeHook:
CONFIG_KEY = "tool.pdm.build.hooks.fancy-pypi-readme"
def pdm_build_hook_enabled(self, context: Context) -> bool:
metadata = context.config.metadata
if "readme" not in metadata.get("dynamic", []):
return False
return self._have_config(context)
def pdm_build_initialize(self, context: Context) -> None:
metadata = context.config.metadata
config = load_and_validate_config(
self._get_config(context), base=f"{self.CONFIG_KEY}."
)
metadata.get("dynamic", []).remove("readme")
metadata["readme"] = {
"content-type": config.content_type,
"text": build_text(config.fragments, config.substitutions),
}
def _have_config(self, context: Context) -> bool:
try:
self._get_config(context)
except (KeyError, TypeError):
return False
return True
def _get_config(self, context: Context) -> dict[str, Any]:
data = context.config.data
for key in self.CONFIG_KEY.split("."):
data = data[key]
if not isinstance(data, dict):
msg = f"expected a dict at `{self.CONFIG_KEY}`"
raise TypeError(msg)
return data