|
| 1 | +"""Format variables available in the ``pyproject.toml`` evaluation""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import dataclasses |
| 6 | +import sys |
| 7 | +import typing |
| 8 | +from pathlib import Path |
| 9 | +from typing import TYPE_CHECKING, TypedDict |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from typing import Literal |
| 13 | + |
| 14 | + from scikit_build_core.builder.wheel_tag import WheelTag |
| 15 | + from scikit_build_core.settings.skbuild_model import ScikitBuildSettings |
| 16 | + |
| 17 | +__all__ = [ |
| 18 | + "PyprojectFormatter", |
| 19 | + "RootPathResolver", |
| 20 | + "pyproject_format", |
| 21 | +] |
| 22 | + |
| 23 | + |
| 24 | +def __dir__() -> list[str]: |
| 25 | + return __all__ |
| 26 | + |
| 27 | + |
| 28 | +class PyprojectFormatter(TypedDict, total=False): |
| 29 | + """Format helper for pyproject.toml. |
| 30 | +
|
| 31 | + Stores all known variables that can be used for evaluating a formatted string |
| 32 | + in the pyproject.toml config file. |
| 33 | + """ |
| 34 | + |
| 35 | + cache_tag: str |
| 36 | + """Tag used by the import machinery in the filenames of cached modules, i.e. ``sys.implementation.cache_tag``.""" |
| 37 | + wheel_tag: str |
| 38 | + """The tags as computed for the wheel.""" |
| 39 | + build_type: str |
| 40 | + """Build type passed as ``cmake.build_type``.""" |
| 41 | + state: Literal["sdist", "wheel", "editable", "metadata_wheel", "metadata_editable"] |
| 42 | + """The state of the build.""" |
| 43 | + root: RootPathResolver |
| 44 | + """Root path of the current project.""" |
| 45 | + |
| 46 | + |
| 47 | +@typing.overload |
| 48 | +def pyproject_format( |
| 49 | + *, |
| 50 | + settings: ScikitBuildSettings, |
| 51 | + state: Literal["sdist", "wheel", "editable", "metadata_wheel", "metadata_editable"] |
| 52 | + | None = ..., |
| 53 | + tags: WheelTag | None = ..., |
| 54 | +) -> PyprojectFormatter: ... |
| 55 | + |
| 56 | + |
| 57 | +@typing.overload |
| 58 | +def pyproject_format(*, dummy: Literal[True]) -> dict[str, str]: ... |
| 59 | + |
| 60 | + |
| 61 | +def pyproject_format( |
| 62 | + *, |
| 63 | + settings: ScikitBuildSettings | None = None, |
| 64 | + state: ( |
| 65 | + Literal["sdist", "wheel", "editable", "metadata_wheel", "metadata_editable"] |
| 66 | + | None |
| 67 | + ) = None, |
| 68 | + tags: WheelTag | None = None, |
| 69 | + dummy: bool = False, |
| 70 | +) -> PyprojectFormatter | dict[str, str]: |
| 71 | + """Generate :py:class:`PyprojectFormatter` dictionary to use in f-string format.""" |
| 72 | + if dummy: |
| 73 | + # Return a dict with all the known keys but with values replaced with dummy values |
| 74 | + return {key: "*" for key in PyprojectFormatter.__annotations__} |
| 75 | + |
| 76 | + assert settings is not None |
| 77 | + # First set all known values |
| 78 | + res = PyprojectFormatter( |
| 79 | + cache_tag=sys.implementation.cache_tag, |
| 80 | + # We are assuming the Path.cwd always evaluates to the folder containing pyproject.toml |
| 81 | + # as part of PEP517 standard. |
| 82 | + root=RootPathResolver(), |
| 83 | + build_type=settings.cmake.build_type, |
| 84 | + ) |
| 85 | + # Then compute all optional keys depending on the function input |
| 86 | + if tags is not None: |
| 87 | + res["wheel_tag"] = str(tags) |
| 88 | + if state is not None: |
| 89 | + res["state"] = state |
| 90 | + # Construct the final dict including the always known keys |
| 91 | + return res |
| 92 | + |
| 93 | + |
| 94 | +@dataclasses.dataclass() |
| 95 | +class RootPathResolver: |
| 96 | + """Handle ``{root:uri}`` like formatting similar to ``hatchling``.""" |
| 97 | + |
| 98 | + path: Path = dataclasses.field(default_factory=Path) |
| 99 | + |
| 100 | + def __post_init__(self) -> None: |
| 101 | + self.path = self.path.resolve() |
| 102 | + |
| 103 | + def __format__(self, fmt: str) -> str: |
| 104 | + command, _, rest = fmt.partition(":") |
| 105 | + if command == "parent": |
| 106 | + parent = RootPathResolver(self.path.parent) |
| 107 | + return parent.__format__(rest) |
| 108 | + if command == "uri" and rest == "": |
| 109 | + return self.path.as_uri() |
| 110 | + if command == "" and rest == "": |
| 111 | + return str(self) |
| 112 | + msg = f"Could not handle format: {fmt}" |
| 113 | + raise ValueError(msg) |
0 commit comments