File: auto_doc.py

package info (click to toggle)
mayavi2 4.8.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,892 kB
  • sloc: python: 49,447; javascript: 32,885; makefile: 129; fortran: 60
file content (77 lines) | stat: -rw-r--r-- 2,225 bytes parent folder | download | duplicates (3)
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
"""
Automatic documentation from traited objects.
"""

# Author: Gael Varoquaux <gael.varoquaux@normalesup.org>
# Copyright (c) 2007-2020, Enthought, Inc.
# License: BSD Style.

from textwrap import wrap, dedent
import types

old_dedent = dedent


def dedent(text):
    """ Removes as much indentation as possible from some text, but does
    not modify the first line.
    """
    text_lines = [line.rstrip() for line in text.split("\n")]
    if len(text_lines) > 0:
        return text_lines[0] + "\n" + old_dedent(
                                            "\n".join(text_lines[1:]))
    else:
        return text


def make_doc(klass):
    """ Builds a docstring from the object's docstring, and it's traits
    help.
    """
    if hasattr(klass, '__doc__'):
        doc = dedent(klass.__doc__) + "\n"
    else:
        doc = ""
    doc += dedent("""**Keyword arguments:**""")
    traits = klass.class_traits().copy()
    traits.pop('trait_added')
    traits.pop('trait_modified')
    doc += traits_doc(traits)
    return doc


def traits_doc(traits):
    doc = ""
    traits_names = sorted(traits.keys())
    for trait_name in traits_names:
        trait_obj = traits[trait_name]
        if not trait_name[0] == '_':
            doc += format_argument(trait_name, trait_obj)
    return doc


def format_argument(trait_name, trait_obj):
    doc = "\n    :%s: " % trait_name
    pad = "\n" + (len(doc) - 1) * " "
    help = trait_obj.help
    if help is not None:
        arg_desc = help
    else:
        arg_desc = ''
        desc = trait_obj.desc
        if desc is not None:
            arg_desc += desc.rstrip()
        handler = trait_obj.handler
        if handler is not None:
            if (not hasattr(handler, 'aType') or
                        not handler.aType in (int, float)):
                # These types are simple enough
                arg_desc += ' Must be %s.' % handler.info()
        default = trait_obj.default_value()[1]
        if not default in ('', None) and not trait_obj.array:
            arg_desc = arg_desc.rstrip() + " Default: %s" % str(default)
    desc_width = 75 - len(doc)
    for line in wrap(arg_desc, width=desc_width):
        doc += line
        doc += pad
    return doc