Skip to content

Commit 4690f52

Browse files
committed
Internal refactor to switch to pypi_json for interacting with PyPI.
1 parent 3e06bd8 commit 4690f52

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
apeye>=1.0.0
2+
dist-meta>=0.1.2
23
domdf-python-tools>=2.8.0
34
packaging>=20.9
5+
pypi-json>=0.1.1
46
requests>=2.24.0
57
shippinglabel>=0.15.0
68
sphinx>=3.0.3

seed_intersphinx_mapping/__init__.py

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@
3737
from typing import Dict, Optional, Tuple, Union
3838

3939
# 3rd party
40+
import dist_meta
4041
import requests
4142
from apeye.requests_url import RequestsURL
43+
from dist_meta.metadata_mapping import MetadataMapping
4244
from domdf_python_tools.compat import importlib_resources
4345
from domdf_python_tools.utils import stderr_writer
4446
from packaging.requirements import Requirement
45-
from shippinglabel import get_project_links
47+
from pypi_json import PyPIJSON
4648

4749
# this package
4850
from seed_intersphinx_mapping.cache import cache
@@ -59,6 +61,41 @@
5961
_DOCUMENTATION_RE = re.compile(r"^[dD]oc(s|umentation)")
6062

6163

64+
def _get_project_links(project_name: str) -> MetadataMapping:
65+
"""
66+
Returns the web links for the given project.
67+
68+
The exact keys vary, but common keys include "Documentation" and "Issue Tracker".
69+
70+
:param project_name:
71+
"""
72+
73+
urls = MetadataMapping()
74+
75+
# Try a local package first
76+
try:
77+
dist = dist_meta.distributions.get_distribution(project_name)
78+
raw_urls = dist.get_metadata().get_all("Project-URL", default=())
79+
80+
for url in raw_urls:
81+
label, url, *_ = map(str.strip, url.split(','))
82+
if _DOCUMENTATION_RE.match(label):
83+
urls[label] = url
84+
85+
except dist_meta.distributions.DistributionNotFoundError:
86+
# Fall back to PyPI
87+
88+
with PyPIJSON() as client:
89+
metadata = client.get_metadata(project_name).info
90+
91+
if "project_urls" in metadata and metadata["project_urls"]:
92+
for label, url in metadata["project_urls"].items():
93+
if _DOCUMENTATION_RE.match(label):
94+
urls[label] = url
95+
96+
return urls
97+
98+
6299
@cache
63100
def get_sphinx_doc_url(pypi_name: str) -> str:
64101
"""
@@ -91,9 +128,7 @@ def get_sphinx_doc_url(pypi_name: str) -> str:
91128
:exc:`apeye.slumber_url.exceptions.HttpNotFoundError` if the project could not be found on PyPI.
92129
"""
93130

94-
for key, value in get_project_links(pypi_name).items():
95-
if not _DOCUMENTATION_RE.match(key):
96-
continue
131+
for key, value in _get_project_links(pypi_name).items():
97132

98133
# Follow redirects to get actual URL
99134
r = RequestsURL(value).head(allow_redirects=True, timeout=10)

0 commit comments

Comments
 (0)