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