|
37 | 37 | from typing import Dict, Optional, Tuple, Union
|
38 | 38 |
|
39 | 39 | # 3rd party
|
| 40 | +import dist_meta |
40 | 41 | import requests
|
41 | 42 | from apeye.requests_url import RequestsURL
|
| 43 | +from dist_meta.metadata_mapping import MetadataMapping |
42 | 44 | from domdf_python_tools.compat import importlib_resources
|
43 | 45 | from domdf_python_tools.utils import stderr_writer
|
44 | 46 | from packaging.requirements import Requirement
|
45 |
| -from shippinglabel import get_project_links |
| 47 | +from pypi_json import PyPIJSON |
46 | 48 |
|
47 | 49 | # this package
|
48 | 50 | from seed_intersphinx_mapping.cache import cache
|
|
59 | 61 | _DOCUMENTATION_RE = re.compile(r"^[dD]oc(s|umentation)")
|
60 | 62 |
|
61 | 63 |
|
| 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 | + |
62 | 99 | @cache
|
63 | 100 | def get_sphinx_doc_url(pypi_name: str) -> str:
|
64 | 101 | """
|
@@ -91,9 +128,7 @@ def get_sphinx_doc_url(pypi_name: str) -> str:
|
91 | 128 | :exc:`apeye.slumber_url.exceptions.HttpNotFoundError` if the project could not be found on PyPI.
|
92 | 129 | """
|
93 | 130 |
|
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(): |
97 | 132 |
|
98 | 133 | # Follow redirects to get actual URL
|
99 | 134 | r = RequestsURL(value).head(allow_redirects=True, timeout=10)
|
|
0 commit comments