Skip to content

Commit 3f55056

Browse files
Revert "pythonGH-121970: Extract availability into a new extension (python#125082)"
This reverts commit cbfd392
1 parent 4ca9fc0 commit 3f55056

File tree

3 files changed

+166
-127
lines changed

3 files changed

+166
-127
lines changed

Doc/conf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
# Our custom Sphinx extensions are found in Doc/Tools/extensions/
2424
extensions = [
2525
'audit_events',
26-
'availability',
2726
'c_annotations',
2827
'changes',
2928
'glossary_search',

Doc/tools/extensions/availability.py

Lines changed: 0 additions & 125 deletions
This file was deleted.

Doc/tools/extensions/pyspecific.py

Lines changed: 166 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@
1212
import re
1313
import io
1414
from os import getenv, path
15+
from time import asctime
16+
from pprint import pformat
1517

1618
from docutils import nodes
19+
from docutils.io import StringOutput
1720
from docutils.parsers.rst import directives
18-
from docutils.utils import unescape
21+
from docutils.utils import new_document, unescape
1922
from sphinx import addnodes
23+
from sphinx.builders import Builder
24+
from sphinx.domains.changeset import VersionChange, versionlabels, versionlabel_classes
2025
from sphinx.domains.python import PyFunction, PyMethod, PyModule
2126
from sphinx.locale import _ as sphinx_gettext
2227
from sphinx.util.docutils import SphinxDirective
28+
from sphinx.writers.text import TextWriter, TextTranslator
29+
from sphinx.util.display import status_iterator
2330

2431

2532
ISSUE_URI = 'https://bugs.python.org/issue?@action=redirect&bpo=%s'
@@ -34,6 +41,16 @@
3441
Body.enum.converters['lowerroman'] = \
3542
Body.enum.converters['upperroman'] = lambda x: None
3643

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+
3754
# Support for marking up and linking to bugs.python.org issues
3855

3956
def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]):
@@ -90,6 +107,32 @@ def run(self):
90107
return [pnode]
91108

92109

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+
93136
class PyCoroutineMixin(object):
94137
def handle_signature(self, sig, signode):
95138
ret = super(PyCoroutineMixin, self).handle_signature(sig, signode)
@@ -141,6 +184,57 @@ def run(self):
141184
return PyMethod.run(self)
142185

143186

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+
144238
# Support for including Misc/NEWS
145239

146240
issue_re = re.compile('(?:[Ii]ssue #|bpo-)([0-9]+)', re.I)
@@ -181,6 +275,69 @@ def run(self):
181275
return []
182276

183277

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+
184341
# Support for documenting Opcodes
185342

186343
opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?')
@@ -260,9 +417,17 @@ def setup(app):
260417
app.add_role('issue', issue_role)
261418
app.add_role('gh', gh_issue_role)
262419
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)
263426
app.add_object_type('opcode', 'opcode', '%s (opcode)', parse_opcode_signature)
264427
app.add_object_type('pdbcommand', 'pdbcmd', '%s (pdb command)', parse_pdb_command)
265428
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)
266431
app.add_directive_to_domain('py', 'coroutinefunction', PyCoroutineFunction)
267432
app.add_directive_to_domain('py', 'coroutinemethod', PyCoroutineMethod)
268433
app.add_directive_to_domain('py', 'awaitablefunction', PyAwaitableFunction)

0 commit comments

Comments
 (0)