Skip to content

Commit 7b1c985

Browse files
committed
Move RootPathResolver to format module
Signed-off-by: Cristian Le <[email protected]>
1 parent 0165349 commit 7b1c985

File tree

2 files changed

+34
-24
lines changed

2 files changed

+34
-24
lines changed

src/scikit_build_core/builder/get_requires.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
import importlib.util
66
import os
77
import sysconfig
8-
from pathlib import Path
98
from typing import TYPE_CHECKING, Literal
109

1110
from packaging.tags import sys_tags
1211

1312
from .._compat import tomllib
1413
from .._logging import logger
14+
from ..format import pyproject_format
1515
from ..program_search import (
1616
best_program,
1717
get_cmake_programs,
@@ -67,28 +67,6 @@ def _load_scikit_build_settings(
6767
return SettingsReader.from_file("pyproject.toml", config_settings).settings
6868

6969

70-
@dataclasses.dataclass()
71-
class RootPathResolver:
72-
"""Handle ``{root:uri}`` like formatting similar to ``hatchling``."""
73-
74-
path: Path = dataclasses.field(default_factory=Path)
75-
76-
def __post_init__(self) -> None:
77-
self.path = self.path.resolve()
78-
79-
def __format__(self, fmt: str) -> str:
80-
command, _, rest = fmt.partition(":")
81-
if command == "parent":
82-
parent = RootPathResolver(self.path.parent)
83-
return parent.__format__(rest)
84-
if command == "uri" and rest == "":
85-
return self.path.as_uri()
86-
if command == "" and rest == "":
87-
return str(self)
88-
msg = f"Could not handle format: {fmt}"
89-
raise ValueError(msg)
90-
91-
9270
@dataclasses.dataclass(frozen=True)
9371
class GetRequires:
9472
settings: ScikitBuildSettings = dataclasses.field(
@@ -164,7 +142,11 @@ def dynamic_metadata(self) -> Generator[str, None, None]:
164142
return
165143

166144
for build_require in self.settings.build.requires:
167-
yield build_require.format(root=RootPathResolver())
145+
yield build_require.format(
146+
**pyproject_format(
147+
settings=self.settings,
148+
)
149+
)
168150

169151
for dynamic_metadata in self.settings.metadata.values():
170152
if "provider" in dynamic_metadata:

src/scikit_build_core/format.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
from __future__ import annotations
44

5+
import dataclasses
56
import sys
7+
from pathlib import Path
68
from typing import TYPE_CHECKING, TypedDict
79

810
if TYPE_CHECKING:
@@ -27,6 +29,7 @@ class PyprojectFormatter(TypedDict, total=False):
2729
wheel_tag: str
2830
build_type: str
2931
state: str
32+
root: RootPathResolver
3033

3134

3235
def pyproject_format(
@@ -45,6 +48,9 @@ def pyproject_format(
4548
# First set all known values
4649
res = PyprojectFormatter(
4750
cache_tag=sys.implementation.cache_tag,
51+
# We are assuming the Path.cwd always evaluates to the folder containing pyproject.toml
52+
# as part of PEP517 standard.
53+
root=RootPathResolver(),
4854
)
4955
# Then compute all optional keys depending on the function input
5056
if settings is not None:
@@ -55,3 +61,25 @@ def pyproject_format(
5561
res["state"] = state
5662
# Construct the final dict including the always known keys
5763
return res
64+
65+
66+
@dataclasses.dataclass()
67+
class RootPathResolver:
68+
"""Handle ``{root:uri}`` like formatting similar to ``hatchling``."""
69+
70+
path: Path = dataclasses.field(default_factory=Path)
71+
72+
def __post_init__(self) -> None:
73+
self.path = self.path.resolve()
74+
75+
def __format__(self, fmt: str) -> str:
76+
command, _, rest = fmt.partition(":")
77+
if command == "parent":
78+
parent = RootPathResolver(self.path.parent)
79+
return parent.__format__(rest)
80+
if command == "uri" and rest == "":
81+
return self.path.as_uri()
82+
if command == "" and rest == "":
83+
return str(self)
84+
msg = f"Could not handle format: {fmt}"
85+
raise ValueError(msg)

0 commit comments

Comments
 (0)