Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
| 2 | """ |
| 3 | pyspecific.py |
| 4 | ~~~~~~~~~~~~~ |
| 5 | |
| 6 | Sphinx extension with Python doc-specific markup. |
| 7 | |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 8 | :copyright: 2008-2014 by Georg Brandl. |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 9 | :license: Python license. |
| 10 | """ |
| 11 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 12 | import re |
Victor Stinner | 272d888 | 2017-06-16 08:59:01 +0200 | [diff] [blame] | 13 | import io |
Steve Dower | afe17a7 | 2018-12-19 18:20:06 -0800 | [diff] [blame] | 14 | from os import getenv, path |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 15 | from time import asctime |
| 16 | from pprint import pformat |
| 17 | from docutils.io import StringOutput |
Ned Deily | 50f5816 | 2017-07-15 15:28:02 -0400 | [diff] [blame] | 18 | from docutils.parsers.rst import Directive |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 19 | from docutils.utils import new_document |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 20 | |
| 21 | from docutils import nodes, utils |
Georg Brandl | 239990d | 2013-10-12 20:50:21 +0200 | [diff] [blame] | 22 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 23 | from sphinx import addnodes |
| 24 | from sphinx.builders import Builder |
Julien Palard | 63c98ed | 2019-09-09 12:54:56 +0200 | [diff] [blame^] | 25 | try: |
| 26 | from sphinx.errors import NoUri |
| 27 | except ImportError: |
| 28 | from sphinx.environment import NoUri |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 29 | from sphinx.locale import translators |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 30 | from sphinx.util import status_iterator, logging |
Georg Brandl | 6881886 | 2010-11-06 07:19:35 +0000 | [diff] [blame] | 31 | from sphinx.util.nodes import split_explicit_title |
Georg Brandl | 239990d | 2013-10-12 20:50:21 +0200 | [diff] [blame] | 32 | from sphinx.writers.html import HTMLTranslator |
Benjamin Peterson | acfb087 | 2018-04-16 22:56:46 -0700 | [diff] [blame] | 33 | from sphinx.writers.text import TextWriter, TextTranslator |
Georg Brandl | 239990d | 2013-10-12 20:50:21 +0200 | [diff] [blame] | 34 | from sphinx.writers.latex import LaTeXTranslator |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 35 | from sphinx.domains.python import PyModulelevel, PyClassmember |
| 36 | |
| 37 | # Support for checking for suspicious markup |
| 38 | |
| 39 | import suspicious |
| 40 | |
| 41 | |
| 42 | ISSUE_URI = 'https://bugs.python.org/issue%s' |
Łukasz Langa | 9ab2fb1 | 2019-06-04 22:12:32 +0200 | [diff] [blame] | 43 | SOURCE_URI = 'https://github.com/python/cpython/tree/master/%s' |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 44 | |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 45 | # monkey-patch reST parser to disable alphabetic and roman enumerated lists |
| 46 | from docutils.parsers.rst.states import Body |
| 47 | Body.enum.converters['loweralpha'] = \ |
| 48 | Body.enum.converters['upperalpha'] = \ |
| 49 | Body.enum.converters['lowerroman'] = \ |
| 50 | Body.enum.converters['upperroman'] = lambda x: None |
| 51 | |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 52 | # monkey-patch HTML and LaTeX translators to keep doctest blocks in the |
| 53 | # doctest docs themselves |
| 54 | orig_visit_literal_block = HTMLTranslator.visit_literal_block |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 55 | orig_depart_literal_block = LaTeXTranslator.depart_literal_block |
| 56 | |
| 57 | |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 58 | def new_visit_literal_block(self, node): |
| 59 | meta = self.builder.env.metadata[self.builder.current_docname] |
| 60 | old_trim_doctest_flags = self.highlighter.trim_doctest_flags |
| 61 | if 'keepdoctest' in meta: |
| 62 | self.highlighter.trim_doctest_flags = False |
| 63 | try: |
| 64 | orig_visit_literal_block(self, node) |
| 65 | finally: |
| 66 | self.highlighter.trim_doctest_flags = old_trim_doctest_flags |
| 67 | |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 68 | |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 69 | def new_depart_literal_block(self, node): |
| 70 | meta = self.builder.env.metadata[self.curfilestack[-1]] |
| 71 | old_trim_doctest_flags = self.highlighter.trim_doctest_flags |
| 72 | if 'keepdoctest' in meta: |
| 73 | self.highlighter.trim_doctest_flags = False |
| 74 | try: |
| 75 | orig_depart_literal_block(self, node) |
| 76 | finally: |
| 77 | self.highlighter.trim_doctest_flags = old_trim_doctest_flags |
| 78 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 79 | |
| 80 | HTMLTranslator.visit_literal_block = new_visit_literal_block |
Georg Brandl | 83e51f4 | 2012-10-10 16:45:11 +0200 | [diff] [blame] | 81 | LaTeXTranslator.depart_literal_block = new_depart_literal_block |
Benjamin Peterson | 5c6d787 | 2009-02-06 02:40:07 +0000 | [diff] [blame] | 82 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 83 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 84 | # Support for marking up and linking to bugs.python.org issues |
| 85 | |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 86 | def issue_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): |
| 87 | issue = utils.unescape(text) |
Brett Cannon | 79ab8be | 2017-02-10 15:10:13 -0800 | [diff] [blame] | 88 | text = 'bpo-' + issue |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 89 | refnode = nodes.reference(text, text, refuri=ISSUE_URI % issue) |
| 90 | return [refnode], [] |
| 91 | |
| 92 | |
Georg Brandl | 6881886 | 2010-11-06 07:19:35 +0000 | [diff] [blame] | 93 | # Support for linking to Python source files easily |
| 94 | |
| 95 | def source_role(typ, rawtext, text, lineno, inliner, options={}, content=[]): |
| 96 | has_t, title, target = split_explicit_title(text) |
| 97 | title = utils.unescape(title) |
| 98 | target = utils.unescape(target) |
| 99 | refnode = nodes.reference(title, title, refuri=SOURCE_URI % target) |
| 100 | return [refnode], [] |
| 101 | |
| 102 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 103 | # Support for marking up implementation details |
| 104 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 105 | class ImplementationDetail(Directive): |
| 106 | |
| 107 | has_content = True |
| 108 | required_arguments = 0 |
| 109 | optional_arguments = 1 |
| 110 | final_argument_whitespace = True |
| 111 | |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 112 | # This text is copied to templates/dummy.html |
| 113 | label_text = 'CPython implementation detail:' |
| 114 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 115 | def run(self): |
| 116 | pnode = nodes.compound(classes=['impl-detail']) |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 117 | label = translators['sphinx'].gettext(self.label_text) |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 118 | content = self.content |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 119 | add_text = nodes.strong(label, label) |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 120 | if self.arguments: |
| 121 | n, m = self.state.inline_text(self.arguments[0], self.lineno) |
| 122 | pnode.append(nodes.paragraph('', '', *(n + m))) |
| 123 | self.state.nested_parse(content, self.content_offset, pnode) |
| 124 | if pnode.children and isinstance(pnode[0], nodes.paragraph): |
INADA Naoki | c351ce6 | 2017-03-08 19:07:13 +0900 | [diff] [blame] | 125 | content = nodes.inline(pnode[0].rawsource, translatable=True) |
| 126 | content.source = pnode[0].source |
| 127 | content.line = pnode[0].line |
| 128 | content += pnode[0].children |
| 129 | pnode[0].replace_self(nodes.paragraph('', '', content, |
| 130 | translatable=False)) |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 131 | pnode[0].insert(0, add_text) |
| 132 | pnode[0].insert(1, nodes.Text(' ')) |
| 133 | else: |
| 134 | pnode.insert(0, nodes.paragraph('', '', add_text)) |
| 135 | return [pnode] |
| 136 | |
| 137 | |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 138 | # Support for documenting platform availability |
| 139 | |
| 140 | class Availability(Directive): |
| 141 | |
| 142 | has_content = False |
| 143 | required_arguments = 1 |
| 144 | optional_arguments = 0 |
| 145 | final_argument_whitespace = True |
| 146 | |
| 147 | def run(self): |
Julien Palard | beed84c | 2018-11-07 22:42:40 +0100 | [diff] [blame] | 148 | availability_ref = ':ref:`Availability <availability>`: ' |
| 149 | pnode = nodes.paragraph(availability_ref + self.arguments[0], |
| 150 | classes=["availability"],) |
| 151 | n, m = self.state.inline_text(availability_ref, self.lineno) |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 152 | pnode.extend(n + m) |
| 153 | n, m = self.state.inline_text(self.arguments[0], self.lineno) |
| 154 | pnode.extend(n + m) |
| 155 | return [pnode] |
| 156 | |
| 157 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 158 | # Support for documenting audit event |
| 159 | |
| 160 | class AuditEvent(Directive): |
| 161 | |
| 162 | has_content = True |
| 163 | required_arguments = 1 |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 164 | optional_arguments = 2 |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 165 | final_argument_whitespace = True |
| 166 | |
| 167 | _label = [ |
| 168 | "Raises an :ref:`auditing event <auditing>` {name} with no arguments.", |
| 169 | "Raises an :ref:`auditing event <auditing>` {name} with argument {args}.", |
| 170 | "Raises an :ref:`auditing event <auditing>` {name} with arguments {args}.", |
| 171 | ] |
| 172 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 173 | @property |
| 174 | def logger(self): |
| 175 | cls = type(self) |
| 176 | return logging.getLogger(cls.__module__ + "." + cls.__name__) |
| 177 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 178 | def run(self): |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 179 | name = self.arguments[0] |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 180 | if len(self.arguments) >= 2 and self.arguments[1]: |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 181 | args = (a.strip() for a in self.arguments[1].strip("'\"").split(",")) |
| 182 | args = [a for a in args if a] |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 183 | else: |
| 184 | args = [] |
| 185 | |
| 186 | label = translators['sphinx'].gettext(self._label[min(2, len(args))]) |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 187 | text = label.format(name="``{}``".format(name), |
| 188 | args=", ".join("``{}``".format(a) for a in args if a)) |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 189 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 190 | env = self.state.document.settings.env |
| 191 | if not hasattr(env, 'all_audit_events'): |
| 192 | env.all_audit_events = {} |
| 193 | |
| 194 | new_info = { |
| 195 | 'source': [], |
| 196 | 'args': args |
| 197 | } |
| 198 | info = env.all_audit_events.setdefault(name, new_info) |
| 199 | if info is not new_info: |
| 200 | if not self._do_args_match(info['args'], new_info['args']): |
| 201 | self.logger.warn( |
| 202 | "Mismatched arguments for audit-event {}: {!r} != {!r}" |
| 203 | .format(name, info['args'], new_info['args']) |
| 204 | ) |
| 205 | |
Steve Dower | e226e83 | 2019-07-01 16:03:53 -0700 | [diff] [blame] | 206 | ids = [] |
| 207 | try: |
| 208 | target = self.arguments[2].strip("\"'") |
| 209 | except (IndexError, TypeError): |
| 210 | target = None |
| 211 | if not target: |
| 212 | target = "audit_event_{}_{}".format( |
| 213 | re.sub(r'\W', '_', name), |
| 214 | len(info['source']), |
| 215 | ) |
| 216 | ids.append(target) |
| 217 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 218 | info['source'].append((env.docname, target)) |
| 219 | |
| 220 | pnode = nodes.paragraph(text, classes=["audit-hook"], ids=ids) |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 221 | if self.content: |
| 222 | self.state.nested_parse(self.content, self.content_offset, pnode) |
| 223 | else: |
| 224 | n, m = self.state.inline_text(text, self.lineno) |
| 225 | pnode.extend(n + m) |
| 226 | |
| 227 | return [pnode] |
| 228 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 229 | # This list of sets are allowable synonyms for event argument names. |
| 230 | # If two names are in the same set, they are treated as equal for the |
| 231 | # purposes of warning. This won't help if number of arguments is |
| 232 | # different! |
| 233 | _SYNONYMS = [ |
| 234 | {"file", "path", "fd"}, |
| 235 | ] |
| 236 | |
| 237 | def _do_args_match(self, args1, args2): |
| 238 | if args1 == args2: |
| 239 | return True |
| 240 | if len(args1) != len(args2): |
| 241 | return False |
| 242 | for a1, a2 in zip(args1, args2): |
| 243 | if a1 == a2: |
| 244 | continue |
| 245 | if any(a1 in s and a2 in s for s in self._SYNONYMS): |
| 246 | continue |
| 247 | return False |
| 248 | return True |
| 249 | |
| 250 | |
| 251 | class audit_event_list(nodes.General, nodes.Element): |
| 252 | pass |
| 253 | |
| 254 | |
| 255 | class AuditEventListDirective(Directive): |
| 256 | |
| 257 | def run(self): |
| 258 | return [audit_event_list('')] |
| 259 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 260 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 261 | # Support for documenting decorators |
| 262 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 263 | class PyDecoratorMixin(object): |
| 264 | def handle_signature(self, sig, signode): |
| 265 | ret = super(PyDecoratorMixin, self).handle_signature(sig, signode) |
| 266 | signode.insert(0, addnodes.desc_addname('@', '@')) |
| 267 | return ret |
| 268 | |
| 269 | def needs_arglist(self): |
| 270 | return False |
| 271 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 272 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 273 | class PyDecoratorFunction(PyDecoratorMixin, PyModulelevel): |
| 274 | def run(self): |
| 275 | # a decorator function is a function after all |
| 276 | self.name = 'py:function' |
| 277 | return PyModulelevel.run(self) |
| 278 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 279 | |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 280 | class PyDecoratorMethod(PyDecoratorMixin, PyClassmember): |
| 281 | def run(self): |
| 282 | self.name = 'py:method' |
| 283 | return PyClassmember.run(self) |
| 284 | |
| 285 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 286 | class PyCoroutineMixin(object): |
| 287 | def handle_signature(self, sig, signode): |
| 288 | ret = super(PyCoroutineMixin, self).handle_signature(sig, signode) |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 289 | signode.insert(0, addnodes.desc_annotation('coroutine ', 'coroutine ')) |
| 290 | return ret |
| 291 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 292 | |
Yury Selivanov | 4715039 | 2018-09-18 17:55:44 -0400 | [diff] [blame] | 293 | class PyAwaitableMixin(object): |
| 294 | def handle_signature(self, sig, signode): |
| 295 | ret = super(PyAwaitableMixin, self).handle_signature(sig, signode) |
| 296 | signode.insert(0, addnodes.desc_annotation('awaitable ', 'awaitable ')) |
| 297 | return ret |
| 298 | |
| 299 | |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 300 | class PyCoroutineFunction(PyCoroutineMixin, PyModulelevel): |
| 301 | def run(self): |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 302 | self.name = 'py:function' |
| 303 | return PyModulelevel.run(self) |
| 304 | |
| 305 | |
| 306 | class PyCoroutineMethod(PyCoroutineMixin, PyClassmember): |
| 307 | def run(self): |
| 308 | self.name = 'py:method' |
| 309 | return PyClassmember.run(self) |
| 310 | |
| 311 | |
Yury Selivanov | 4715039 | 2018-09-18 17:55:44 -0400 | [diff] [blame] | 312 | class PyAwaitableFunction(PyAwaitableMixin, PyClassmember): |
| 313 | def run(self): |
| 314 | self.name = 'py:function' |
| 315 | return PyClassmember.run(self) |
| 316 | |
| 317 | |
| 318 | class PyAwaitableMethod(PyAwaitableMixin, PyClassmember): |
| 319 | def run(self): |
| 320 | self.name = 'py:method' |
| 321 | return PyClassmember.run(self) |
| 322 | |
| 323 | |
Berker Peksag | 6e9d2e6 | 2015-12-08 12:14:50 +0200 | [diff] [blame] | 324 | class PyAbstractMethod(PyClassmember): |
| 325 | |
| 326 | def handle_signature(self, sig, signode): |
| 327 | ret = super(PyAbstractMethod, self).handle_signature(sig, signode) |
| 328 | signode.insert(0, addnodes.desc_annotation('abstractmethod ', |
| 329 | 'abstractmethod ')) |
| 330 | return ret |
| 331 | |
| 332 | def run(self): |
| 333 | self.name = 'py:method' |
| 334 | return PyClassmember.run(self) |
| 335 | |
| 336 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 337 | # Support for documenting version of removal in deprecations |
| 338 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 339 | class DeprecatedRemoved(Directive): |
| 340 | has_content = True |
| 341 | required_arguments = 2 |
| 342 | optional_arguments = 1 |
| 343 | final_argument_whitespace = True |
| 344 | option_spec = {} |
| 345 | |
cocoatomo | 0febc05 | 2018-02-23 20:47:19 +0900 | [diff] [blame] | 346 | _label = 'Deprecated since version {deprecated}, will be removed in version {removed}' |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 347 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 348 | def run(self): |
| 349 | node = addnodes.versionmodified() |
| 350 | node.document = self.state.document |
| 351 | node['type'] = 'deprecated-removed' |
| 352 | version = (self.arguments[0], self.arguments[1]) |
| 353 | node['version'] = version |
cocoatomo | 0febc05 | 2018-02-23 20:47:19 +0900 | [diff] [blame] | 354 | label = translators['sphinx'].gettext(self._label) |
| 355 | text = label.format(deprecated=self.arguments[0], removed=self.arguments[1]) |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 356 | if len(self.arguments) == 3: |
| 357 | inodes, messages = self.state.inline_text(self.arguments[2], |
| 358 | self.lineno+1) |
cocoatomo | 0febc05 | 2018-02-23 20:47:19 +0900 | [diff] [blame] | 359 | para = nodes.paragraph(self.arguments[2], '', *inodes, translatable=False) |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 360 | node.append(para) |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 361 | else: |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 362 | messages = [] |
| 363 | if self.content: |
| 364 | self.state.nested_parse(self.content, self.content_offset, node) |
Berker Peksag | eb1a3cd | 2014-11-08 22:40:22 +0200 | [diff] [blame] | 365 | if len(node): |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 366 | if isinstance(node[0], nodes.paragraph) and node[0].rawsource: |
| 367 | content = nodes.inline(node[0].rawsource, translatable=True) |
| 368 | content.source = node[0].source |
| 369 | content.line = node[0].line |
| 370 | content += node[0].children |
cocoatomo | 0febc05 | 2018-02-23 20:47:19 +0900 | [diff] [blame] | 371 | node[0].replace_self(nodes.paragraph('', '', content, translatable=False)) |
Berker Peksag | eb1a3cd | 2014-11-08 22:40:22 +0200 | [diff] [blame] | 372 | node[0].insert(0, nodes.inline('', '%s: ' % text, |
| 373 | classes=['versionmodified'])) |
Ned Deily | b682fd3 | 2014-09-22 14:44:22 -0700 | [diff] [blame] | 374 | else: |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 375 | para = nodes.paragraph('', '', |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 376 | nodes.inline('', '%s.' % text, |
cocoatomo | 0febc05 | 2018-02-23 20:47:19 +0900 | [diff] [blame] | 377 | classes=['versionmodified']), |
| 378 | translatable=False) |
Berker Peksag | eb1a3cd | 2014-11-08 22:40:22 +0200 | [diff] [blame] | 379 | node.append(para) |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 380 | env = self.state.document.settings.env |
Pablo Galindo | 960bb88 | 2019-05-10 22:58:17 +0100 | [diff] [blame] | 381 | env.get_domain('changeset').note_changeset(node) |
Georg Brandl | 7ed509a | 2014-01-21 19:20:31 +0100 | [diff] [blame] | 382 | return [node] + messages |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 383 | |
| 384 | |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 385 | # Support for including Misc/NEWS |
| 386 | |
Brett Cannon | 79ab8be | 2017-02-10 15:10:13 -0800 | [diff] [blame] | 387 | issue_re = re.compile('(?:[Ii]ssue #|bpo-)([0-9]+)') |
Georg Brandl | 44d0c21 | 2012-10-01 19:08:50 +0200 | [diff] [blame] | 388 | whatsnew_re = re.compile(r"(?im)^what's new in (.*?)\??$") |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 389 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 390 | |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 391 | class MiscNews(Directive): |
| 392 | has_content = False |
| 393 | required_arguments = 1 |
| 394 | optional_arguments = 0 |
| 395 | final_argument_whitespace = False |
| 396 | option_spec = {} |
| 397 | |
| 398 | def run(self): |
| 399 | fname = self.arguments[0] |
| 400 | source = self.state_machine.input_lines.source( |
| 401 | self.lineno - self.state_machine.input_offset - 1) |
Steve Dower | afe17a7 | 2018-12-19 18:20:06 -0800 | [diff] [blame] | 402 | source_dir = getenv('PY_MISC_NEWS_DIR') |
| 403 | if not source_dir: |
| 404 | source_dir = path.dirname(path.abspath(source)) |
Georg Brandl | 44d0c21 | 2012-10-01 19:08:50 +0200 | [diff] [blame] | 405 | fpath = path.join(source_dir, fname) |
| 406 | self.state.document.settings.record_dependencies.add(fpath) |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 407 | try: |
Victor Stinner | 272d888 | 2017-06-16 08:59:01 +0200 | [diff] [blame] | 408 | with io.open(fpath, encoding='utf-8') as fp: |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 409 | content = fp.read() |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 410 | except Exception: |
| 411 | text = 'The NEWS file is not available.' |
| 412 | node = nodes.strong(text, text) |
| 413 | return [node] |
Brett Cannon | 79ab8be | 2017-02-10 15:10:13 -0800 | [diff] [blame] | 414 | content = issue_re.sub(r'`bpo-\1 <https://bugs.python.org/issue\1>`__', |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 415 | content) |
Georg Brandl | 44d0c21 | 2012-10-01 19:08:50 +0200 | [diff] [blame] | 416 | content = whatsnew_re.sub(r'\1', content) |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 417 | # remove first 3 lines as they are the main heading |
Georg Brandl | 6c47581 | 2012-10-01 19:27:05 +0200 | [diff] [blame] | 418 | lines = ['.. default-role:: obj', ''] + content.splitlines()[3:] |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 419 | self.state_machine.insert_input(lines, fname) |
| 420 | return [] |
| 421 | |
| 422 | |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 423 | # Support for building "topic help" for pydoc |
| 424 | |
| 425 | pydoc_topic_labels = [ |
Jelle Zijlstra | ac31770 | 2017-10-05 20:24:46 -0700 | [diff] [blame] | 426 | 'assert', 'assignment', 'async', 'atom-identifiers', 'atom-literals', |
| 427 | 'attribute-access', 'attribute-references', 'augassign', 'await', |
| 428 | 'binary', 'bitwise', 'bltin-code-objects', 'bltin-ellipsis-object', |
Benjamin Peterson | 0d31d58 | 2010-06-06 02:44:41 +0000 | [diff] [blame] | 429 | 'bltin-null-object', 'bltin-type-objects', 'booleans', |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 430 | 'break', 'callable-types', 'calls', 'class', 'comparisons', 'compound', |
| 431 | 'context-managers', 'continue', 'conversions', 'customization', 'debugger', |
| 432 | 'del', 'dict', 'dynamic-features', 'else', 'exceptions', 'execmodel', |
| 433 | 'exprlists', 'floating', 'for', 'formatstrings', 'function', 'global', |
| 434 | 'id-classes', 'identifiers', 'if', 'imaginary', 'import', 'in', 'integers', |
Benjamin Peterson | f5a3d69 | 2010-08-31 14:31:01 +0000 | [diff] [blame] | 435 | 'lambda', 'lists', 'naming', 'nonlocal', 'numbers', 'numeric-types', |
| 436 | 'objects', 'operator-summary', 'pass', 'power', 'raise', 'return', |
| 437 | 'sequence-types', 'shifting', 'slicings', 'specialattrs', 'specialnames', |
| 438 | 'string-methods', 'strings', 'subscriptions', 'truth', 'try', 'types', |
| 439 | 'typesfunctions', 'typesmapping', 'typesmethods', 'typesmodules', |
| 440 | 'typesseq', 'typesseq-mutable', 'unary', 'while', 'with', 'yield' |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 441 | ] |
| 442 | |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 443 | |
| 444 | class PydocTopicsBuilder(Builder): |
| 445 | name = 'pydoc-topics' |
| 446 | |
Benjamin Peterson | acfb087 | 2018-04-16 22:56:46 -0700 | [diff] [blame] | 447 | default_translator_class = TextTranslator |
| 448 | |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 449 | def init(self): |
| 450 | self.topics = {} |
Benjamin Peterson | acfb087 | 2018-04-16 22:56:46 -0700 | [diff] [blame] | 451 | self.secnumbers = {} |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 452 | |
| 453 | def get_outdated_docs(self): |
| 454 | return 'all pydoc topics' |
| 455 | |
| 456 | def get_target_uri(self, docname, typ=None): |
| 457 | return '' # no URIs |
| 458 | |
| 459 | def write(self, *ignored): |
| 460 | writer = TextWriter(self) |
Benjamin Peterson | acfb087 | 2018-04-16 22:56:46 -0700 | [diff] [blame] | 461 | for label in status_iterator(pydoc_topic_labels, |
| 462 | 'building topics... ', |
| 463 | length=len(pydoc_topic_labels)): |
Georg Brandl | 80ff2ad | 2010-07-31 08:27:46 +0000 | [diff] [blame] | 464 | if label not in self.env.domaindata['std']['labels']: |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 465 | self.env.logger.warn('label %r not in documentation' % label) |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 466 | continue |
Georg Brandl | 80ff2ad | 2010-07-31 08:27:46 +0000 | [diff] [blame] | 467 | docname, labelid, sectname = self.env.domaindata['std']['labels'][label] |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 468 | doctree = self.env.get_and_resolve_doctree(docname, self) |
| 469 | document = new_document('<section node>') |
| 470 | document.append(doctree.ids[labelid]) |
| 471 | destination = StringOutput(encoding='utf-8') |
| 472 | writer.write(document, destination) |
Ned Deily | b682fd3 | 2014-09-22 14:44:22 -0700 | [diff] [blame] | 473 | self.topics[label] = writer.output |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 474 | |
| 475 | def finish(self): |
Ned Deily | b682fd3 | 2014-09-22 14:44:22 -0700 | [diff] [blame] | 476 | f = open(path.join(self.outdir, 'topics.py'), 'wb') |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 477 | try: |
Ned Deily | b682fd3 | 2014-09-22 14:44:22 -0700 | [diff] [blame] | 478 | f.write('# -*- coding: utf-8 -*-\n'.encode('utf-8')) |
| 479 | f.write(('# Autogenerated by Sphinx on %s\n' % asctime()).encode('utf-8')) |
| 480 | f.write(('topics = ' + pformat(self.topics) + '\n').encode('utf-8')) |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 481 | finally: |
| 482 | f.close() |
| 483 | |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 484 | |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 485 | # Support for documenting Opcodes |
| 486 | |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 487 | opcode_sig_re = re.compile(r'(\w+(?:\+\d)?)(?:\s*\((.*)\))?') |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 488 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 489 | |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 490 | def parse_opcode_signature(env, sig, signode): |
| 491 | """Transform an opcode signature into RST nodes.""" |
| 492 | m = opcode_sig_re.match(sig) |
| 493 | if m is None: |
| 494 | raise ValueError |
| 495 | opname, arglist = m.groups() |
| 496 | signode += addnodes.desc_name(opname, opname) |
Georg Brandl | 4833e5b | 2010-07-03 10:41:33 +0000 | [diff] [blame] | 497 | if arglist is not None: |
| 498 | paramlist = addnodes.desc_parameterlist() |
| 499 | signode += paramlist |
| 500 | paramlist += addnodes.desc_parameter(arglist, arglist) |
Georg Brandl | f0dd6a6 | 2008-07-23 15:19:11 +0000 | [diff] [blame] | 501 | return opname.strip() |
| 502 | |
| 503 | |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 504 | # Support for documenting pdb commands |
| 505 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 506 | pdbcmd_sig_re = re.compile(r'([a-z()!]+)\s*(.*)') |
| 507 | |
| 508 | # later... |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 509 | # pdbargs_tokens_re = re.compile(r'''[a-zA-Z]+ | # identifiers |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 510 | # [.,:]+ | # punctuation |
| 511 | # [\[\]()] | # parens |
| 512 | # \s+ # whitespace |
| 513 | # ''', re.X) |
| 514 | |
Georg Brandl | 35903c8 | 2014-10-30 22:55:13 +0100 | [diff] [blame] | 515 | |
Georg Brandl | 02053ee | 2010-07-18 10:11:03 +0000 | [diff] [blame] | 516 | def parse_pdb_command(env, sig, signode): |
| 517 | """Transform a pdb command signature into RST nodes.""" |
| 518 | m = pdbcmd_sig_re.match(sig) |
| 519 | if m is None: |
| 520 | raise ValueError |
| 521 | name, args = m.groups() |
| 522 | fullname = name.replace('(', '').replace(')', '') |
| 523 | signode += addnodes.desc_name(name, name) |
| 524 | if args: |
| 525 | signode += addnodes.desc_addname(' '+args, ' '+args) |
| 526 | return fullname |
| 527 | |
| 528 | |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 529 | def process_audit_events(app, doctree, fromdocname): |
| 530 | for node in doctree.traverse(audit_event_list): |
| 531 | break |
| 532 | else: |
| 533 | return |
| 534 | |
| 535 | env = app.builder.env |
| 536 | |
| 537 | table = nodes.table(cols=3) |
| 538 | group = nodes.tgroup( |
| 539 | '', |
| 540 | nodes.colspec(colwidth=30), |
| 541 | nodes.colspec(colwidth=55), |
| 542 | nodes.colspec(colwidth=15), |
| 543 | ) |
| 544 | head = nodes.thead() |
| 545 | body = nodes.tbody() |
| 546 | |
| 547 | table += group |
| 548 | group += head |
| 549 | group += body |
| 550 | |
| 551 | row = nodes.row() |
| 552 | row += nodes.entry('', nodes.paragraph('', nodes.Text('Audit event'))) |
| 553 | row += nodes.entry('', nodes.paragraph('', nodes.Text('Arguments'))) |
| 554 | row += nodes.entry('', nodes.paragraph('', nodes.Text('References'))) |
| 555 | head += row |
| 556 | |
| 557 | for name in sorted(getattr(env, "all_audit_events", ())): |
| 558 | audit_event = env.all_audit_events[name] |
| 559 | |
| 560 | row = nodes.row() |
| 561 | node = nodes.paragraph('', nodes.Text(name)) |
| 562 | row += nodes.entry('', node) |
| 563 | |
| 564 | node = nodes.paragraph() |
| 565 | for i, a in enumerate(audit_event['args']): |
| 566 | if i: |
| 567 | node += nodes.Text(", ") |
| 568 | node += nodes.literal(a, nodes.Text(a)) |
| 569 | row += nodes.entry('', node) |
| 570 | |
| 571 | node = nodes.paragraph() |
Steve Dower | e226e83 | 2019-07-01 16:03:53 -0700 | [diff] [blame] | 572 | backlinks = enumerate(sorted(set(audit_event['source'])), start=1) |
| 573 | for i, (doc, label) in backlinks: |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 574 | if isinstance(label, str): |
| 575 | ref = nodes.reference("", nodes.Text("[{}]".format(i)), internal=True) |
Julien Palard | 63c98ed | 2019-09-09 12:54:56 +0200 | [diff] [blame^] | 576 | try: |
| 577 | ref['refuri'] = "{}#{}".format( |
| 578 | app.builder.get_relative_uri(fromdocname, doc), |
| 579 | label, |
| 580 | ) |
| 581 | except NoUri: |
| 582 | continue |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 583 | node += ref |
| 584 | row += nodes.entry('', node) |
| 585 | |
| 586 | body += row |
| 587 | |
| 588 | for node in doctree.traverse(audit_event_list): |
| 589 | node.replace_self(table) |
| 590 | |
| 591 | |
Martin v. Löwis | 5680d0c | 2008-04-10 03:06:53 +0000 | [diff] [blame] | 592 | def setup(app): |
| 593 | app.add_role('issue', issue_role) |
Georg Brandl | 6881886 | 2010-11-06 07:19:35 +0000 | [diff] [blame] | 594 | app.add_role('source', source_role) |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 595 | app.add_directive('impl-detail', ImplementationDetail) |
Cheryl Sabella | 2d6097d | 2018-10-12 10:55:20 -0400 | [diff] [blame] | 596 | app.add_directive('availability', Availability) |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 597 | app.add_directive('audit-event', AuditEvent) |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 598 | app.add_directive('audit-event-table', AuditEventListDirective) |
Georg Brandl | 281d6ba | 2010-11-12 08:57:12 +0000 | [diff] [blame] | 599 | app.add_directive('deprecated-removed', DeprecatedRemoved) |
Georg Brandl | 6b38daa | 2008-06-01 21:05:17 +0000 | [diff] [blame] | 600 | app.add_builder(PydocTopicsBuilder) |
Benjamin Peterson | 28d88b4 | 2009-01-09 03:03:23 +0000 | [diff] [blame] | 601 | app.add_builder(suspicious.CheckSuspiciousMarkupBuilder) |
Stéphane Wirtel | e385d06 | 2018-10-13 08:14:08 +0200 | [diff] [blame] | 602 | app.add_object_type('opcode', 'opcode', '%s (opcode)', parse_opcode_signature) |
| 603 | app.add_object_type('pdbcommand', 'pdbcmd', '%s (pdb command)', parse_pdb_command) |
| 604 | app.add_object_type('2to3fixer', '2to3fixer', '%s (2to3 fixer)') |
Georg Brandl | 8a1caa2 | 2010-07-29 16:01:11 +0000 | [diff] [blame] | 605 | app.add_directive_to_domain('py', 'decorator', PyDecoratorFunction) |
| 606 | app.add_directive_to_domain('py', 'decoratormethod', PyDecoratorMethod) |
Victor Stinner | bdd574d | 2015-02-12 22:49:18 +0100 | [diff] [blame] | 607 | app.add_directive_to_domain('py', 'coroutinefunction', PyCoroutineFunction) |
| 608 | app.add_directive_to_domain('py', 'coroutinemethod', PyCoroutineMethod) |
Yury Selivanov | 4715039 | 2018-09-18 17:55:44 -0400 | [diff] [blame] | 609 | app.add_directive_to_domain('py', 'awaitablefunction', PyAwaitableFunction) |
| 610 | app.add_directive_to_domain('py', 'awaitablemethod', PyAwaitableMethod) |
Berker Peksag | 6e9d2e6 | 2015-12-08 12:14:50 +0200 | [diff] [blame] | 611 | app.add_directive_to_domain('py', 'abstractmethod', PyAbstractMethod) |
Georg Brandl | 2cac28b | 2012-09-30 15:10:06 +0200 | [diff] [blame] | 612 | app.add_directive('miscnews', MiscNews) |
Steve Dower | 44f91c3 | 2019-06-27 10:47:59 -0700 | [diff] [blame] | 613 | app.connect('doctree-resolved', process_audit_events) |
Georg Brandl | bae334c | 2014-09-30 22:17:41 +0200 | [diff] [blame] | 614 | return {'version': '1.0', 'parallel_read_safe': True} |