intersphinx: Add read_inventory()

This commit is contained in:
Takeshi KOMIYA 2016-08-18 00:56:40 +09:00
parent e0d2783c7b
commit dcdd401d65
2 changed files with 18 additions and 20 deletions

View File

@ -125,6 +125,14 @@ def read_inventory_v2(f, uri, join, bufsize=16*1024):
return invdata return invdata
def read_inventory(f, uri, join, bufsize=16*1024):
line = f.readline().rstrip().decode('utf-8')
if line == '# Sphinx inventory version 1':
return read_inventory_v1(f, uri, join)
elif line == '# Sphinx inventory version 2':
return read_inventory_v2(f, uri, join, bufsize=bufsize)
def _strip_basic_auth(url): def _strip_basic_auth(url):
"""Returns *url* with basic auth credentials removed. Also returns the """Returns *url* with basic auth credentials removed. Also returns the
basic auth username and password if they're present in *url*. basic auth username and password if they're present in *url*.
@ -227,7 +235,6 @@ def fetch_inventory(app, uri, inv):
if not localuri: if not localuri:
# case: inv URI points to remote resource; strip any existing auth # case: inv URI points to remote resource; strip any existing auth
uri, _, _ = _strip_basic_auth(uri) uri, _, _ = _strip_basic_auth(uri)
join = localuri and path.join or posixpath.join
try: try:
if '://' in inv: if '://' in inv:
f = _read_from_url(inv) f = _read_from_url(inv)
@ -245,18 +252,12 @@ def fetch_inventory(app, uri, inv):
if uri in (inv, path.dirname(inv), path.dirname(inv) + '/'): if uri in (inv, path.dirname(inv), path.dirname(inv) + '/'):
uri = path.dirname(newinv) uri = path.dirname(newinv)
line = f.readline().rstrip().decode('utf-8') with f:
try: try:
if line == '# Sphinx inventory version 1': join = localuri and path.join or posixpath.join
invdata = read_inventory_v1(f, uri, join) invdata = read_inventory(f, uri, join)
elif line == '# Sphinx inventory version 2': except ValueError:
invdata = read_inventory_v2(f, uri, join) raise ValueError('unknown or unsupported inventory version')
else:
raise ValueError
f.close()
except ValueError:
f.close()
raise ValueError('unknown or unsupported inventory version')
except Exception as err: except Exception as err:
app.warn('intersphinx inventory %r not readable due to ' app.warn('intersphinx inventory %r not readable due to '
'%s: %s' % (inv, err.__class__.__name__, err)) '%s: %s' % (inv, err.__class__.__name__, err))

View File

@ -17,7 +17,7 @@ from six import BytesIO
from docutils import nodes from docutils import nodes
from sphinx import addnodes from sphinx import addnodes
from sphinx.ext.intersphinx import read_inventory_v1, read_inventory_v2, \ from sphinx.ext.intersphinx import read_inventory, \
load_mappings, missing_reference, _strip_basic_auth, _read_from_url, \ load_mappings, missing_reference, _strip_basic_auth, _read_from_url, \
_get_safe_url, fetch_inventory, INVENTORY_FILENAME _get_safe_url, fetch_inventory, INVENTORY_FILENAME
@ -49,8 +49,7 @@ a term including:colon std:term -1 glossary.html#term-a-term-including-colon -
def test_read_inventory_v1(): def test_read_inventory_v1():
f = BytesIO(inventory_v1) f = BytesIO(inventory_v1)
f.readline() invdata = read_inventory(f, '/util', posixpath.join)
invdata = read_inventory_v1(f, '/util', posixpath.join)
assert invdata['py:module']['module'] == \ assert invdata['py:module']['module'] == \
('foo', '1.0', '/util/foo.html#module-module', '-') ('foo', '1.0', '/util/foo.html#module-module', '-')
assert invdata['py:class']['module.cls'] == \ assert invdata['py:class']['module.cls'] == \
@ -59,13 +58,11 @@ def test_read_inventory_v1():
def test_read_inventory_v2(): def test_read_inventory_v2():
f = BytesIO(inventory_v2) f = BytesIO(inventory_v2)
f.readline() invdata1 = read_inventory(f, '/util', posixpath.join)
invdata1 = read_inventory_v2(f, '/util', posixpath.join)
# try again with a small buffer size to test the chunking algorithm # try again with a small buffer size to test the chunking algorithm
f = BytesIO(inventory_v2) f = BytesIO(inventory_v2)
f.readline() invdata2 = read_inventory(f, '/util', posixpath.join, bufsize=5)
invdata2 = read_inventory_v2(f, '/util', posixpath.join, bufsize=5)
assert invdata1 == invdata2 assert invdata1 == invdata2