-
Notifications
You must be signed in to change notification settings - Fork 76
refactor: add a format module that is used in the expansion of pyproject.toml
#998
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
387c790
Add a `format` module
LecrisUT 0c4e7f9
Implement the `build_dir` format variables
LecrisUT b978f69
Move RootPathResolver to format module
LecrisUT ad5278e
Document formattable fields
LecrisUT 9ecfece
refactor: tighten typing using override
henryiii File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Formattable fields | ||
|
|
||
| The following configure keys are formatted as Python f-strings: | ||
|
|
||
| - `build-dir` | ||
| - `build.requires` | ||
|
|
||
| The available variables are documented in the members of | ||
| {py:class}`scikit_build_core.format.PyprojectFormatter` copied here for | ||
| visibility | ||
|
|
||
| ```{eval-rst} | ||
| .. autoattribute:: scikit_build_core.format.PyprojectFormatter.build_type | ||
| :no-index: | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like to generate this section, but maybe in a later PR |
||
|
|
||
| .. autoattribute:: scikit_build_core.format.PyprojectFormatter.cache_tag | ||
| :no-index: | ||
|
|
||
| .. autoattribute:: scikit_build_core.format.PyprojectFormatter.root | ||
| :no-index: | ||
|
|
||
| .. autoattribute:: scikit_build_core.format.PyprojectFormatter.state | ||
| :no-index: | ||
|
|
||
| .. autoattribute:: scikit_build_core.format.PyprojectFormatter.wheel_tag | ||
| :no-index: | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """Format variables available in the ``pyproject.toml`` evaluation""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import dataclasses | ||
| import sys | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, TypedDict | ||
|
|
||
| if TYPE_CHECKING: | ||
| from typing import Literal | ||
|
|
||
| from scikit_build_core.builder.wheel_tag import WheelTag | ||
| from scikit_build_core.settings.skbuild_model import ScikitBuildSettings | ||
|
|
||
| __all__ = [ | ||
| "PyprojectFormatter", | ||
| "RootPathResolver", | ||
| "pyproject_format", | ||
| ] | ||
|
|
||
|
|
||
| def __dir__() -> list[str]: | ||
| return __all__ | ||
|
|
||
|
|
||
| class PyprojectFormatter(TypedDict, total=False): | ||
| """Format helper for pyproject.toml. | ||
|
|
||
| Stores all known variables that can be used for evaluating a formatted string | ||
| in the pyproject.toml config file. | ||
| """ | ||
|
|
||
| cache_tag: str | ||
| """Tag used by the import machinery in the filenames of cached modules, i.e. ``sys.implementation.cache_tag``.""" | ||
| wheel_tag: str | ||
| """The tags as computed for the wheel.""" | ||
| build_type: str | ||
| """Build type passed as ``cmake.build_type``.""" | ||
| state: Literal["sdist", "wheel", "editable", "metadata_wheel", "metadata_editable"] | ||
| """The state of the build.""" | ||
| root: RootPathResolver | ||
| """Root path of the current project.""" | ||
|
|
||
|
|
||
| def pyproject_format( | ||
| *, | ||
| settings: ScikitBuildSettings | None = None, | ||
| state: ( | ||
| Literal["sdist", "wheel", "editable", "metadata_wheel", "metadata_editable"] | ||
| | None | ||
| ) = None, | ||
| tags: WheelTag | None = None, | ||
| dummy: bool = False, | ||
| ) -> PyprojectFormatter | dict[str, str]: | ||
| """Generate :py:class:`PyprojectFormatter` dictionary to use in f-string format.""" | ||
| if dummy: | ||
| # Return a dict with all the known keys but with values replaced with dummy values | ||
| return {key: "*" for key in PyprojectFormatter.__annotations__} | ||
| # First set all known values | ||
| res = PyprojectFormatter( | ||
| cache_tag=sys.implementation.cache_tag, | ||
| # We are assuming the Path.cwd always evaluates to the folder containing pyproject.toml | ||
| # as part of PEP517 standard. | ||
| root=RootPathResolver(), | ||
| ) | ||
| # Then compute all optional keys depending on the function input | ||
| if settings is not None: | ||
| res["build_type"] = settings.cmake.build_type | ||
| if tags is not None: | ||
| res["wheel_tag"] = str(tags) | ||
| if state is not None: | ||
| res["state"] = state | ||
| # Construct the final dict including the always known keys | ||
| return res | ||
|
|
||
|
|
||
| @dataclasses.dataclass() | ||
| class RootPathResolver: | ||
| """Handle ``{root:uri}`` like formatting similar to ``hatchling``.""" | ||
|
|
||
| path: Path = dataclasses.field(default_factory=Path) | ||
|
|
||
| def __post_init__(self) -> None: | ||
| self.path = self.path.resolve() | ||
|
|
||
| def __format__(self, fmt: str) -> str: | ||
| command, _, rest = fmt.partition(":") | ||
| if command == "parent": | ||
| parent = RootPathResolver(self.path.parent) | ||
| return parent.__format__(rest) | ||
| if command == "uri" and rest == "": | ||
| return self.path.as_uri() | ||
| if command == "" and rest == "": | ||
| return str(self) | ||
| msg = f"Could not handle format: {fmt}" | ||
| raise ValueError(msg) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not good at naming these.