12
12
from typing import TYPE_CHECKING
13
13
from typing import Union
14
14
15
+ from ._integration .pyproject_reading import (
16
+ get_args_for_pyproject as _get_args_for_pyproject ,
17
+ )
18
+ from ._integration .pyproject_reading import read_pyproject as _read_pyproject
15
19
from ._version_cls import NonNormalizedVersion
16
20
from ._version_cls import Version
17
21
from .utils import trace
24
28
DEFAULT_TAG_REGEX = r"^(?:[\w-]+-)?(?P<version>[vV]?\d+(?:\.\d+){0,2}[^\+]*)(?:\+.*)?$"
25
29
DEFAULT_VERSION_SCHEME = "guess-next-dev"
26
30
DEFAULT_LOCAL_SCHEME = "node-and-date"
27
- _ROOT = "root"
28
31
29
32
30
33
def _check_tag_regex (value : str | Pattern [str ] | None ) -> Pattern [str ]:
@@ -67,12 +70,6 @@ def _check_absolute_root(root: _t.PathT, relative_to: _t.PathT | None) -> str:
67
70
return os .path .abspath (root )
68
71
69
72
70
- def _lazy_tomli_load (data : str ) -> dict [str , Any ]:
71
- from tomli import loads
72
-
73
- return loads (data )
74
-
75
-
76
73
_VersionT = Union [Version , NonNormalizedVersion ]
77
74
78
75
@@ -202,7 +199,7 @@ def from_file(
202
199
cls ,
203
200
name : str = "pyproject.toml" ,
204
201
dist_name : str | None = None ,
205
- _load_toml : Callable [[str ], dict [str , Any ]] = _lazy_tomli_load ,
202
+ _load_toml : Callable [[str ], dict [str , Any ]] | None = None ,
206
203
** kwargs : Any ,
207
204
) -> Configuration :
208
205
"""
@@ -212,61 +209,7 @@ def from_file(
212
209
not contain the [tool.setuptools_scm] section.
213
210
"""
214
211
215
- with open (name , encoding = "UTF-8" ) as strm :
216
- data = strm . read ( )
212
+ pyproject_data = _read_pyproject (name , _load_toml = _load_toml )
213
+ args = _get_args_for_pyproject ( pyproject_data , dist_name , kwargs )
217
214
218
- defn = _load_toml (data )
219
- try :
220
- section = defn .get ("tool" , {})["setuptools_scm" ]
221
- except LookupError as e :
222
- raise LookupError (
223
- f"{ name } does not contain a tool.setuptools_scm section"
224
- ) from e
225
-
226
- project = defn .get ("project" , {})
227
- dist_name = cls ._cleanup_from_file_args_data (
228
- project , dist_name , kwargs , section
229
- )
230
- return cls (dist_name = dist_name , relative_to = name , ** section , ** kwargs )
231
-
232
- @staticmethod
233
- def _cleanup_from_file_args_data (
234
- project : dict [str , Any ],
235
- dist_name : str | None ,
236
- kwargs : dict [str , Any ],
237
- section : dict [str , Any ],
238
- ) -> str | None :
239
- """drops problematic details and figures the distribution name"""
240
- if "dist_name" in section :
241
- if dist_name is None :
242
- dist_name = section .pop ("dist_name" )
243
- else :
244
- assert dist_name == section ["dist_name" ]
245
- del section ["dist_name" ]
246
- if dist_name is None :
247
- # minimal pep 621 support for figuring the pretend keys
248
- dist_name = project .get ("name" )
249
- if dist_name is None :
250
- dist_name = _read_dist_name_from_setup_cfg ()
251
- if _ROOT in kwargs :
252
- if kwargs [_ROOT ] is None :
253
- kwargs .pop (_ROOT , None )
254
- elif _ROOT in section :
255
- if section [_ROOT ] != kwargs [_ROOT ]:
256
- warnings .warn (
257
- f"root { section [_ROOT ]} is overridden"
258
- f" by the cli arg { kwargs [_ROOT ]} "
259
- )
260
- section .pop ("root" , None )
261
- return dist_name
262
-
263
-
264
- def _read_dist_name_from_setup_cfg () -> str | None :
265
-
266
- # minimal effort to read dist_name off setup.cfg metadata
267
- import configparser
268
-
269
- parser = configparser .ConfigParser ()
270
- parser .read (["setup.cfg" ])
271
- dist_name = parser .get ("metadata" , "name" , fallback = None )
272
- return dist_name
215
+ return cls (relative_to = name , ** args )
0 commit comments