summaryrefslogtreecommitdiff
path: root/numpydoc/docscrape_sphinx.py
blob: b15afd26f5dc96b017298d97c4b35e794ab0be62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
import re
import inspect
import textwrap
import pydoc
from collections.abc import Callable
import os

from jinja2 import FileSystemLoader
from jinja2.sandbox import SandboxedEnvironment
import sphinx
from sphinx.jinja2glue import BuiltinTemplateLoader

from .docscrape import NumpyDocString, FunctionDoc, ClassDoc, ObjDoc
from .xref import make_xref


IMPORT_MATPLOTLIB_RE = r'\b(import +matplotlib|from +matplotlib +import)\b'


class SphinxDocString(NumpyDocString):
    def __init__(self, docstring, config=None):
        if config is None:
            config = {}
        NumpyDocString.__init__(self, docstring, config=config)
        self.load_config(config)

    def load_config(self, config):
        self.use_plots = config.get('use_plots', False)
        self.use_blockquotes = config.get('use_blockquotes', False)
        self.class_members_toctree = config.get('class_members_toctree', True)
        self.attributes_as_param_list = config.get('attributes_as_param_list', True)
        self.xref_param_type = config.get('xref_param_type', False)
        self.xref_aliases = config.get('xref_aliases', dict())
        self.xref_ignore = config.get('xref_ignore', set())
        self.template = config.get('template', None)
        if self.template is None:
            template_dirs = [os.path.join(os.path.dirname(__file__), 'templates')]
            template_loader = FileSystemLoader(template_dirs)
            template_env = SandboxedEnvironment(loader=template_loader)
            self.template = template_env.get_template('numpydoc_docstring.rst')

    # string conversion routines
    def _str_header(self, name, symbol='`'):
        return ['.. rubric:: ' + name, '']

    def _str_field_list(self, name):
        return [':' + name + ':']

    def _str_indent(self, doc, indent=4):
        out = []
        for line in doc:
            out += [' '*indent + line]
        return out

    def _str_signature(self):
        return ['']

    def _str_summary(self):
        return self['Summary'] + ['']

    def _str_extended_summary(self):
        return self['Extended Summary'] + ['']

    def _str_returns(self, name='Returns'):
        named_fmt = '**%s** : %s'
        unnamed_fmt = '%s'

        out = []
        if self[name]:
            out += self._str_field_list(name)
            out += ['']
            for param in self[name]:
                param_type = param.type
                if param_type and self.xref_param_type:
                    param_type = make_xref(
                        param_type,
                        self.xref_aliases,
                        self.xref_ignore
                    )
                if param.name:
                    out += self._str_indent([named_fmt % (param.name.strip(),
                                                          param_type)])
                else:
                    out += self._str_indent([unnamed_fmt % param_type.strip()])
                if not param.desc:
                    out += self._str_indent(['..'], 8)
                else:
                    if self.use_blockquotes:
                        out += ['']
                    out += self._str_indent(param.desc, 8)
                out += ['']
        return out

    def _escape_args_and_kwargs(self, name):
        if name[:2] == '**':
            return r'\*\*' + name[2:]
        elif name[:1] == '*':
            return r'\*' + name[1:]
        else:
            return name

    def _process_param(self, param, desc, fake_autosummary):
        """Determine how to display a parameter

        Emulates autosummary behavior if fake_autosummary

        Parameters
        ----------
        param : str
            The name of the parameter
        desc : list of str
            The parameter description as given in the docstring. This is
            ignored when autosummary logic applies.
        fake_autosummary : bool
            If True, autosummary-style behaviour will apply for params
            that are attributes of the class and have a docstring.

        Returns
        -------
        display_param : str
            The marked up parameter name for display. This may include a link
            to the corresponding attribute's own documentation.
        desc : list of str
            A list of description lines. This may be identical to the input
            ``desc``, if ``autosum is None`` or ``param`` is not a class
            attribute, or it will be a summary of the class attribute's
            docstring.

        Notes
        -----
        This does not have the autosummary functionality to display a method's
        signature, and hence is not used to format methods.  It may be
        complicated to incorporate autosummary's signature mangling, as it
        relies on Sphinx's plugin mechanism.
        """
        param = self._escape_args_and_kwargs(param.strip())
        # param = param.strip()
        # XXX: If changing the following, please check the rendering when param
        # ends with '_', e.g. 'word_'
        # See https://github.com/numpy/numpydoc/pull/144
        display_param = f'**{param}**'

        if not fake_autosummary:
            return display_param, desc

        param_obj = getattr(self._obj, param, None)
        if not (callable(param_obj)
                or isinstance(param_obj, property)
                or inspect.isgetsetdescriptor(param_obj)
                or inspect.ismemberdescriptor(param_obj)):
            param_obj = None
        obj_doc = pydoc.getdoc(param_obj)

        if not (param_obj and obj_doc):
            return display_param, desc

        prefix = getattr(self, '_name', '')
        if prefix:
            link_prefix = f'{prefix}.'
        else:
            link_prefix = ''

        # Referenced object has a docstring
        display_param = f':obj:`{param} <{link_prefix}{param}>`'
        if obj_doc:
            # Overwrite desc. Take summary logic of autosummary
            desc = re.split(r'\n\s*\n', obj_doc.strip(), 1)[0]
            # XXX: Should this have DOTALL?
            #      It does not in autosummary
            m = re.search(r"^([A-Z].*?\.)(?:\s|$)",
                          ' '.join(desc.split()))
            if m:
                desc = m.group(1).strip()
            else:
                desc = desc.partition('\n')[0]
            desc = desc.split('\n')
        return display_param, desc

    def _str_param_list(self, name, fake_autosummary=False):
        """Generate RST for a listing of parameters or similar

        Parameter names are displayed as bold text, and descriptions
        are in blockquotes.  Descriptions may therefore contain block
        markup as well.

        Parameters
        ----------
        name : str
            Section name (e.g. Parameters)
        fake_autosummary : bool
            When True, the parameter names may correspond to attributes of the
            object beign documented, usually ``property`` instances on a class.
            In this case, names will be linked to fuller descriptions.

        Returns
        -------
        rst : list of str
        """
        out = []
        if self[name]:
            out += self._str_field_list(name)
            out += ['']
            for param in self[name]:
                display_param, desc = self._process_param(param.name,
                                                          param.desc,
                                                          fake_autosummary)
                parts = []
                if display_param:
                    parts.append(display_param)
                param_type = param.type
                if param_type:
                    param_type = param.type
                    if self.xref_param_type:
                        param_type = make_xref(
                            param_type,
                            self.xref_aliases,
                            self.xref_ignore
                        )
                    parts.append(param_type)
                out += self._str_indent([' : '.join(parts)])

                if desc and self.use_blockquotes:
                    out += ['']
                elif not desc:
                    # empty definition
                    desc = ['..']
                out += self._str_indent(desc, 8)
                out += ['']

        return out

    def _str_member_list(self, name):
        """
        Generate a member listing, autosummary:: table where possible,
        and a table where not.

        """
        out = []
        if self[name]:
            out += [f'.. rubric:: {name}', '']
            prefix = getattr(self, '_name', '')

            if prefix:
                prefix = f'~{prefix}.'

            autosum = []
            others = []
            for param in self[name]:
                param = param._replace(name=param.name.strip())

                # Check if the referenced member can have a docstring or not
                param_obj = getattr(self._obj, param.name, None)
                if not (callable(param_obj)
                        or isinstance(param_obj, property)
                        or inspect.isdatadescriptor(param_obj)):
                    param_obj = None

                if param_obj and pydoc.getdoc(param_obj):
                    # Referenced object has a docstring
                    autosum += [f"   {prefix}{param.name}"]
                else:
                    others.append(param)

            if autosum:
                out += ['.. autosummary::']
                if self.class_members_toctree:
                    out += ['   :toctree:']
                out += [''] + autosum

            if others:
                maxlen_0 = max(3, max(len(p.name) + 4 for p in others))
                hdr = "=" * maxlen_0 + "  " + "=" * 10
                fmt = '%%%ds  %%s  ' % (maxlen_0,)
                out += ['', '', hdr]
                for param in others:
                    name = "**" + param.name.strip() + "**"
                    desc = " ".join(x.strip()
                                          for x in param.desc).strip()
                    if param.type:
                        desc = f"({param.type}) {desc}"
                    out += [fmt % (name, desc)]
                out += [hdr]
            out += ['']
        return out

    def _str_section(self, name):
        out = []
        if self[name]:
            out += self._str_header(name)
            content = textwrap.dedent("\n".join(self[name])).split("\n")
            out += content
            out += ['']
        return out

    def _str_see_also(self, func_role):
        out = []
        if self['See Also']:
            see_also = super()._str_see_also(func_role)
            out = ['.. seealso::', '']
            out += self._str_indent(see_also[2:])
        return out

    def _str_warnings(self):
        out = []
        if self['Warnings']:
            out = ['.. warning::', '']
            out += self._str_indent(self['Warnings'])
            out += ['']
        return out

    def _str_index(self):
        idx = self['index']
        out = []
        if len(idx) == 0:
            return out

        out += [f".. index:: {idx.get('default', '')}"]
        for section, references in idx.items():
            if section == 'default':
                continue
            elif section == 'refguide':
                out += [f"   single: {', '.join(references)}"]
            else:
                out += [f"   {section}: {','.join(references)}"]
        out += ['']
        return out

    def _str_references(self):
        out = []
        if self['References']:
            out += self._str_header('References')
            if isinstance(self['References'], str):
                self['References'] = [self['References']]
            out.extend(self['References'])
            out += ['']
            # Latex collects all references to a separate bibliography,
            # so we need to insert links to it
            out += ['.. only:: latex', '']
            items = []
            for line in self['References']:
                m = re.match(r'.. \[([a-z0-9._-]+)\]', line, re.I)
                if m:
                    items.append(m.group(1))
            out += ['   ' + ", ".join([f"[{item}]_" for item in items]), '']
        return out

    def _str_examples(self):
        examples_str = "\n".join(self['Examples'])

        if (self.use_plots and re.search(IMPORT_MATPLOTLIB_RE, examples_str)
                and 'plot::' not in examples_str):
            out = []
            out += self._str_header('Examples')
            out += ['.. plot::', '']
            out += self._str_indent(self['Examples'])
            out += ['']
            return out
        else:
            return self._str_section('Examples')

    def __str__(self, indent=0, func_role="obj"):
        ns = {
            'signature':  self._str_signature(),
            'index': self._str_index(),
            'summary': self._str_summary(),
            'extended_summary': self._str_extended_summary(),
            'parameters': self._str_param_list('Parameters'),
            'returns': self._str_returns('Returns'),
            'yields': self._str_returns('Yields'),
            'receives': self._str_returns('Receives'),
            'other_parameters': self._str_param_list('Other Parameters'),
            'raises': self._str_returns('Raises'),
            'warns': self._str_returns('Warns'),
            'warnings': self._str_warnings(),
            'see_also': self._str_see_also(func_role),
            'notes': self._str_section('Notes'),
            'references': self._str_references(),
            'examples': self._str_examples(),
            'attributes':
                self._str_param_list('Attributes', fake_autosummary=True)
                if self.attributes_as_param_list
                else self._str_member_list('Attributes'),
            'methods': self._str_member_list('Methods'),
        }
        ns = {k: '\n'.join(v) for k, v in ns.items()}

        rendered = self.template.render(**ns)
        return '\n'.join(self._str_indent(rendered.split('\n'), indent))


