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