-
-
Notifications
You must be signed in to change notification settings - Fork 43
ENH Support alternative index urls #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
1cf0992
5012813
948d09a
1e042ff
464336d
9cb49fb
0de73c3
02edf5b
ccb8f70
6b50de6
e212ce2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| from .. import package_index | ||
|
|
||
|
|
||
| def set_index_urls(urls: list[str] | str) -> None: | ||
| """ | ||
| Set the index URLs to use when looking up packages. | ||
|
|
||
| The URLs should contain the placeholder {package_name} which will be | ||
| replaced with the package name when looking up a package. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| urls | ||
| A list of URLs or a single URL to use as the package index. | ||
| """ | ||
|
|
||
| if isinstance(urls, str): | ||
| urls = [urls] | ||
|
|
||
| for url in urls: | ||
| package_index._check_index_url(url) | ||
|
|
||
| package_index.INDEX_URLS = urls |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| import json | ||
| import string | ||
| import sys | ||
| from collections import defaultdict | ||
| from collections.abc import Generator | ||
|
|
@@ -7,8 +9,14 @@ | |
| from packaging.utils import InvalidWheelFilename | ||
| from packaging.version import InvalidVersion, Version | ||
|
|
||
| from ._compat import fetch_string | ||
| from ._utils import is_package_compatible, parse_version | ||
|
|
||
| DEFAULT_INDEX_URLS = ["https://pypi.org/pypi/{package_name}/json"] | ||
| INDEX_URLS = DEFAULT_INDEX_URLS | ||
|
|
||
| _formatter = string.Formatter() | ||
|
|
||
|
|
||
| # TODO: Merge this class with WheelInfo | ||
| @dataclass | ||
|
|
@@ -182,3 +190,59 @@ def _fast_check_incompatibility(filename: str) -> bool: | |
| return False | ||
|
|
||
| return True | ||
|
|
||
|
|
||
| def _check_index_url(url: str) -> None: | ||
| fields = [parsed[1] for parsed in _formatter.parse(url)] | ||
|
|
||
| if "package_name" not in fields: | ||
| raise ValueError( | ||
| f"Invalid index URL: {url!r}. " | ||
| "Please make sure it contains the placeholder {package_name}." | ||
| ) | ||
|
|
||
|
|
||
| async def search_packages( | ||
| pkg: str, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should it be It's not "search" technically though. Search is returning top k results for a query, possibly with some pre-processing or fuzzing of the query. While this return a single response with which is the exact match. Maybe
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion! I wasn't aware of the difference between search and query :) I will go with |
||
| fetch_kwargs: dict[str, str] | None = None, | ||
| index_urls: list[str] | str | None = None, | ||
| ) -> ProjectInfo: | ||
| """ | ||
| Search for packages from given index URLs. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| pkg | ||
| Name of the package to search for. | ||
| fetch_kwargs | ||
| Keyword arguments to pass to the fetch function. | ||
| index_urls | ||
| A list of URLs or a single URL to use as the package index. | ||
| If None, the default index URLs are used. | ||
| """ | ||
| global INDEX_URLS | ||
|
|
||
| if not fetch_kwargs: | ||
| fetch_kwargs = {} | ||
|
|
||
| if index_urls is None: | ||
| index_urls = INDEX_URLS | ||
| elif isinstance(index_urls, str): | ||
| index_urls = [index_urls] | ||
|
|
||
| for url in index_urls: | ||
| _check_index_url(url) | ||
|
|
||
| url = url.format(package_name=pkg) | ||
|
|
||
| try: | ||
| metadata = await fetch_string(url, fetch_kwargs) | ||
| except OSError: | ||
| continue | ||
|
|
||
| return ProjectInfo.from_json_api(json.loads(metadata)) | ||
| else: | ||
| raise ValueError( | ||
| f"Can't fetch metadata for '{pkg}'." | ||
| "Please make sure you have entered a correct package name." | ||
|
ryanking13 marked this conversation as resolved.
Outdated
|
||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.