Last active
January 24, 2017 22:39
-
-
Save dahlia/9e3a988a52de6319cd5d to your computer and use it in GitHub Desktop.
Example code to fetch Sphinx intersphinx inventory
This file contains hidden or 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
#!/usr/bin/env python3 | |
from __future__ import print_function | |
import pprint | |
import sys | |
import urllib.parse | |
import urllib.request | |
from sphinx.ext.intersphinx import read_inventory_v1, read_inventory_v2 | |
PACKAGES = { | |
'flask': 'http://flask.pocoo.org/docs/', | |
'wand': 'http://docs.wand-py.org/en/0.3.7/' | |
} | |
def get_inventory(url): | |
inv_url = urllib.parse.urljoin(url, 'objects.inv') | |
with urllib.request.urlopen(inv_url) as f: | |
line = f.readline().rstrip().decode('utf-8') | |
if line == '# Sphinx inventory version 1': | |
invdata = read_inventory_v1(f, url, urllib.parse.urljoin) | |
elif line == '# Sphinx inventory version 2': | |
invdata = read_inventory_v2(f, url, urllib.parse.urljoin) | |
else: | |
raise ValueError(line) | |
return invdata | |
def main(): | |
if len(sys.argv) < 2: | |
print('usage:', sys.argv[0], 'PACKAGE-NAME', file=sys.stderr) | |
raise SystemExit(1) | |
package = sys.argv[1] | |
try: | |
url = PACKAGES[package] | |
except KeyError: | |
print('unsupported package:', package, file=sys.stderr) | |
raise SystemExit(1) | |
result = get_inventory(url) | |
pprint.pprint(result) | |
if __name__ == '__main__': | |
main() |
This file contains hidden or 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
Sphinx >= 1.2.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works perfectly, thanks!