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