Skip to content

Instantly share code, notes, and snippets.

@acsr
Last active January 5, 2021 01:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save acsr/fb7b5cf97627f70876ceeae031cd37f1 to your computer and use it in GitHub Desktop.
Save acsr/fb7b5cf97627f70876ceeae031cd37f1 to your computer and use it in GitHub Desktop.
patch sphinx.ext.intersphinx output tweaking the main function to using almost csv compatible tab separators instead of columns adjusted by spaces
#!./bin/python
# -*- coding: utf-8 -*-
"""
inspect_objects
based on and requiring sphinx.ext.intersphinx
~~~~~~~~~~~~~~~~~~~~~~
List links to objects documented in as build Sphinx documentation.
Usage: ./bin/python inspect_objects.py _build/html/objects.inv > objects_inventory.csv
This works as follows:
* Each Sphinx HTML build creates a file named "objects.inv" that contains a
mapping from object names to URIs relative to the HTML set's root.
* This is a modified call of the main function of intersphinx to export the
List of object as pure tab delimited file instead of the aligned text with
justified spaces. It is much better suitable for postproceessing than the
regular output aiming at programmers for a quick look during debugging.
* By default, the mapping file is assumed to be at the same location as the
rest of the documentation; however, the location of the mapping file can
also be specified individually, e.g. if the docs should be buildable
without Internet access.
:copyright: Copyright of the source sphinx.ext.intersphinx 2007-2018 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from __future__ import print_function
import functools
import posixpath
import sys
import time
import warnings
from os import path
from docutils import nodes
from docutils.utils import relative_path
from six import PY3, iteritems, string_types
from six.moves.urllib.parse import urlsplit, urlunsplit
import sphinx
from sphinx.builders.html import INVENTORY_FILENAME
from sphinx.deprecation import RemovedInSphinx20Warning
from sphinx.locale import _, __
from sphinx.util import requests, logging
from sphinx.util.inventory import InventoryFile
from sphinx.ext.intersphinx import *
if False:
# For type annotation
from typing import Any, Dict, IO, List, Tuple, Union # NOQA
from sphinx.application import Sphinx # NOQA
from sphinx.config import Config # NOQA
from sphinx.environment import BuildEnvironment # NOQA
if PY3:
unicode = str
Inventory = Dict[unicode, Dict[unicode, Tuple[unicode, unicode, unicode, unicode]]]
logger = logging.getLogger(__name__)
def inspect_main_csv(argv):
# type: (List[unicode]) -> None
"""Debug functionality to print out an inventory"""
if len(argv) < 1:
print("Print out an inventory file.\n"
"Error: must specify local path or URL to an inventory file.",
file=sys.stderr)
sys.exit(1)
class MockConfig(object):
intersphinx_timeout = None # type: int
tls_verify = False
class MockApp(object):
srcdir = ''
config = MockConfig()
def warn(self, msg):
# type: (unicode) -> None
print(msg, file=sys.stderr)
try:
filename = argv[0]
invdata = fetch_inventory(MockApp(), '', filename) # type: ignore
for key in sorted(invdata or {}):
print(key)
for entry, einfo in sorted(invdata[key].items()):
print('\t%s\t%s\t%s' % (entry,
einfo[3] != '-' and '%s' % einfo[3] or '',
einfo[2]))
except ValueError as exc:
print(exc.args[0] % exc.args[1:])
except Exception as exc:
print('Unknown error: %r' % exc)
if __name__ == '__main__':
import logging # type: ignore
logging.basicConfig() # type: ignore
inspect_main_csv(argv=sys.argv[1:]) # type: ignore
@acsr
Copy link
Author

acsr commented Jan 27, 2019

Changed the print output to create pure tab delimited output. (Compare to Gist revision 1 original version) https://gist.github.com/acsr/fb7b5cf97627f70876ceeae031cd37f1/revisions

@acsr
Copy link
Author

acsr commented Apr 15, 2019

Replaced the patched version of https://github.com/sphinx-doc/sphinx/blob/master/sphinx/ext/intersphinx.py by the inspect_objects.py script calling the regular module and tweak the main function during runtime.

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