class SphinxFunctionDoc(SphinxDocString, FunctionDoc):
    def __init__(self, obj, doc=None, config=None):
        if config is None:
            config = {}
        self.load_config(config)
        FunctionDoc.__init__(self, obj, doc=doc, config=config)


class SphinxClassDoc(SphinxDocString, ClassDoc):
    def __init__(self, obj, doc=None, func_doc=None, config=None):
        if config is None:
            config = {}
        self.load_config(config)
        ClassDoc.__init__(self, obj, doc=doc, func_doc=None, config=config)


class SphinxObjDoc(SphinxDocString, ObjDoc):
    def __init__(self, obj, doc=None, config=None):
        if config is None:
            config = {}
        self.load_config(config)
        ObjDoc.__init__(self, obj, doc=doc, config=config)


# TODO: refactor to use docscrape.get_doc_object
def get_doc_object(obj, what=None, doc=None, config=None, builder=None):
    if what is None:
        if inspect.isclass(obj):
            what = 'class'
        elif inspect.ismodule(obj):
            what = 'module'
        elif isinstance(obj, Callable):
            what = 'function'
        else:
            what = 'object'

    if config is None:
        config = {}
    template_dirs = [os.path.join(os.path.dirname(__file__), 'templates')]
    if builder is not None:
        template_loader = BuiltinTemplateLoader()
        template_loader.init(builder, dirs=template_dirs)
    else:
        template_loader = FileSystemLoader(template_dirs)
    template_env = SandboxedEnvironment(loader=template_loader)
    config['template'] = template_env.get_template('numpydoc_docstring.rst')

    if what == 'class':
        return SphinxClassDoc(obj, func_doc=SphinxFunctionDoc, doc=doc,
                              config=config)
    elif what in ('function', 'method'):
        return SphinxFunctionDoc(obj, doc=doc, config=config)
    else:
        if doc is None:
            doc = pydoc.getdoc(obj)
        return SphinxObjDoc(obj, doc, config=config)