PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/python/helpers/sphinxcontrib/napoleon/docstring.py

http://github.com/JetBrains/intellij-community
Python | 933 lines | 901 code | 6 blank | 26 comment | 0 complexity | fb6c7cc3c209771142572c8c0e9f30dc MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2014 Rob Ruana
  3. # Licensed under the BSD License, see LICENSE file for details.
  4. """Classes for docstring parsing and formatting."""
  5. import collections
  6. import inspect
  7. import re
  8. import sys
  9. from six.moves import range
  10. from pockets import modify_iter
  11. from six import string_types
  12. _directive_regex = re.compile(r'\.\. \S+::')
  13. _google_section_regex = re.compile(r'^(\s|\w)+:\s*$')
  14. _google_typed_arg_regex = re.compile(r'\s*(.+?)\s*\(\s*(.+?)\s*\)')
  15. _numpy_section_regex = re.compile(r'^[=\-`:\'"~^_*+#<>]{2,}\s*$')
  16. _xref_regex = re.compile(r'(:\w+:\S+:`.+?`|:\S+:`.+?`|`.+?`)')
  17. class GoogleDocstring(object):
  18. """Convert Google style docstrings to reStructuredText.
  19. Parameters
  20. ----------
  21. docstring : str or List[str]
  22. The docstring to parse, given either as a string or split into
  23. individual lines.
  24. config : Optional[sphinxcontrib.napoleon.Config or sphinx.config.Config]
  25. The configuration settings to use. If not given, defaults to the
  26. config object on `app`; or if `app` is not given defaults to the
  27. a new `sphinxcontrib.napoleon.Config` object.
  28. See Also
  29. --------
  30. :class:`sphinxcontrib.napoleon.Config`
  31. Other Parameters
  32. ----------------
  33. app : Optional[sphinx.application.Sphinx]
  34. Application object representing the Sphinx process.
  35. what : Optional[str]
  36. A string specifying the type of the object to which the docstring
  37. belongs. Valid values: "module", "class", "exception", "function",
  38. "method", "attribute".
  39. name : Optional[str]
  40. The fully qualified name of the object.
  41. obj : module, class, exception, function, method, or attribute
  42. The object to which the docstring belongs.
  43. options : Optional[sphinx.ext.autodoc.Options]
  44. The options given to the directive: an object with attributes
  45. inherited_members, undoc_members, show_inheritance and noindex that
  46. are True if the flag option of same name was given to the auto
  47. directive.
  48. Example
  49. -------
  50. >>> from sphinxcontrib.napoleon import Config
  51. >>> config = Config(napoleon_use_param=True, napoleon_use_rtype=True)
  52. >>> docstring = '''One line summary.
  53. ...
  54. ... Extended description.
  55. ...
  56. ... Args:
  57. ... arg1(int): Description of `arg1`
  58. ... arg2(str): Description of `arg2`
  59. ... Returns:
  60. ... str: Description of return value.
  61. ... '''
  62. >>> print(GoogleDocstring(docstring, config))
  63. One line summary.
  64. <BLANKLINE>
  65. Extended description.
  66. <BLANKLINE>
  67. :param arg1: Description of `arg1`
  68. :type arg1: int
  69. :param arg2: Description of `arg2`
  70. :type arg2: str
  71. <BLANKLINE>
  72. :returns: Description of return value.
  73. :rtype: str
  74. <BLANKLINE>
  75. """
  76. def __init__(self, docstring, config=None, app=None, what='', name='',
  77. obj=None, options=None):
  78. self._config = config
  79. self._app = app
  80. if not self._config:
  81. from sphinxcontrib.napoleon import Config
  82. self._config = self._app and self._app.config or Config()
  83. if not what:
  84. if inspect.isclass(obj):
  85. what = 'class'
  86. elif inspect.ismodule(obj):
  87. what = 'module'
  88. elif isinstance(obj, collections.Callable):
  89. what = 'function'
  90. else:
  91. what = 'object'
  92. self._what = what
  93. self._name = name
  94. self._obj = obj
  95. self._opt = options
  96. if isinstance(docstring, string_types):
  97. docstring = docstring.splitlines()
  98. self._lines = docstring
  99. self._line_iter = modify_iter(docstring, modifier=lambda s: s.rstrip())
  100. self._parsed_lines = []
  101. self._is_in_section = False
  102. self._section_indent = 0
  103. if not hasattr(self, '_directive_sections'):
  104. self._directive_sections = []
  105. if not hasattr(self, '_sections'):
  106. self._sections = {
  107. 'args': self._parse_parameters_section,
  108. 'arguments': self._parse_parameters_section,
  109. 'attributes': self._parse_attributes_section,
  110. 'example': self._parse_examples_section,
  111. 'examples': self._parse_examples_section,
  112. 'keyword args': self._parse_keyword_arguments_section,
  113. 'keyword arguments': self._parse_keyword_arguments_section,
  114. 'methods': self._parse_methods_section,
  115. 'note': self._parse_note_section,
  116. 'notes': self._parse_notes_section,
  117. 'other parameters': self._parse_other_parameters_section,
  118. 'parameters': self._parse_parameters_section,
  119. 'return': self._parse_returns_section,
  120. 'returns': self._parse_returns_section,
  121. 'raises': self._parse_raises_section,
  122. 'references': self._parse_references_section,
  123. 'see also': self._parse_see_also_section,
  124. 'warning': self._parse_warning_section,
  125. 'warnings': self._parse_warning_section,
  126. 'warns': self._parse_warns_section,
  127. 'yield': self._parse_yields_section,
  128. 'yields': self._parse_yields_section,
  129. }
  130. self._parse()
  131. def __str__(self):
  132. """Return the parsed docstring in reStructuredText format.
  133. Returns
  134. -------
  135. str
  136. UTF-8 encoded version of the docstring.
  137. """
  138. if sys.version_info[0] >= 3:
  139. return self.__unicode__()
  140. else:
  141. return self.__unicode__().encode('utf8')
  142. def __unicode__(self):
  143. """Return the parsed docstring in reStructuredText format.
  144. Returns
  145. -------
  146. unicode
  147. Unicode version of the docstring.
  148. """
  149. return u'\n'.join(self.lines())
  150. def lines(self):
  151. """Return the parsed lines of the docstring in reStructuredText format.
  152. Returns
  153. -------
  154. List[str]
  155. The lines of the docstring in a list.
  156. """
  157. return self._parsed_lines
  158. def _consume_indented_block(self, indent=1):
  159. lines = []
  160. line = self._line_iter.peek()
  161. while(not self._is_section_break() and
  162. (not line or self._is_indented(line, indent))):
  163. lines.append(next(self._line_iter))
  164. line = self._line_iter.peek()
  165. return lines
  166. def _consume_contiguous(self):
  167. lines = []
  168. while (self._line_iter.has_next() and
  169. self._line_iter.peek() and
  170. not self._is_section_header()):
  171. lines.append(next(self._line_iter))
  172. return lines
  173. def _consume_empty(self):
  174. lines = []
  175. line = self._line_iter.peek()
  176. while self._line_iter.has_next() and not line:
  177. lines.append(next(self._line_iter))
  178. line = self._line_iter.peek()
  179. return lines
  180. def _consume_field(self, parse_type=True, prefer_type=False):
  181. line = next(self._line_iter)
  182. before, colon, after = self._partition_field_on_colon(line)
  183. _name, _type, _desc = before, '', after
  184. if parse_type:
  185. match = _google_typed_arg_regex.match(before)
  186. if match:
  187. _name = match.group(1)
  188. _type = match.group(2)
  189. if _name[:2] == '**':
  190. _name = r'\*\*'+_name[2:]
  191. elif _name[:1] == '*':
  192. _name = r'\*'+_name[1:]
  193. if prefer_type and not _type:
  194. _type, _name = _name, _type
  195. indent = self._get_indent(line) + 1
  196. _desc = [_desc] + self._dedent(self._consume_indented_block(indent))
  197. _desc = self.__class__(_desc, self._config).lines()
  198. return _name, _type, _desc
  199. def _consume_fields(self, parse_type=True, prefer_type=False):
  200. self._consume_empty()
  201. fields = []
  202. while not self._is_section_break():
  203. _name, _type, _desc = self._consume_field(parse_type, prefer_type)
  204. if _name or _type or _desc:
  205. fields.append((_name, _type, _desc,))
  206. return fields
  207. def _consume_inline_attribute(self):
  208. line = next(self._line_iter)
  209. _type, colon, _desc = self._partition_field_on_colon(line)
  210. if not colon:
  211. _type, _desc = _desc, _type
  212. _desc = [_desc] + self._dedent(self._consume_to_end())
  213. _desc = self.__class__(_desc, self._config).lines()
  214. return _type, _desc
  215. def _consume_returns_section(self):
  216. lines = self._dedent(self._consume_to_next_section())
  217. if lines:
  218. before, colon, after = self._partition_field_on_colon(lines[0])
  219. _name, _type, _desc = '', '', lines
  220. if colon:
  221. if after:
  222. _desc = [after] + lines[1:]
  223. else:
  224. _desc = lines[1:]
  225. match = _google_typed_arg_regex.match(before)
  226. if match:
  227. _name = match.group(1)
  228. _type = match.group(2)
  229. else:
  230. _type = before
  231. _desc = self.__class__(_desc, self._config).lines()
  232. return [(_name, _type, _desc,)]
  233. else:
  234. return []
  235. def _consume_usage_section(self):
  236. lines = self._dedent(self._consume_to_next_section())
  237. return lines
  238. def _consume_section_header(self):
  239. section = next(self._line_iter)
  240. stripped_section = section.strip(':')
  241. if stripped_section.lower() in self._sections:
  242. section = stripped_section
  243. return section
  244. def _consume_to_end(self):
  245. lines = []
  246. while self._line_iter.has_next():
  247. lines.append(next(self._line_iter))
  248. return lines
  249. def _consume_to_next_section(self):
  250. self._consume_empty()
  251. lines = []
  252. while not self._is_section_break():
  253. lines.append(next(self._line_iter))
  254. return lines + self._consume_empty()
  255. def _dedent(self, lines, full=False):
  256. if full:
  257. return [line.lstrip() for line in lines]
  258. else:
  259. min_indent = self._get_min_indent(lines)
  260. return [line[min_indent:] for line in lines]
  261. def _format_admonition(self, admonition, lines):
  262. lines = self._strip_empty(lines)
  263. if len(lines) == 1:
  264. return ['.. %s:: %s' % (admonition, lines[0].strip()), '']
  265. elif lines:
  266. lines = self._indent(self._dedent(lines), 3)
  267. return ['.. %s::' % admonition, ''] + lines + ['']
  268. else:
  269. return ['.. %s::' % admonition, '']
  270. def _format_block(self, prefix, lines, padding=None):
  271. if lines:
  272. if padding is None:
  273. padding = ' ' * len(prefix)
  274. result_lines = []
  275. for i, line in enumerate(lines):
  276. if i == 0:
  277. result_lines.append((prefix + line).rstrip())
  278. elif line:
  279. result_lines.append(padding + line)
  280. else:
  281. result_lines.append('')
  282. return result_lines
  283. else:
  284. return [prefix]
  285. def _format_field(self, _name, _type, _desc):
  286. _desc = self._strip_empty(_desc)
  287. has_desc = any(_desc)
  288. separator = has_desc and ' -- ' or ''
  289. if _name:
  290. if _type:
  291. if '`' in _type:
  292. field = '**%s** (%s)%s' % (_name, _type, separator)
  293. else:
  294. field = '**%s** (*%s*)%s' % (_name, _type, separator)
  295. else:
  296. field = '**%s**%s' % (_name, separator)
  297. elif _type:
  298. if '`' in _type:
  299. field = '%s%s' % (_type, separator)
  300. else:
  301. field = '*%s*%s' % (_type, separator)
  302. else:
  303. field = ''
  304. if has_desc:
  305. return [field + _desc[0]] + _desc[1:]
  306. else:
  307. return [field]
  308. def _format_fields(self, field_type, fields):
  309. field_type = ':%s:' % field_type.strip()
  310. padding = ' ' * len(field_type)
  311. multi = len(fields) > 1
  312. lines = []
  313. for _name, _type, _desc in fields:
  314. field = self._format_field(_name, _type, _desc)
  315. if multi:
  316. if lines:
  317. lines.extend(self._format_block(padding + ' * ', field))
  318. else:
  319. lines.extend(self._format_block(field_type + ' * ', field))
  320. else:
  321. lines.extend(self._format_block(field_type + ' ', field))
  322. if lines and lines[-1]:
  323. lines.append('')
  324. return lines
  325. def _get_current_indent(self, peek_ahead=0):
  326. line = self._line_iter.peek(peek_ahead + 1)[peek_ahead]
  327. while line != self._line_iter.sentinel:
  328. if line:
  329. return self._get_indent(line)
  330. peek_ahead += 1
  331. line = self._line_iter.peek(peek_ahead + 1)[peek_ahead]
  332. return 0
  333. def _get_indent(self, line):
  334. for i, s in enumerate(line):
  335. if not s.isspace():
  336. return i
  337. return len(line)
  338. def _get_min_indent(self, lines):
  339. min_indent = None
  340. for line in lines:
  341. if line:
  342. indent = self._get_indent(line)
  343. if min_indent is None:
  344. min_indent = indent
  345. elif indent < min_indent:
  346. min_indent = indent
  347. return min_indent or 0
  348. def _indent(self, lines, n=4):
  349. return [(' ' * n) + line for line in lines]
  350. def _is_indented(self, line, indent=1):
  351. for i, s in enumerate(line):
  352. if i >= indent:
  353. return True
  354. elif not s.isspace():
  355. return False
  356. return False
  357. def _is_section_header(self):
  358. section = self._line_iter.peek().lower()
  359. match = _google_section_regex.match(section)
  360. if match and section.strip(':') in self._sections:
  361. header_indent = self._get_indent(section)
  362. section_indent = self._get_current_indent(peek_ahead=1)
  363. return section_indent > header_indent
  364. elif self._directive_sections:
  365. if _directive_regex.match(section):
  366. for directive_section in self._directive_sections:
  367. if section.startswith(directive_section):
  368. return True
  369. return False
  370. def _is_section_break(self):
  371. line = self._line_iter.peek()
  372. return (not self._line_iter.has_next() or
  373. self._is_section_header() or
  374. (self._is_in_section and
  375. line and
  376. not self._is_indented(line, self._section_indent)))
  377. def _parse(self):
  378. self._parsed_lines = self._consume_empty()
  379. if self._name and (self._what == 'attribute' or self._what == 'data'):
  380. self._parsed_lines.extend(self._parse_attribute_docstring())
  381. return
  382. while self._line_iter.has_next():
  383. if self._is_section_header():
  384. try:
  385. section = self._consume_section_header()
  386. self._is_in_section = True
  387. self._section_indent = self._get_current_indent()
  388. if _directive_regex.match(section):
  389. lines = [section] + self._consume_to_next_section()
  390. else:
  391. lines = self._sections[section.lower()](section)
  392. finally:
  393. self._is_in_section = False
  394. self._section_indent = 0
  395. else:
  396. if not self._parsed_lines:
  397. lines = self._consume_contiguous() + self._consume_empty()
  398. else:
  399. lines = self._consume_to_next_section()
  400. self._parsed_lines.extend(lines)
  401. def _parse_attribute_docstring(self):
  402. _type, _desc = self._consume_inline_attribute()
  403. return self._format_field('', _type, _desc)
  404. def _parse_attributes_section(self, section):
  405. lines = []
  406. for _name, _type, _desc in self._consume_fields():
  407. if self._config.napoleon_use_ivar:
  408. field = ':ivar %s: ' % _name
  409. lines.extend(self._format_block(field, _desc))
  410. if _type:
  411. lines.append(':vartype %s: %s' % (_name, _type))
  412. else:
  413. lines.extend(['.. attribute:: ' + _name, ''])
  414. field = self._format_field('', _type, _desc)
  415. lines.extend(self._indent(field, 3))
  416. lines.append('')
  417. if self._config.napoleon_use_ivar:
  418. lines.append('')
  419. return lines
  420. def _parse_examples_section(self, section):
  421. use_admonition = self._config.napoleon_use_admonition_for_examples
  422. return self._parse_generic_section(section, use_admonition)
  423. def _parse_usage_section(self, section):
  424. header = ['.. rubric:: Usage:', '']
  425. block = ['.. code-block:: python', '']
  426. lines = self._consume_usage_section()
  427. lines = self._indent(lines, 3)
  428. return header + block + lines + ['']
  429. def _parse_generic_section(self, section, use_admonition):
  430. lines = self._strip_empty(self._consume_to_next_section())
  431. lines = self._dedent(lines)
  432. if use_admonition:
  433. header = '.. admonition:: %s' % section
  434. lines = self._indent(lines, 3)
  435. else:
  436. header = '.. rubric:: %s' % section
  437. if lines:
  438. return [header, ''] + lines + ['']
  439. else:
  440. return [header, '']
  441. def _parse_keyword_arguments_section(self, section):
  442. return self._format_fields('Keyword Arguments', self._consume_fields())
  443. def _parse_methods_section(self, section):
  444. lines = []
  445. for _name, _, _desc in self._consume_fields(parse_type=False):
  446. lines.append('.. method:: %s' % _name)
  447. if _desc:
  448. lines.extend([''] + self._indent(_desc, 3))
  449. lines.append('')
  450. return lines
  451. def _parse_note_section(self, section):
  452. lines = self._consume_to_next_section()
  453. return self._format_admonition('note', lines)
  454. def _parse_notes_section(self, section):
  455. use_admonition = self._config.napoleon_use_admonition_for_notes
  456. return self._parse_generic_section('Notes', use_admonition)
  457. def _parse_other_parameters_section(self, section):
  458. return self._format_fields('Other Parameters', self._consume_fields())
  459. def _parse_parameters_section(self, section):
  460. fields = self._consume_fields()
  461. if self._config.napoleon_use_param:
  462. lines = []
  463. for _name, _type, _desc in fields:
  464. field = ':param %s: ' % _name
  465. lines.extend(self._format_block(field, _desc))
  466. if _type:
  467. lines.append(':type %s: %s' % (_name, _type))
  468. return lines + ['']
  469. else:
  470. return self._format_fields('Parameters', fields)
  471. def _parse_raises_section(self, section):
  472. fields = self._consume_fields(parse_type=False, prefer_type=True)
  473. field_type = ':raises:'
  474. padding = ' ' * len(field_type)
  475. multi = len(fields) > 1
  476. lines = []
  477. for _, _type, _desc in fields:
  478. _desc = self._strip_empty(_desc)
  479. has_desc = any(_desc)
  480. separator = has_desc and ' -- ' or ''
  481. if _type:
  482. has_refs = '`' in _type or ':' in _type
  483. has_space = any(c in ' \t\n\v\f ' for c in _type)
  484. if not has_refs and not has_space:
  485. _type = ':exc:`%s`%s' % (_type, separator)
  486. elif has_desc and has_space:
  487. _type = '*%s*%s' % (_type, separator)
  488. else:
  489. _type = '%s%s' % (_type, separator)
  490. if has_desc:
  491. field = [_type + _desc[0]] + _desc[1:]
  492. else:
  493. field = [_type]
  494. else:
  495. field = _desc
  496. if multi:
  497. if lines:
  498. lines.extend(self._format_block(padding + ' * ', field))
  499. else:
  500. lines.extend(self._format_block(field_type + ' * ', field))
  501. else:
  502. lines.extend(self._format_block(field_type + ' ', field))
  503. if lines and lines[-1]:
  504. lines.append('')
  505. return lines
  506. def _parse_references_section(self, section):
  507. use_admonition = self._config.napoleon_use_admonition_for_references
  508. return self._parse_generic_section('References', use_admonition)
  509. def _parse_returns_section(self, section):
  510. fields = self._consume_returns_section()
  511. multi = len(fields) > 1
  512. if multi:
  513. use_rtype = False
  514. else:
  515. use_rtype = self._config.napoleon_use_rtype
  516. lines = []
  517. for _name, _type, _desc in fields:
  518. if use_rtype:
  519. field = self._format_field(_name, '', _desc)
  520. else:
  521. field = self._format_field(_name, _type, _desc)
  522. if multi:
  523. if lines:
  524. lines.extend(self._format_block(' * ', field))
  525. else:
  526. lines.extend(self._format_block(':returns: * ', field))
  527. else:
  528. lines.extend(self._format_block(':returns: ', field))
  529. if _type and use_rtype:
  530. lines.extend([':rtype: %s' % _type, ''])
  531. if lines and lines[-1]:
  532. lines.append('')
  533. return lines
  534. def _parse_see_also_section(self, section):
  535. lines = self._consume_to_next_section()
  536. return self._format_admonition('seealso', lines)
  537. def _parse_warning_section(self, section):
  538. lines = self._consume_to_next_section()
  539. return self._format_admonition('warning', lines)
  540. def _parse_warns_section(self, section):
  541. return self._format_fields('Warns', self._consume_fields())
  542. def _parse_yields_section(self, section):
  543. fields = self._consume_returns_section()
  544. return self._format_fields('Yields', fields)
  545. def _partition_field_on_colon(self, line):
  546. before_colon = []
  547. after_colon = []
  548. colon = ''
  549. found_colon = False
  550. for i, source in enumerate(_xref_regex.split(line)):
  551. if found_colon:
  552. after_colon.append(source)
  553. else:
  554. if (i % 2) == 0 and ":" in source:
  555. found_colon = True
  556. before, colon, after = source.partition(":")
  557. before_colon.append(before)
  558. after_colon.append(after)
  559. else:
  560. before_colon.append(source)
  561. return ("".join(before_colon).strip(),
  562. colon,
  563. "".join(after_colon).strip())
  564. def _strip_empty(self, lines):
  565. if lines:
  566. start = -1
  567. for i, line in enumerate(lines):
  568. if line:
  569. start = i
  570. break
  571. if start == -1:
  572. lines = []
  573. end = -1
  574. for i in reversed(range(len(lines))):
  575. line = lines[i]
  576. if line:
  577. end = i
  578. break
  579. if start > 0 or end + 1 < len(lines):
  580. lines = lines[start:end + 1]
  581. return lines
  582. class NumpyDocstring(GoogleDocstring):
  583. """Convert NumPy style docstrings to reStructuredText.
  584. Parameters
  585. ----------
  586. docstring : str or List[str]
  587. The docstring to parse, given either as a string or split into
  588. individual lines.
  589. config : Optional[sphinxcontrib.napoleon.Config or sphinx.config.Config]
  590. The configuration settings to use. If not given, defaults to the
  591. config object on `app`; or if `app` is not given defaults to the
  592. a new `sphinxcontrib.napoleon.Config` object.
  593. See Also
  594. --------
  595. :class:`sphinxcontrib.napoleon.Config`
  596. Other Parameters
  597. ----------------
  598. app : Optional[sphinx.application.Sphinx]
  599. Application object representing the Sphinx process.
  600. what : Optional[str]
  601. A string specifying the type of the object to which the docstring
  602. belongs. Valid values: "module", "class", "exception", "function",
  603. "method", "attribute".
  604. name : Optional[str]
  605. The fully qualified name of the object.
  606. obj : module, class, exception, function, method, or attribute
  607. The object to which the docstring belongs.
  608. options : Optional[sphinx.ext.autodoc.Options]
  609. The options given to the directive: an object with attributes
  610. inherited_members, undoc_members, show_inheritance and noindex that
  611. are True if the flag option of same name was given to the auto
  612. directive.
  613. Example
  614. -------
  615. >>> from sphinxcontrib.napoleon import Config
  616. >>> config = Config(napoleon_use_param=True, napoleon_use_rtype=True)
  617. >>> docstring = '''One line summary.
  618. ...
  619. ... Extended description.
  620. ...
  621. ... Parameters
  622. ... ----------
  623. ... arg1 : int
  624. ... Description of `arg1`
  625. ... arg2 : str
  626. ... Description of `arg2`
  627. ... Returns
  628. ... -------
  629. ... str
  630. ... Description of return value.
  631. ... '''
  632. >>> print(NumpyDocstring(docstring, config))
  633. One line summary.
  634. <BLANKLINE>
  635. Extended description.
  636. <BLANKLINE>
  637. :param arg1: Description of `arg1`
  638. :type arg1: int
  639. :param arg2: Description of `arg2`
  640. :type arg2: str
  641. <BLANKLINE>
  642. :returns: Description of return value.
  643. :rtype: str
  644. <BLANKLINE>
  645. Methods
  646. -------
  647. __str__()
  648. Return the parsed docstring in reStructuredText format.
  649. Returns
  650. -------
  651. str
  652. UTF-8 encoded version of the docstring.
  653. __unicode__()
  654. Return the parsed docstring in reStructuredText format.
  655. Returns
  656. -------
  657. unicode
  658. Unicode version of the docstring.
  659. lines()
  660. Return the parsed lines of the docstring in reStructuredText format.
  661. Returns
  662. -------
  663. List[str]
  664. The lines of the docstring in a list.
  665. """
  666. def __init__(self, docstring, config=None, app=None, what='', name='',
  667. obj=None, options=None):
  668. self._directive_sections = ['.. index::']
  669. super(NumpyDocstring, self).__init__(docstring, config, app, what,
  670. name, obj, options)
  671. def _consume_field(self, parse_type=True, prefer_type=False):
  672. line = next(self._line_iter)
  673. if parse_type:
  674. _name, _, _type = self._partition_field_on_colon(line)
  675. else:
  676. _name, _type = line, ''
  677. _name, _type = _name.strip(), _type.strip()
  678. if prefer_type and not _type:
  679. _type, _name = _name, _type
  680. if _name[:2] == '**':
  681. _name = r'\*\*'+_name[2:]
  682. elif _name[:1] == '*':
  683. _name = r'\*'+_name[1:]
  684. indent = self._get_indent(line)
  685. _desc = self._dedent(self._consume_indented_block(indent + 1))
  686. _desc = self.__class__(_desc, self._config).lines()
  687. return _name, _type, _desc
  688. def _consume_returns_section(self):
  689. return self._consume_fields(prefer_type=True)
  690. def _consume_section_header(self):
  691. section = next(self._line_iter)
  692. if not _directive_regex.match(section):
  693. # Consume the header underline
  694. next(self._line_iter)
  695. return section
  696. def _is_section_break(self):
  697. line1, line2 = self._line_iter.peek(2)
  698. return (not self._line_iter.has_next() or
  699. self._is_section_header() or
  700. ['', ''] == [line1, line2] or
  701. (self._is_in_section and
  702. line1 and
  703. not self._is_indented(line1, self._section_indent)))
  704. def _is_section_header(self):
  705. section, underline = self._line_iter.peek(2)
  706. section = section.lower()
  707. if section in self._sections and isinstance(underline, string_types):
  708. return bool(_numpy_section_regex.match(underline))
  709. elif self._directive_sections:
  710. if _directive_regex.match(section):
  711. for directive_section in self._directive_sections:
  712. if section.startswith(directive_section):
  713. return True
  714. return False
  715. _name_rgx = re.compile(r"^\s*(:(?P<role>\w+):`(?P<name>[a-zA-Z0-9_.-]+)`|"
  716. r" (?P<name2>[a-zA-Z0-9_.-]+))\s*", re.X)
  717. def _parse_see_also_section(self, section):
  718. lines = self._consume_to_next_section()
  719. try:
  720. return self._parse_numpydoc_see_also_section(lines)
  721. except ValueError:
  722. return self._format_admonition('seealso', lines)
  723. def _parse_numpydoc_see_also_section(self, content):
  724. """
  725. Derived from the NumpyDoc implementation of _parse_see_also.
  726. See Also
  727. --------
  728. func_name : Descriptive text
  729. continued text
  730. another_func_name : Descriptive text
  731. func_name1, func_name2, :meth:`func_name`, func_name3
  732. """
  733. items = []
  734. def parse_item_name(text):
  735. """Match ':role:`name`' or 'name'"""
  736. m = self._name_rgx.match(text)
  737. if m:
  738. g = m.groups()
  739. if g[1] is None:
  740. return g[3], None
  741. else:
  742. return g[2], g[1]
  743. raise ValueError("%s is not a item name" % text)
  744. def push_item(name, rest):
  745. if not name:
  746. return
  747. name, role = parse_item_name(name)
  748. items.append((name, list(rest), role))
  749. del rest[:]
  750. current_func = None
  751. rest = []
  752. for line in content:
  753. if not line.strip():
  754. continue
  755. m = self._name_rgx.match(line)
  756. if m and line[m.end():].strip().startswith(':'):
  757. push_item(current_func, rest)
  758. current_func, line = line[:m.end()], line[m.end():]
  759. rest = [line.split(':', 1)[1].strip()]
  760. if not rest[0]:
  761. rest = []
  762. elif not line.startswith(' '):
  763. push_item(current_func, rest)
  764. current_func = None
  765. if ',' in line:
  766. for func in line.split(','):
  767. if func.strip():
  768. push_item(func, [])
  769. elif line.strip():
  770. current_func = line
  771. elif current_func is not None:
  772. rest.append(line.strip())
  773. push_item(current_func, rest)
  774. if not items:
  775. return []
  776. roles = {
  777. 'method': 'meth',
  778. 'meth': 'meth',
  779. 'function': 'func',
  780. 'func': 'func',
  781. 'class': 'class',
  782. 'exception': 'exc',
  783. 'exc': 'exc',
  784. 'object': 'obj',
  785. 'obj': 'obj',
  786. 'module': 'mod',
  787. 'mod': 'mod',
  788. 'data': 'data',
  789. 'constant': 'const',
  790. 'const': 'const',
  791. 'attribute': 'attr',
  792. 'attr': 'attr'
  793. }
  794. if self._what is None:
  795. func_role = 'obj'
  796. else:
  797. func_role = roles.get(self._what, '')
  798. lines = []
  799. last_had_desc = True
  800. for func, desc, role in items:
  801. if role:
  802. link = ':%s:`%s`' % (role, func)
  803. elif func_role:
  804. link = ':%s:`%s`' % (func_role, func)
  805. else:
  806. link = "`%s`_" % func
  807. if desc or last_had_desc:
  808. lines += ['']
  809. lines += [link]
  810. else:
  811. lines[-1] += ", %s" % link
  812. if desc:
  813. lines += self._indent([' '.join(desc)])
  814. last_had_desc = True
  815. else:
  816. last_had_desc = False
  817. lines += ['']
  818. return self._format_admonition('seealso', lines)