| 
12 | 12 | import re  | 
13 | 13 | import io  | 
14 | 14 | from os import getenv, path  | 
 | 15 | +from time import asctime  | 
 | 16 | +from pprint import pformat  | 
15 | 17 | 
 
  | 
16 | 18 | from docutils import nodes  | 
 | 19 | +from docutils.io import StringOutput  | 
17 | 20 | from docutils.parsers.rst import directives  | 
18 |  | -from docutils.utils import unescape  | 
 | 21 | +from docutils.utils import new_document, unescape  | 
19 | 22 | from sphinx import addnodes  | 
 | 23 | +from sphinx.builders import Builder  | 
20 | 24 | from sphinx.domains.python import PyFunction, PyMethod, PyModule  | 
21 | 25 | from sphinx.locale import _ as sphinx_gettext  | 
22 | 26 | from sphinx.util.docutils import SphinxDirective  | 
 | 27 | +from sphinx.writers.text import TextWriter, TextTranslator  | 
 | 28 | +from sphinx.util.display import status_iterator  | 
23 | 29 | 
 
  | 
24 | 30 | # Used in conf.py and updated here by python/release-tools/run_release.py  | 
25 | 31 | SOURCE_URI = 'https://github.com/python/cpython/tree/main/%s'  | 
@@ -51,6 +57,69 @@ def run(self):  | 
51 | 57 |         return PyMethod.run(self)  | 
52 | 58 | 
 
  | 
53 | 59 | 
 
  | 
 | 60 | +# Support for building "topic help" for pydoc  | 
 | 61 | + | 
 | 62 | +pydoc_topic_labels = [  | 
 | 63 | +    'assert', 'assignment', 'assignment-expressions', 'async',  'atom-identifiers',  | 
 | 64 | +    'atom-literals', 'attribute-access', 'attribute-references', 'augassign', 'await',  | 
 | 65 | +    'binary', 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object',  | 
 | 66 | +    'bltin-null-object', 'bltin-type-objects', 'booleans',  | 
 | 67 | +    'break', 'callable-types', 'calls', 'class', 'comparisons', 'compound',  | 
 | 68 | +    'context-managers', 'continue', 'conversions', 'customization', 'debugger',  | 
 | 69 | +    'del', 'dict', 'dynamic-features', 'else', 'exceptions', 'execmodel',  | 
 | 70 | +    'exprlists', 'floating', 'for', 'formatstrings', 'function', 'global',  | 
 | 71 | +    'id-classes', 'identifiers', 'if', 'imaginary', 'import', 'in', 'integers',  | 
 | 72 | +    'lambda', 'lists', 'naming', 'nonlocal', 'numbers', 'numeric-types',  | 
 | 73 | +    'objects', 'operator-summary', 'pass', 'power', 'raise', 'return',  | 
 | 74 | +    'sequence-types', 'shifting', 'slicings', 'specialattrs', 'specialnames',  | 
 | 75 | +    'string-methods', 'strings', 'subscriptions', 'truth', 'try', 'types',  | 
 | 76 | +    'typesfunctions', 'typesmapping', 'typesmethods', 'typesmodules',  | 
 | 77 | +    'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', 'yield'  | 
 | 78 | +]  | 
 | 79 | + | 
 | 80 | + | 
 | 81 | +class PydocTopicsBuilder(Builder):  | 
 | 82 | +    name = 'pydoc-topics'  | 
 | 83 | + | 
 | 84 | +    default_translator_class = TextTranslator  | 
 | 85 | + | 
 | 86 | +    def init(self):  | 
 | 87 | +        self.topics = {}  | 
 | 88 | +        self.secnumbers = {}  | 
 | 89 | + | 
 | 90 | +    def get_outdated_docs(self):  | 
 | 91 | +        return 'all pydoc topics'  | 
 | 92 | + | 
 | 93 | +    def get_target_uri(self, docname, typ=None):  | 
 | 94 | +        return ''  # no URIs  | 
 | 95 | + | 
 | 96 | +    def write(self, *ignored):  | 
 | 97 | +        writer = TextWriter(self)  | 
 | 98 | +        for label in status_iterator(pydoc_topic_labels,  | 
 | 99 | +                                     'building topics... ',  | 
 | 100 | +                                     length=len(pydoc_topic_labels)):  | 
 | 101 | +            if label not in self.env.domaindata['std']['labels']:  | 
 | 102 | +                self.env.logger.warning(f'label {label!r} not in documentation')  | 
 | 103 | +                continue  | 
 | 104 | +            docname, labelid, sectname = self.env.domaindata['std']['labels'][label]  | 
 | 105 | +            doctree = self.env.get_and_resolve_doctree(docname, self)  | 
 | 106 | +            document = new_document('<section node>')  | 
 | 107 | +            document.append(doctree.ids[labelid])  | 
 | 108 | +            destination = StringOutput(encoding='utf-8')  | 
 | 109 | +            writer.write(document, destination)  | 
 | 110 | +            self.topics[label] = writer.output  | 
 | 111 | + | 
 | 112 | +    def finish(self):  | 
 | 113 | +        f = open(path.join(self.outdir, 'topics.py'), 'wb')  | 
 | 114 | +        try:  | 
 | 115 | +            f.write('# -*- coding: utf-8 -*-\n'.encode('utf-8'))  | 
 | 116 | +            f.write(('# Autogenerated by Sphinx on %s\n' % asctime()).encode('utf-8'))  | 
 | 117 | +            f.write('# as part of the release process.\n'.encode('utf-8'))  | 
 | 118 | +            f.write(('topics = ' + pformat(self.topics) + '\n').encode('utf-8'))  | 
 | 119 | +        finally:  | 
 | 120 | +            f.close()  | 
 | 121 | + | 
 | 122 | + | 
54 | 123 | # Support for documenting Opcodes  | 
55 | 124 | 
 
  | 
56 | 125 | opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?')  | 
@@ -127,6 +196,7 @@ def patch_pairindextypes(app, _env) -> None:  | 
127 | 196 | 
 
  | 
128 | 197 | 
 
  | 
129 | 198 | def setup(app):  | 
 | 199 | +    app.add_builder(PydocTopicsBuilder)  | 
130 | 200 |     app.add_object_type('opcode', 'opcode', '%s (opcode)', parse_opcode_signature)  | 
131 | 201 |     app.add_object_type('pdbcommand', 'pdbcmd', '%s (pdb command)', parse_pdb_command)  | 
132 | 202 |     app.add_object_type('monitoring-event', 'monitoring-event', '%s (monitoring event)', parse_monitoring_event)  | 
 | 
0 commit comments