diff options
| author | Ronny Pfannschmidt <opensource@ronnypfannschmidt.de> | 2021-09-26 19:38:13 +0200 |
|---|---|---|
| committer | Ronny Pfannschmidt <opensource@ronnypfannschmidt.de> | 2021-09-26 20:06:46 +0200 |
| commit | 23aa992e69cf86bf7cc5113a69bc5cd9560d31a4 (patch) | |
| tree | 971f5f9b2ce8551154e18d2fc28bb4cfaca13f9b /src | |
| parent | c916a85b358433be8ca6f0defa67e85d34ce9641 (diff) | |
| download | setuptools-scm-23aa992e69cf86bf7cc5113a69bc5cd9560d31a4.tar.gz | |
move private entrypoint helpers to singular module
Diffstat (limited to 'src')
| -rw-r--r-- | src/setuptools_scm/__init__.py | 31 | ||||
| -rw-r--r-- | src/setuptools_scm/_entrypoints.py | 53 | ||||
| -rw-r--r-- | src/setuptools_scm/utils.py | 22 |
3 files changed, 61 insertions, 45 deletions
diff --git a/src/setuptools_scm/__init__.py b/src/setuptools_scm/__init__.py index 5e7baf3..8d4a36c 100644 --- a/src/setuptools_scm/__init__.py +++ b/src/setuptools_scm/__init__.py @@ -5,6 +5,8 @@ import os import warnings +from ._entrypoints import _call_entrypoint_fn +from ._entrypoints import _version_from_entrypoints from ._overrides import _read_pretended_version_for from ._overrides import PRETEND_KEY from ._overrides import PRETEND_KEY_NAMED @@ -44,35 +46,6 @@ def version_from_scm(root): return _version_from_entrypoints(config) -def _call_entrypoint_fn(root, config, fn): - if function_has_arg(fn, "config"): - return fn(root, config=config) - else: - warnings.warn( - f"parse function {fn.__module__}.{fn.__name__}" - " are required to provide a named argument" - " 'config', setuptools_scm>=8.0 will remove support.", - category=DeprecationWarning, - stacklevel=2, - ) - return fn(root) - - -def _version_from_entrypoints(config: Configuration, fallback=False): - if fallback: - entrypoint = "setuptools_scm.parse_scm_fallback" - root = config.fallback_root - else: - entrypoint = "setuptools_scm.parse_scm" - root = config.absolute_root - - for ep in iter_matching_entrypoints(root, entrypoint, config): - version = _call_entrypoint_fn(root, config, ep.load()) - trace(ep, version) - if version: - return version - - def dump_version(root, version: str, write_to, template: "str | None" = None): assert isinstance(version, str) if not write_to: diff --git a/src/setuptools_scm/_entrypoints.py b/src/setuptools_scm/_entrypoints.py new file mode 100644 index 0000000..48510e2 --- /dev/null +++ b/src/setuptools_scm/_entrypoints.py @@ -0,0 +1,53 @@ +import warnings +from typing import Optional + +from .config import Configuration +from .discover import iter_matching_entrypoints +from .utils import function_has_arg +from .utils import trace + + +def _call_entrypoint_fn(root, config, fn): + if function_has_arg(fn, "config"): + return fn(root, config=config) + else: + warnings.warn( + f"parse function {fn.__module__}.{fn.__name__}" + " are required to provide a named argument" + " 'config', setuptools_scm>=8.0 will remove support.", + category=DeprecationWarning, + stacklevel=2, + ) + return fn(root) + + +def _version_from_entrypoints(config: Configuration, fallback=False): + if fallback: + entrypoint = "setuptools_scm.parse_scm_fallback" + root = config.fallback_root + else: + entrypoint = "setuptools_scm.parse_scm" + root = config.absolute_root + + for ep in iter_matching_entrypoints(root, entrypoint, config): + version = _call_entrypoint_fn(root, config, ep.load()) + trace(ep, version) + if version: + return version + + +try: + from importlib.metadata import entry_points # type: ignore +except ImportError: + from pkg_resources import iter_entry_points +else: + + def iter_entry_points(group: str, name: Optional[str] = None): + all_eps = entry_points() + if hasattr(all_eps, "select"): + eps = all_eps.select(group=group) + else: + eps = all_eps[group] + if name is None: + return iter(eps) + return (ep for ep in eps if ep.name == name) diff --git a/src/setuptools_scm/utils.py b/src/setuptools_scm/utils.py index 2e84f87..2d200b3 100644 --- a/src/setuptools_scm/utils.py +++ b/src/setuptools_scm/utils.py @@ -8,7 +8,7 @@ import shlex import subprocess import sys import warnings -from typing import Optional + DEBUG = bool(os.environ.get("SETUPTOOLS_SCM_DEBUG")) IS_WINDOWS = platform.system() == "Windows" @@ -137,18 +137,8 @@ def require_command(name): raise OSError("%r was not found" % name) -try: - from importlib.metadata import entry_points # type: ignore -except ImportError: - from pkg_resources import iter_entry_points -else: - - def iter_entry_points(group: str, name: Optional[str] = None): - all_eps = entry_points() - if hasattr(all_eps, "select"): - eps = all_eps.select(group=group) - else: - eps = all_eps[group] - if name is None: - return iter(eps) - return (ep for ep in eps if ep.name == name) +def iter_entry_points(*k, **kw): + + from ._entrypoints import iter_entry_points + + return iter_entry_points(*k, **kw) |
