From: Vladimír Vondruš Date: Sun, 25 Aug 2019 08:49:17 +0000 (+0200) Subject: m.sphinx: utility to pretty-print the inventory files. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=ddb111aeaf65f21cfab25fa7f1fb185c5b973f4a;p=blog.git m.sphinx: utility to pretty-print the inventory files. The builtin sphinx tool is beyond useless. FFS. --- diff --git a/plugins/m/sphinx.py b/plugins/m/sphinx.py old mode 100644 new mode 100755 index 310fa82f..88b784ef --- a/plugins/m/sphinx.py +++ b/plugins/m/sphinx.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + # # This file is part of m.css. # @@ -22,6 +24,7 @@ # DEALINGS IN THE SOFTWARE. # +import argparse import logging import os import re @@ -36,8 +39,6 @@ from docutils.parsers.rst import directives from docutils.parsers.rst.roles import set_classes from docutils.parsers.rst.states import Inliner -from pelican import signals - referer_path = [] module_doc_output = None class_doc_output = None @@ -391,6 +392,38 @@ def _pelican_configure(pelicanobj): inventories=pelicanobj.settings.get('M_SPHINX_INVENTORIES', [])) def register(): # for Pelican + from pelican import signals + rst.roles.register_local_role('ref', ref) signals.initialized.connect(_pelican_configure) + +def pretty_print_intersphinx_inventory(file): + return ''.join([ + # Sphinx inventory version 2 + file.readline().decode('utf-8'), + # Project and version + file.readline().decode('utf-8'), + file.readline().decode('utf-8'), + # Zlib compression line + file.readline().decode('utf-8'), + # The rest, zlib-compressed + zlib.decompress(file.read()).decode('utf-8') + ]) + +if __name__ == '__main__': # pragma: no cover + parser = argparse.ArgumentParser() + parser.add_argument('file', help="inventory file to print") + parser.add_argument('--raw', help="show raw content", action='store_true') + parser.add_argument('--types', help="list all type keys", action='store_true') + args = parser.parse_args() + + if args.raw or not args.types: + with open(args.file, 'rb') as f: + print(pretty_print_intersphinx_inventory(f)) + + if args.types: + with open(args.file, 'rb') as f: + inventory = {} + parse_intersphinx_inventory(f, '', inventory, []) + print(inventory.keys())