1 | """ |
---|
2 | ======== |
---|
3 | numpydoc |
---|
4 | ======== |
---|
5 | |
---|
6 | Sphinx extension that handles docstrings in the Numpy standard format. [1] |
---|
7 | |
---|
8 | It will: |
---|
9 | |
---|
10 | - Convert Parameters etc. sections to field lists. |
---|
11 | - Convert See Also section to a See also entry. |
---|
12 | - Renumber references. |
---|
13 | - Extract the signature from the docstring, if it can't be determined otherwise. |
---|
14 | |
---|
15 | .. [1] http://projects.scipy.org/scipy/numpy/wiki/CodingStyleGuidelines#docstring-standard |
---|
16 | |
---|
17 | """ |
---|
18 | |
---|
19 | import os, re, pydoc |
---|
20 | from docscrape_sphinx import get_doc_object, SphinxDocString |
---|
21 | import inspect |
---|
22 | |
---|
23 | def mangle_docstrings(app, what, name, obj, options, lines, |
---|
24 | reference_offset=[0]): |
---|
25 | if what == 'module': |
---|
26 | # Strip top title |
---|
27 | title_re = re.compile(r'^\s*[#*=]{4,}\n[a-z0-9 -]+\n[#*=]{4,}\s*', |
---|
28 | re.I|re.S) |
---|
29 | lines[:] = title_re.sub('', "\n".join(lines)).split("\n") |
---|
30 | else: |
---|
31 | doc = get_doc_object(obj, what, "\n".join(lines)) |
---|
32 | lines[:] = str(doc).split("\n") |
---|
33 | |
---|
34 | if app.config.numpydoc_edit_link and hasattr(obj, '__name__') and \ |
---|
35 | obj.__name__: |
---|
36 | if hasattr(obj, '__module__'): |
---|
37 | v = dict(full_name="%s.%s" % (obj.__module__, obj.__name__)) |
---|
38 | else: |
---|
39 | v = dict(full_name=obj.__name__) |
---|
40 | lines += ['', '.. htmlonly::', ''] |
---|
41 | lines += [' %s' % x for x in |
---|
42 | (app.config.numpydoc_edit_link % v).split("\n")] |
---|
43 | |
---|
44 | # replace reference numbers so that there are no duplicates |
---|
45 | references = [] |
---|
46 | for l in lines: |
---|
47 | l = l.strip() |
---|
48 | if l.startswith('.. ['): |
---|
49 | try: |
---|
50 | references.append(int(l[len('.. ['):l.index(']')])) |
---|
51 | except ValueError: |
---|
52 | print "WARNING: invalid reference in %s docstring" % name |
---|
53 | |
---|
54 | # Start renaming from the biggest number, otherwise we may |
---|
55 | # overwrite references. |
---|
56 | references.sort() |
---|
57 | if references: |
---|
58 | for i, line in enumerate(lines): |
---|
59 | for r in references: |
---|
60 | new_r = reference_offset[0] + r |
---|
61 | lines[i] = lines[i].replace('[%d]_' % r, |
---|
62 | '[%d]_' % new_r) |
---|
63 | lines[i] = lines[i].replace('.. [%d]' % r, |
---|
64 | '.. [%d]' % new_r) |
---|
65 | |
---|
66 | reference_offset[0] += len(references) |
---|
67 | |
---|
68 | def mangle_signature(app, what, name, obj, options, sig, retann): |
---|
69 | # Do not try to inspect classes that don't define `__init__` |
---|
70 | if (inspect.isclass(obj) and |
---|
71 | 'initializes x; see ' in pydoc.getdoc(obj.__init__)): |
---|
72 | return '', '' |
---|
73 | |
---|
74 | if not (callable(obj) or hasattr(obj, '__argspec_is_invalid_')): return |
---|
75 | if not hasattr(obj, '__doc__'): return |
---|
76 | |
---|
77 | doc = SphinxDocString(pydoc.getdoc(obj)) |
---|
78 | if doc['Signature']: |
---|
79 | sig = re.sub("^[^(]*", "", doc['Signature']) |
---|
80 | return sig, '' |
---|
81 | |
---|
82 | def initialize(app): |
---|
83 | try: |
---|
84 | app.connect('autodoc-process-signature', mangle_signature) |
---|
85 | except: |
---|
86 | monkeypatch_sphinx_ext_autodoc() |
---|
87 | |
---|
88 | def setup(app, get_doc_object_=get_doc_object): |
---|
89 | global get_doc_object |
---|
90 | get_doc_object = get_doc_object_ |
---|
91 | |
---|
92 | app.connect('autodoc-process-docstring', mangle_docstrings) |
---|
93 | app.connect('builder-inited', initialize) |
---|
94 | app.add_config_value('numpydoc_edit_link', None, True) |
---|
95 | |
---|
96 | #------------------------------------------------------------------------------ |
---|
97 | # Monkeypatch sphinx.ext.autodoc to accept argspecless autodocs (Sphinx < 0.5) |
---|
98 | #------------------------------------------------------------------------------ |
---|
99 | |
---|
100 | def monkeypatch_sphinx_ext_autodoc(): |
---|
101 | global _original_format_signature |
---|
102 | import sphinx.ext.autodoc |
---|
103 | |
---|
104 | if sphinx.ext.autodoc.format_signature is our_format_signature: |
---|
105 | return |
---|
106 | |
---|
107 | print "[numpydoc] Monkeypatching sphinx.ext.autodoc ..." |
---|
108 | _original_format_signature = sphinx.ext.autodoc.format_signature |
---|
109 | sphinx.ext.autodoc.format_signature = our_format_signature |
---|
110 | |
---|
111 | def our_format_signature(what, obj): |
---|
112 | r = mangle_signature(None, what, None, obj, None, None, None) |
---|
113 | if r is not None: |
---|
114 | return r[0] |
---|
115 | else: |
---|
116 | return _original_format_signature(what, obj) |
---|