-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from Carreau/improve-lookup
Improve lookup with full url, and filter.
- Loading branch information
Showing
2 changed files
with
88 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,75 @@ | ||
import sys | ||
|
||
from intersphinx_registry import get_intersphinx_mapping | ||
import logging as _logging | ||
from sphinx.ext.intersphinx import inspect_main | ||
from urllib.parse import urljoin | ||
|
||
if len(sys.argv) != 2: | ||
sys.exit("Usage: python -m intersphinx_registry.lookup <package>") | ||
from typing import Optional | ||
|
||
packages = set([sys.argv[1]]) | ||
from sphinx.util.inventory import InventoryFile | ||
from io import BytesIO | ||
|
||
# filename = 'https://ipython.readthedocs.io/en/latest/' | ||
import requests | ||
|
||
if len(sys.argv) not in [2, 3]: | ||
sys.exit( | ||
"""Usage: python -m intersphinx_registry.lookup <package>[,package] [search_term] | ||
Example: | ||
$ python -m intersphinx_registry.lookup numpy,scipy array | ||
$ python -m intersphinx_registry.lookup ipython formatters.html | ||
""" | ||
) | ||
|
||
packages = set(sys.argv[1].split(",")) | ||
|
||
search_term: Optional[str] | ||
if len(sys.argv) == 3: | ||
search_term = sys.argv[2] | ||
else: | ||
search_term = None | ||
|
||
# there will be only one url | ||
urls = [ | ||
urljoin(u[0], (u[1] if u[1] else "objects.inv")) | ||
(u[0], (u[1] if u[1] else "objects.inv")) | ||
for u in get_intersphinx_mapping(packages=packages).values() | ||
] | ||
|
||
flattened = [] | ||
for base_url, obj in urls: | ||
|
||
final_url = urljoin(base_url, obj) | ||
|
||
resp = requests.get(final_url) | ||
|
||
inv = InventoryFile.load(BytesIO(resp.content), base_url, urljoin) | ||
|
||
for key, v in inv.items(): | ||
inv_entries = sorted(v.items()) | ||
for entry, (_proj, _ver, url_path, display_name) in inv_entries: | ||
# display_name = display_name * (display_name != '-') | ||
flattened.append((key, entry, _proj, _ver, display_name, url_path)) | ||
|
||
filtered = [] | ||
|
||
width = [len(x) for x in flattened[0]] | ||
|
||
for item in flattened: | ||
key, entry, proj, version, display_name, url_path = item | ||
if ( | ||
(search_term is None) | ||
or (search_term in entry) | ||
or (search_term in display_name) | ||
or (search_term in url_path) | ||
): | ||
filtered.append((key, entry, proj, version, display_name, url_path)) | ||
width = [max(w, len(x)) for w, x in zip(width, item)] | ||
|
||
|
||
_logging.basicConfig() | ||
raise SystemExit(inspect_main(urls)) | ||
for key, entry, proj, version, display_name, url_path in filtered: | ||
w_key, w_entry, w_proj, w_version, w_di, w_url = width | ||
print( | ||
f"{key:<{w_key}} {entry:<{w_entry}} {proj:<{w_proj}} {version:<{w_version}} {display_name!r:<{w_di+2}} {url_path}" | ||
) |