2008-04-10 03:06:53 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
pyspecific.py
|
|
|
|
|
~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Sphinx extension with Python doc-specific markup.
|
|
|
|
|
|
2014-01-21 19:20:31 +01:00
|
|
|
:copyright: 2008-2014 by Georg Brandl.
|
2008-04-10 03:06:53 +00:00
|
|
|
:license: Python license.
|
|
|
|
|
"""
|
|
|
|
|
|
2014-10-30 22:55:13 +01:00
|
|
|
import re
|
2017-06-16 08:59:01 +02:00
|
|
|
import io
|
2018-12-19 18:20:06 -08:00
|
|
|
from os import getenv, path
|
2023-06-23 08:58:45 +01:00
|
|
|
|
2024-07-19 08:33:51 +01:00
|
|
|
from docutils import nodes
|
2024-07-20 20:46:41 +01:00
|
|
|
from docutils.parsers.rst import directives
|
2025-03-19 18:35:11 +00:00
|
|
|
from docutils.utils import unescape
|
2014-10-30 22:55:13 +01:00
|
|
|
from sphinx import addnodes
|
2024-07-20 20:46:41 +01:00
|
|
|
from sphinx.domains.python import PyFunction, PyMethod, PyModule
|
2022-11-11 09:18:11 +08:00
|
|
|
from sphinx.locale import _ as sphinx_gettext
|
2023-02-12 15:20:11 +01:00
|
|
|
from sphinx.util.docutils import SphinxDirective
|
2014-10-30 22:55:13 +01:00
|
|
|
|
2024-04-15 21:22:00 +03:00
|
|
|
# Used in conf.py and updated here by python/release-tools/run_release.py
|
2025-05-07 20:46:41 +03:00
|
|
|
SOURCE_URI = 'https://github.com/python/cpython/tree/main/%s'
|
2008-04-10 03:06:53 +00:00
|
|
|
|
2018-09-18 17:55:44 -04:00
|
|
|
class PyAwaitableMixin(object):
|
|
|
|
|
def handle_signature(self, sig, signode):
|
|
|
|
|
ret = super(PyAwaitableMixin, self).handle_signature(sig, signode)
|
|
|
|
|
signode.insert(0, addnodes.desc_annotation('awaitable ', 'awaitable '))
|
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
2020-09-18 18:22:36 +09:00
|
|
|
class PyAwaitableFunction(PyAwaitableMixin, PyFunction):
|
2018-09-18 17:55:44 -04:00
|
|
|
def run(self):
|
|
|
|
|
self.name = 'py:function'
|
2020-09-18 18:22:36 +09:00
|
|
|
return PyFunction.run(self)
|
2018-09-18 17:55:44 -04:00
|
|
|
|
|
|
|
|
|
2020-09-18 18:22:36 +09:00
|
|
|
class PyAwaitableMethod(PyAwaitableMixin, PyMethod):
|
2018-09-18 17:55:44 -04:00
|
|
|
def run(self):
|
|
|
|
|
self.name = 'py:method'
|
2020-09-18 18:22:36 +09:00
|
|
|
return PyMethod.run(self)
|
2018-09-18 17:55:44 -04:00
|
|
|
|
|
|
|
|
|
2008-07-23 15:19:11 +00:00
|
|
|
# Support for documenting Opcodes
|
|
|
|
|
|
2010-07-03 10:41:33 +00:00
|
|
|
opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?')
|
2008-07-23 15:19:11 +00:00
|
|
|
|
2014-10-30 22:55:13 +01:00
|
|
|
|
2008-07-23 15:19:11 +00:00
|
|
|
def parse_opcode_signature(env, sig, signode):
|
|
|
|
|
"""Transform an opcode signature into RST nodes."""
|
|
|
|
|
m = opcode_sig_re.match(sig)
|
|
|
|
|
if m is None:
|
|
|
|
|
raise ValueError
|
|
|
|
|
opname, arglist = m.groups()
|
|
|
|
|
signode += addnodes.desc_name(opname, opname)
|
2010-07-03 10:41:33 +00:00
|
|
|
if arglist is not None:
|
|
|
|
|
paramlist = addnodes.desc_parameterlist()
|
|
|
|
|
signode += paramlist
|
|
|
|
|
paramlist += addnodes.desc_parameter(arglist, arglist)
|
2008-07-23 15:19:11 +00:00
|
|
|
return opname.strip()
|
|
|
|
|
|
|
|
|
|
|
2010-11-12 08:57:12 +00:00
|
|
|
# Support for documenting pdb commands
|
|
|
|
|
|
2010-07-18 10:11:03 +00:00
|
|
|
pdbcmd_sig_re = re.compile(r'([a-z()!]+)\s*(.*)')
|
|
|
|
|
|
|
|
|
|
# later...
|
2014-10-30 22:55:13 +01:00
|
|
|
# pdbargs_tokens_re = re.compile(r'''[a-zA-Z]+ | # identifiers
|
2010-07-18 10:11:03 +00:00
|
|
|
# [.,:]+ | # punctuation
|
|
|
|
|
# [\[\]()] | # parens
|
|
|
|
|
# \s+ # whitespace
|
|
|
|
|
# ''', re.X)
|
|
|
|
|
|
2014-10-30 22:55:13 +01:00
|
|
|
|
2010-07-18 10:11:03 +00:00
|
|
|
def parse_pdb_command(env, sig, signode):
|
|
|
|
|
"""Transform a pdb command signature into RST nodes."""
|
|
|
|
|
m = pdbcmd_sig_re.match(sig)
|
|
|
|
|
if m is None:
|
|
|
|
|
raise ValueError
|
|
|
|
|
name, args = m.groups()
|
|
|
|
|
fullname = name.replace('(', '').replace(')', '')
|
|
|
|
|
signode += addnodes.desc_name(name, name)
|
|
|
|
|
if args:
|
|
|
|
|
signode += addnodes.desc_addname(' '+args, ' '+args)
|
|
|
|
|
return fullname
|
|
|
|
|
|
|
|
|
|
|
2023-10-18 16:15:42 -07:00
|
|
|
def parse_monitoring_event(env, sig, signode):
|
|
|
|
|
"""Transform a monitoring event signature into RST nodes."""
|
|
|
|
|
signode += addnodes.desc_addname('sys.monitoring.events.', 'sys.monitoring.events.')
|
|
|
|
|
signode += addnodes.desc_name(sig, sig)
|
|
|
|
|
return sig
|
|
|
|
|
|
|
|
|
|
|
2023-05-08 20:01:25 +01:00
|
|
|
def patch_pairindextypes(app, _env) -> None:
|
|
|
|
|
"""Remove all entries from ``pairindextypes`` before writing POT files.
|
|
|
|
|
|
|
|
|
|
We want to run this just before writing output files, as the check to
|
|
|
|
|
circumvent is in ``I18nBuilder.write_doc()``.
|
|
|
|
|
As such, we link this to ``env-check-consistency``, even though it has
|
|
|
|
|
nothing to do with the environment consistency check.
|
|
|
|
|
"""
|
2023-05-04 08:11:09 +01:00
|
|
|
if app.builder.name != 'gettext':
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# allow translating deprecated index entries
|
|
|
|
|
try:
|
|
|
|
|
from sphinx.domains.python import pairindextypes
|
|
|
|
|
except ImportError:
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
# Sphinx checks if a 'pair' type entry on an index directive is one of
|
|
|
|
|
# the Sphinx-translated pairindextypes values. As we intend to move
|
|
|
|
|
# away from this, we need Sphinx to believe that these values don't
|
|
|
|
|
# exist, by deleting them when using the gettext builder.
|
2023-05-08 20:01:25 +01:00
|
|
|
pairindextypes.clear()
|
2023-05-04 08:11:09 +01:00
|
|
|
|
|
|
|
|
|
2008-04-10 03:06:53 +00:00
|
|
|
def setup(app):
|
2018-10-13 08:14:08 +02:00
|
|
|
app.add_object_type('opcode', 'opcode', '%s (opcode)', parse_opcode_signature)
|
|
|
|
|
app.add_object_type('pdbcommand', 'pdbcmd', '%s (pdb command)', parse_pdb_command)
|
2023-10-18 16:15:42 -07:00
|
|
|
app.add_object_type('monitoring-event', 'monitoring-event', '%s (monitoring event)', parse_monitoring_event)
|
2018-09-18 17:55:44 -04:00
|
|
|
app.add_directive_to_domain('py', 'awaitablefunction', PyAwaitableFunction)
|
|
|
|
|
app.add_directive_to_domain('py', 'awaitablemethod', PyAwaitableMethod)
|
2023-05-08 20:01:25 +01:00
|
|
|
app.connect('env-check-consistency', patch_pairindextypes)
|
2014-09-30 22:17:41 +02:00
|
|
|
return {'version': '1.0', 'parallel_read_safe': True}
|