[36] | 1 | import re, inspect, textwrap, pydoc |
---|
| 2 | from docscrape import NumpyDocString, FunctionDoc, ClassDoc |
---|
| 3 | |
---|
| 4 | class SphinxDocString(NumpyDocString): |
---|
| 5 | # string conversion routines |
---|
| 6 | def _str_header(self, name, symbol='`'): |
---|
| 7 | return ['.. rubric:: ' + name, ''] |
---|
| 8 | |
---|
| 9 | def _str_field_list(self, name): |
---|
| 10 | return [':' + name + ':'] |
---|
| 11 | |
---|
| 12 | def _str_indent(self, doc, indent=4): |
---|
| 13 | out = [] |
---|
| 14 | for line in doc: |
---|
| 15 | out += [' '*indent + line] |
---|
| 16 | return out |
---|
| 17 | |
---|
| 18 | def _str_signature(self): |
---|
| 19 | return [''] |
---|
| 20 | if self['Signature']: |
---|
| 21 | return ['``%s``' % self['Signature']] + [''] |
---|
| 22 | else: |
---|
| 23 | return [''] |
---|
| 24 | |
---|
| 25 | def _str_summary(self): |
---|
| 26 | return self['Summary'] + [''] |
---|
| 27 | |
---|
| 28 | def _str_extended_summary(self): |
---|
| 29 | return self['Extended Summary'] + [''] |
---|
| 30 | |
---|
| 31 | def _str_param_list(self, name): |
---|
| 32 | out = [] |
---|
| 33 | if self[name]: |
---|
| 34 | out += self._str_field_list(name) |
---|
| 35 | out += [''] |
---|
| 36 | for param,param_type,desc in self[name]: |
---|
| 37 | out += self._str_indent(['**%s** : %s' % (param.strip(), |
---|
| 38 | param_type)]) |
---|
| 39 | out += [''] |
---|
| 40 | out += self._str_indent(desc,8) |
---|
| 41 | out += [''] |
---|
| 42 | return out |
---|
| 43 | |
---|
| 44 | def _str_section(self, name): |
---|
| 45 | out = [] |
---|
| 46 | if self[name]: |
---|
| 47 | out += self._str_header(name) |
---|
| 48 | out += [''] |
---|
| 49 | content = textwrap.dedent("\n".join(self[name])).split("\n") |
---|
| 50 | out += content |
---|
| 51 | out += [''] |
---|
| 52 | return out |
---|
| 53 | |
---|
| 54 | def _str_see_also(self, func_role): |
---|
| 55 | out = [] |
---|
| 56 | if self['See Also']: |
---|
| 57 | see_also = super(SphinxDocString, self)._str_see_also(func_role) |
---|
| 58 | out = ['.. seealso::', ''] |
---|
| 59 | out += self._str_indent(see_also[2:]) |
---|
| 60 | return out |
---|
| 61 | |
---|
| 62 | def _str_warnings(self): |
---|
| 63 | out = [] |
---|
| 64 | if self['Warnings']: |
---|
| 65 | out = ['.. warning::', ''] |
---|
| 66 | out += self._str_indent(self['Warnings']) |
---|
| 67 | return out |
---|
| 68 | |
---|
| 69 | def _str_index(self): |
---|
| 70 | idx = self['index'] |
---|
| 71 | out = [] |
---|
| 72 | if len(idx) == 0: |
---|
| 73 | return out |
---|
| 74 | |
---|
| 75 | out += ['.. index:: %s' % idx.get('default','')] |
---|
| 76 | for section, references in idx.iteritems(): |
---|
| 77 | if section == 'default': |
---|
| 78 | continue |
---|
| 79 | elif section == 'refguide': |
---|
| 80 | out += [' single: %s' % (', '.join(references))] |
---|
| 81 | else: |
---|
| 82 | out += [' %s: %s' % (section, ','.join(references))] |
---|
| 83 | return out |
---|
| 84 | |
---|
| 85 | def _str_references(self): |
---|
| 86 | out = [] |
---|
| 87 | if self['References']: |
---|
| 88 | out += self._str_header('References') |
---|
| 89 | if isinstance(self['References'], str): |
---|
| 90 | self['References'] = [self['References']] |
---|
| 91 | out.extend(self['References']) |
---|
| 92 | out += [''] |
---|
| 93 | return out |
---|
| 94 | |
---|
| 95 | def __str__(self, indent=0, func_role="obj"): |
---|
| 96 | out = [] |
---|
| 97 | out += self._str_signature() |
---|
| 98 | out += self._str_index() + [''] |
---|
| 99 | out += self._str_summary() |
---|
| 100 | out += self._str_extended_summary() |
---|
| 101 | for param_list in ('Parameters', 'Attributes', 'Methods', |
---|
| 102 | 'Returns','Raises'): |
---|
| 103 | out += self._str_param_list(param_list) |
---|
| 104 | out += self._str_warnings() |
---|
| 105 | out += self._str_see_also(func_role) |
---|
| 106 | out += self._str_section('Notes') |
---|
| 107 | out += self._str_references() |
---|
| 108 | out += self._str_section('Examples') |
---|
| 109 | out = self._str_indent(out,indent) |
---|
| 110 | return '\n'.join(out) |
---|
| 111 | |
---|
| 112 | class SphinxFunctionDoc(SphinxDocString, FunctionDoc): |
---|
| 113 | pass |
---|
| 114 | |
---|
| 115 | class SphinxClassDoc(SphinxDocString, ClassDoc): |
---|
| 116 | pass |
---|
| 117 | |
---|
| 118 | def get_doc_object(obj, what=None, doc=None): |
---|
| 119 | if what is None: |
---|
| 120 | if inspect.isclass(obj): |
---|
| 121 | what = 'class' |
---|
| 122 | elif inspect.ismodule(obj): |
---|
| 123 | what = 'module' |
---|
| 124 | elif callable(obj): |
---|
| 125 | what = 'function' |
---|
| 126 | else: |
---|
| 127 | what = 'object' |
---|
| 128 | if what == 'class': |
---|
| 129 | return SphinxClassDoc(obj, '', func_doc=SphinxFunctionDoc, doc=doc) |
---|
| 130 | elif what in ('function', 'method'): |
---|
| 131 | return SphinxFunctionDoc(obj, '', doc=doc) |
---|
| 132 | else: |
---|
| 133 | if doc is None: |
---|
| 134 | doc = pydoc.getdoc(obj) |
---|
| 135 | return SphinxDocString(doc) |
---|
| 136 | |
---|