Skip to content

Instantly share code, notes, and snippets.

@dahlia
Last active January 24, 2017 22:39
Show Gist options
  • Select an option

  • Save dahlia/9e3a988a52de6319cd5d to your computer and use it in GitHub Desktop.

Select an option

Save dahlia/9e3a988a52de6319cd5d to your computer and use it in GitHub Desktop.
Example code to fetch Sphinx intersphinx inventory
#!/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()
@lmazuel
Copy link
Copy Markdown

lmazuel commented Jan 24, 2017

Works perfectly, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment