|
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 |
| 24 | +from sphinx.domains.changeset import VersionChange, versionlabels, versionlabel_classes |
20 | 25 | from sphinx.domains.python import PyFunction, PyMethod, PyModule
|
21 | 26 | from sphinx.locale import _ as sphinx_gettext
|
22 | 27 | from sphinx.util.docutils import SphinxDirective
|
| 28 | +from sphinx.writers.text import TextWriter, TextTranslator |
| 29 | +from sphinx.util.display import status_iterator |
23 | 30 |
|
24 | 31 |
|
25 | 32 | ISSUE_URI = 'https://bugs.python.org/issue?@action=redirect&bpo=%s'
|
|
34 | 41 | Body.enum.converters['lowerroman'] = \
|
35 | 42 | Body.enum.converters['upperroman'] = lambda x: None
|
36 | 43 |
|
| 44 | +# monkey-patch the productionlist directive to allow hyphens in group names |
| 45 | +# https://github.com/sphinx-doc/sphinx/issues/11854 |
| 46 | +from sphinx.domains import std |
| 47 | + |
| 48 | +std.token_re = re.compile(r'`((~?[\w-]*:)?\w+)`') |
| 49 | + |
| 50 | +# backport :no-index: |
| 51 | +PyModule.option_spec['no-index'] = directives.flag |
| 52 | + |
| 53 | + |
37 | 54 | # Support for marking up and linking to bugs.python.org issues
|
38 | 55 |
|
39 | 56 | def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
|
@@ -90,6 +107,32 @@ def run(self):
|
90 | 107 | return [pnode]
|
91 | 108 |
|
92 | 109 |
|
| 110 | +# Support for documenting decorators |
| 111 | + |
| 112 | +class PyDecoratorMixin(object): |
| 113 | + def handle_signature(self, sig, signode): |
| 114 | + ret = super(PyDecoratorMixin, self).handle_signature(sig, signode) |
| 115 | + signode.insert(0, addnodes.desc_addname('@', '@')) |
| 116 | + return ret |
| 117 | + |
| 118 | + def needs_arglist(self): |
| 119 | + return False |
| 120 | + |
| 121 | + |
| 122 | +class PyDecoratorFunction(PyDecoratorMixin, PyFunction): |
| 123 | + def run(self): |
| 124 | + # a decorator function is a function after all |
| 125 | + self.name = 'py:function' |
| 126 | + return PyFunction.run(self) |
| 127 | + |
| 128 | + |
| 129 | +# TODO: Use sphinx.domains.python.PyDecoratorMethod when possible |
| 130 | +class PyDecoratorMethod(PyDecoratorMixin, PyMethod): |
| 131 | + def run(self): |
| 132 | + self.name = 'py:method' |
| 133 | + return PyMethod.run(self) |
| 134 | + |
| 135 | + |
93 | 136 | class PyCoroutineMixin(object):
|
94 | 137 | def handle_signature(self, sig, signode):
|
95 | 138 | ret = super(PyCoroutineMixin, self).handle_signature(sig, signode)
|
@@ -141,6 +184,57 @@ def run(self):
|
141 | 184 | return PyMethod.run(self)
|
142 | 185 |
|
143 | 186 |
|
| 187 | +# Support for documenting version of changes, additions, deprecations |
| 188 | + |
| 189 | +def expand_version_arg(argument, release): |
| 190 | + """Expand "next" to the current version""" |
| 191 | + if argument == 'next': |
| 192 | + return sphinx_gettext('{} (unreleased)').format(release) |
| 193 | + return argument |
| 194 | + |
| 195 | + |
| 196 | +class PyVersionChange(VersionChange): |
| 197 | + def run(self): |
| 198 | + # Replace the 'next' special token with the current development version |
| 199 | + self.arguments[0] = expand_version_arg(self.arguments[0], |
| 200 | + self.config.release) |
| 201 | + return super().run() |
| 202 | + |
| 203 | + |
| 204 | +class DeprecatedRemoved(VersionChange): |
| 205 | + required_arguments = 2 |
| 206 | + |
| 207 | + _deprecated_label = sphinx_gettext('Deprecated since version %s, will be removed in version %s') |
| 208 | + _removed_label = sphinx_gettext('Deprecated since version %s, removed in version %s') |
| 209 | + |
| 210 | + def run(self): |
| 211 | + # Replace the first two arguments (deprecated version and removed version) |
| 212 | + # with a single tuple of both versions. |
| 213 | + version_deprecated = expand_version_arg(self.arguments[0], |
| 214 | + self.config.release) |
| 215 | + version_removed = self.arguments.pop(1) |
| 216 | + if version_removed == 'next': |
| 217 | + raise ValueError( |
| 218 | + 'deprecated-removed:: second argument cannot be `next`') |
| 219 | + self.arguments[0] = version_deprecated, version_removed |
| 220 | + |
| 221 | + # Set the label based on if we have reached the removal version |
| 222 | + current_version = tuple(map(int, self.config.version.split('.'))) |
| 223 | + removed_version = tuple(map(int, version_removed.split('.'))) |
| 224 | + if current_version < removed_version: |
| 225 | + versionlabels[self.name] = self._deprecated_label |
| 226 | + versionlabel_classes[self.name] = 'deprecated' |
| 227 | + else: |
| 228 | + versionlabels[self.name] = self._removed_label |
| 229 | + versionlabel_classes[self.name] = 'removed' |
| 230 | + try: |
| 231 | + return super().run() |
| 232 | + finally: |
| 233 | + # reset versionlabels and versionlabel_classes |
| 234 | + versionlabels[self.name] = '' |
| 235 | + versionlabel_classes[self.name] = '' |
| 236 | + |
| 237 | + |
144 | 238 | # Support for including Misc/NEWS
|
145 | 239 |
|
146 | 240 | issue_re = re.compile('(?:[Ii]ssue #|bpo-)([0-9]+)', re.I)
|
@@ -181,6 +275,69 @@ def run(self):
|
181 | 275 | return []
|
182 | 276 |
|
183 | 277 |
|
| 278 | +# Support for building "topic help" for pydoc |
| 279 | + |
| 280 | +pydoc_topic_labels = [ |
| 281 | + 'assert', 'assignment', 'assignment-expressions', 'async', 'atom-identifiers', |
| 282 | + 'atom-literals', 'attribute-access', 'attribute-references', 'augassign', 'await', |
| 283 | + 'binary', 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object', |
| 284 | + 'bltin-null-object', 'bltin-type-objects', 'booleans', |
| 285 | + 'break', 'callable-types', 'calls', 'class', 'comparisons', 'compound', |
| 286 | + 'context-managers', 'continue', 'conversions', 'customization', 'debugger', |
| 287 | + 'del', 'dict', 'dynamic-features', 'else', 'exceptions', 'execmodel', |
| 288 | + 'exprlists', 'floating', 'for', 'formatstrings', 'function', 'global', |
| 289 | + 'id-classes', 'identifiers', 'if', 'imaginary', 'import', 'in', 'integers', |
| 290 | + 'lambda', 'lists', 'naming', 'nonlocal', 'numbers', 'numeric-types', |
| 291 | + 'objects', 'operator-summary', 'pass', 'power', 'raise', 'return', |
| 292 | + 'sequence-types', 'shifting', 'slicings', 'specialattrs', 'specialnames', |
| 293 | + 'string-methods', 'strings', 'subscriptions', 'truth', 'try', 'types', |
| 294 | + 'typesfunctions', 'typesmapping', 'typesmethods', 'typesmodules', |
| 295 | + 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', 'yield' |
| 296 | +] |
| 297 | + |
| 298 | + |
| 299 | +class PydocTopicsBuilder(Builder): |
| 300 | + name = 'pydoc-topics' |
| 301 | + |
| 302 | + default_translator_class = TextTranslator |
| 303 | + |
| 304 | + def init(self): |
| 305 | + self.topics = {} |
| 306 | + self.secnumbers = {} |
| 307 | + |
| 308 | + def get_outdated_docs(self): |
| 309 | + return 'all pydoc topics' |
| 310 | + |
| 311 | + def get_target_uri(self, docname, typ=None): |
| 312 | + return '' # no URIs |
| 313 | + |
| 314 | + def write(self, *ignored): |
| 315 | + writer = TextWriter(self) |
| 316 | + for label in status_iterator(pydoc_topic_labels, |
| 317 | + 'building topics... ', |
| 318 | + length=len(pydoc_topic_labels)): |
| 319 | + if label not in self.env.domaindata['std']['labels']: |
| 320 | + self.env.logger.warning(f'label {label!r} not in documentation') |
| 321 | + continue |
| 322 | + docname, labelid, sectname = self.env.domaindata['std']['labels'][label] |
| 323 | + doctree = self.env.get_and_resolve_doctree(docname, self) |
| 324 | + document = new_document('<section node>') |
| 325 | + document.append(doctree.ids[labelid]) |
| 326 | + destination = StringOutput(encoding='utf-8') |
| 327 | + writer.write(document, destination) |
| 328 | + self.topics[label] = writer.output |
| 329 | + |
| 330 | + def finish(self): |
| 331 | + f = open(path.join(self.outdir, 'topics.py'), 'wb') |
| 332 | + try: |
| 333 | + f.write('# -*- coding: utf-8 -*-\n'.encode('utf-8')) |
| 334 | + f.write(('# Autogenerated by Sphinx on %s\n' % asctime()).encode('utf-8')) |
| 335 | + f.write('# as part of the release process.\n'.encode('utf-8')) |
| 336 | + f.write(('topics = ' + pformat(self.topics) + '\n').encode('utf-8')) |
| 337 | + finally: |
| 338 | + f.close() |
| 339 | + |
| 340 | + |
184 | 341 | # Support for documenting Opcodes
|
185 | 342 |
|
186 | 343 | opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?')
|
@@ -260,9 +417,17 @@ def setup(app):
|
260 | 417 | app.add_role('issue', issue_role)
|
261 | 418 | app.add_role('gh', gh_issue_role)
|
262 | 419 | app.add_directive('impl-detail', ImplementationDetail)
|
| 420 | + app.add_directive('versionadded', PyVersionChange, override=True) |
| 421 | + app.add_directive('versionchanged', PyVersionChange, override=True) |
| 422 | + app.add_directive('versionremoved', PyVersionChange, override=True) |
| 423 | + app.add_directive('deprecated', PyVersionChange, override=True) |
| 424 | + app.add_directive('deprecated-removed', DeprecatedRemoved) |
| 425 | + app.add_builder(PydocTopicsBuilder) |
263 | 426 | app.add_object_type('opcode', 'opcode', '%s (opcode)', parse_opcode_signature)
|
264 | 427 | app.add_object_type('pdbcommand', 'pdbcmd', '%s (pdb command)', parse_pdb_command)
|
265 | 428 | app.add_object_type('monitoring-event', 'monitoring-event', '%s (monitoring event)', parse_monitoring_event)
|
| 429 | + app.add_directive_to_domain('py', 'decorator', PyDecoratorFunction) |
| 430 | + app.add_directive_to_domain('py', 'decoratormethod', PyDecoratorMethod) |
266 | 431 | app.add_directive_to_domain('py', 'coroutinefunction', PyCoroutineFunction)
|
267 | 432 | app.add_directive_to_domain('py', 'coroutinemethod', PyCoroutineMethod)
|
268 | 433 | app.add_directive_to_domain('py', 'awaitablefunction', PyAwaitableFunction)
|
|
0 commit comments