buildframework/helium/external/python/lib/common/Sphinx-0.5.1-py2.5.egg/sphinx/ext/autodoc.py
author wbernard
Wed, 23 Dec 2009 19:29:07 +0200
changeset 179 d8ac696cc51f
permissions -rw-r--r--
helium_7.0-r14027
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
179
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     2
"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     3
    sphinx.ext.autodoc
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     4
    ~~~~~~~~~~~~~~~~~~
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     5
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     6
    Automatically insert docstrings for functions, classes or whole modules into
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     7
    the doctree, thus avoiding duplication between docstrings and documentation
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     8
    for those who like elaborate docstrings.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     9
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    10
    :copyright: 2008 by Georg Brandl, Pauli Virtanen, Martin Hans.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    11
    :license: BSD.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    12
"""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    13
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    14
import re
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    15
import sys
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    16
import types
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    17
import inspect
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    18
import linecache
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    19
from types import FunctionType, BuiltinMethodType, MethodType
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    20
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    21
from docutils import nodes
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    22
from docutils.parsers.rst import directives
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    23
from docutils.statemachine import ViewList
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    24
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    25
from sphinx.util import rpartition, nested_parse_with_titles
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    26
from sphinx.directives.desc import py_sig_re
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    27
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    28
try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    29
    base_exception = BaseException
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    30
except NameError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    31
    base_exception = Exception
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    32
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    33
_charset_re = re.compile(r'coding[:=]\s*([-\w.]+)')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    34
_module_charsets = {}
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    35
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    36
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    37
class Options(object):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    38
    pass
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    39
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    40
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    41
def is_static_method(obj):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    42
    """Check if the object given is a static method."""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    43
    if isinstance(obj, (FunctionType, classmethod)):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    44
        return True
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    45
    elif isinstance(obj, BuiltinMethodType):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    46
        return obj.__self__ is not None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    47
    elif isinstance(obj, MethodType):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    48
        return obj.im_self is not None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    49
    return False
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    50
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    51
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    52
class AutodocReporter(object):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    53
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    54
    A reporter replacement that assigns the correct source name
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    55
    and line number to a system message, as recorded in a ViewList.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    56
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    57
    def __init__(self, viewlist, reporter):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    58
        self.viewlist = viewlist
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    59
        self.reporter = reporter
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    60
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    61
    def __getattr__(self, name):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    62
        return getattr(self.reporter, name)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    63
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    64
    def system_message(self, level, message, *children, **kwargs):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    65
        if 'line' in kwargs:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    66
            try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    67
                source, line = self.viewlist.items[kwargs['line']]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    68
            except IndexError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    69
                pass
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    70
            else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    71
                kwargs['source'] = source
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    72
                kwargs['line'] = line
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    73
        return self.reporter.system_message(level, message,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    74
                                            *children, **kwargs)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    75
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    76
    def debug(self, *args, **kwargs):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    77
        if self.reporter.debug_flag:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    78
            return self.system_message(0, *args, **kwargs)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    79
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    80
    def info(self, *args, **kwargs):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    81
        return self.system_message(1, *args, **kwargs)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    82
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    83
    def warning(self, *args, **kwargs):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    84
        return self.system_message(2, *args, **kwargs)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    85
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    86
    def error(self, *args, **kwargs):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    87
        return self.system_message(3, *args, **kwargs)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    88
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    89
    def severe(self, *args, **kwargs):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    90
        return self.system_message(4, *args, **kwargs)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    91
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    92
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    93
# Some useful event listener factories for autodoc-process-docstring.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    94
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    95
def cut_lines(pre, post=0, what=None):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    96
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    97
    Return a listener that removes the first *pre* and last *post*
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    98
    lines of every docstring.  If *what* is a sequence of strings,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    99
    only docstrings of a type in *what* will be processed.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   100
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   101
    Use like this (e.g. in the ``setup()`` function of :file:`conf.py`)::
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   102
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   103
       from sphinx.ext.autodoc import cut_lines
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   104
       app.connect('autodoc-process-docstring', cut_lines(4, what=['module']))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   105
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   106
    This can (and should) be used in place of :confval:`automodule_skip_lines`.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   107
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   108
    def process(app, what_, name, obj, options, lines):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   109
        if what and what_ not in what:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   110
            return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   111
        del lines[:pre]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   112
        if post:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   113
            # remove one trailing blank line.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   114
            if lines and not lines[-1]:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   115
                lines.pop(-1)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   116
            del lines[-post:]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   117
        # make sure there is a blank line at the end
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   118
        if lines and lines[-1]:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   119
            lines.append('')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   120
    return process
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   121
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   122
def between(marker, what=None, keepempty=False):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   123
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   124
    Return a listener that only keeps lines between lines that match the
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   125
    *marker* regular expression.  If no line matches, the resulting docstring
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   126
    would be empty, so no change will be made unless *keepempty* is true.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   127
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   128
    If *what* is a sequence of strings, only docstrings of a type in *what* will
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   129
    be processed.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   130
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   131
    marker_re = re.compile(marker)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   132
    def process(app, what_, name, obj, options, lines):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   133
        if what and what_ not in what:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   134
            return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   135
        deleted = 0
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   136
        delete = True
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   137
        orig_lines = lines[:]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   138
        for i, line in enumerate(orig_lines):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   139
            if delete:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   140
                lines.pop(i - deleted)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   141
                deleted += 1
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   142
            if marker_re.match(line):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   143
                delete = not delete
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   144
                if delete:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   145
                    lines.pop(i - deleted)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   146
                    deleted += 1
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   147
        if not lines and not keepempty:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   148
            lines[:] = orig_lines
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   149
        # make sure there is a blank line at the end
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   150
        if lines and lines[-1]:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   151
            lines.append('')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   152
    return process
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   153
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   154
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   155
def isdescriptor(x):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   156
    """Check if the object is some kind of descriptor."""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   157
    for item in '__get__', '__set__', '__delete__':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   158
        if callable(getattr(x, item, None)):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   159
            return True
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   160
    return False
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   161
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   162
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   163
def prepare_docstring(s):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   164
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   165
    Convert a docstring into lines of parseable reST.  Return it as a list of
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   166
    lines usable for inserting into a docutils ViewList (used as argument
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   167
    of nested_parse().)  An empty line is added to act as a separator between
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   168
    this docstring and following content.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   169
    """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   170
    lines = s.expandtabs().splitlines()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   171
    # Find minimum indentation of any non-blank lines after first line.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   172
    margin = sys.maxint
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   173
    for line in lines[1:]:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   174
        content = len(line.lstrip())
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   175
        if content:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   176
            indent = len(line) - content
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   177
            margin = min(margin, indent)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   178
    # Remove indentation.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   179
    if lines:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   180
        lines[0] = lines[0].lstrip()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   181
    if margin < sys.maxint:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   182
        for i in range(1, len(lines)): lines[i] = lines[i][margin:]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   183
    # Remove any leading blank lines.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   184
    while lines and not lines[0]:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   185
        lines.pop(0)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   186
    # make sure there is an empty line at the end
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   187
    if lines and lines[-1]:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   188
        lines.append('')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   189
    return lines
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   190
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   191
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   192
def get_module_charset(module):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   193
    """Return the charset of the given module (cached in _module_charsets)."""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   194
    if module in _module_charsets:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   195
        return _module_charsets[module]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   196
    try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   197
        filename = __import__(module, None, None, ['foo']).__file__
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   198
    except (ImportError, AttributeError):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   199
        return None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   200
    if filename[-4:].lower() in ('.pyc', '.pyo'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   201
        filename = filename[:-1]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   202
    for line in [linecache.getline(filename, x) for x in (1, 2)]:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   203
        match = _charset_re.search(line)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   204
        if match is not None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   205
            charset = match.group(1)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   206
            break
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   207
    else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   208
        charset = 'ascii'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   209
    _module_charsets[module] = charset
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   210
    return charset
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   211
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   212
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   213
class RstGenerator(object):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   214
    def __init__(self, options, document, lineno):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   215
        self.options = options
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   216
        self.env = document.settings.env
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   217
        self.reporter = document.reporter
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   218
        self.lineno = lineno
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   219
        self.filename_set = set()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   220
        self.warnings = []
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   221
        self.result = ViewList()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   222
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   223
    def warn(self, msg):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   224
        self.warnings.append(self.reporter.warning(msg, line=self.lineno))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   225
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   226
    def get_doc(self, what, name, obj):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   227
        """Format and yield lines of the docstring(s) for the object."""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   228
        docstrings = []
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   229
        if getattr(obj, '__doc__', None):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   230
            docstrings.append(obj.__doc__)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   231
        # skip some lines in module docstrings if configured
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   232
        if what == 'module' and self.env.config.automodule_skip_lines and docstrings:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   233
            docstrings[0] = '\n'.join(docstrings[0].splitlines()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   234
                                      [self.env.config.automodule_skip_lines:])
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   235
        # for classes, what the "docstring" is can be controlled via an option
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   236
        if what in ('class', 'exception'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   237
            content = self.env.config.autoclass_content
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   238
            if content in ('both', 'init'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   239
                initdocstring = getattr(obj, '__init__', None).__doc__
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   240
                # for new-style classes, no __init__ means default __init__
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   241
                if initdocstring == object.__init__.__doc__:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   242
                    initdocstring = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   243
                if initdocstring:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   244
                    if content == 'init':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   245
                        docstrings = [initdocstring]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   246
                    else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   247
                        docstrings.append(initdocstring)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   248
            # the default is only the class docstring
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   249
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   250
        # decode the docstrings using the module's source encoding
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   251
        charset = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   252
        module = getattr(obj, '__module__', None)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   253
        if module is not None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   254
            charset = get_module_charset(module)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   255
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   256
        for docstring in docstrings:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   257
            if isinstance(docstring, str):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   258
                if charset:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   259
                    docstring = docstring.decode(charset)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   260
                else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   261
                    try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   262
                        # try decoding with utf-8, should only work for real UTF-8
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   263
                        docstring = docstring.decode('utf-8')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   264
                    except UnicodeError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   265
                        # last resort -- can't fail
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   266
                        docstring = docstring.decode('latin1')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   267
            docstringlines = prepare_docstring(docstring)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   268
            if self.env.app:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   269
                # let extensions preprocess docstrings
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   270
                self.env.app.emit('autodoc-process-docstring',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   271
                                  what, name, obj, self.options, docstringlines)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   272
            for line in docstringlines:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   273
                yield line
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   274
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   275
    def resolve_name(self, what, name):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   276
        """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   277
        Determine what module to import and what attribute to document.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   278
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   279
        Returns a tuple of: the full name, the module name, a path of
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   280
        names to get via getattr, the signature and return annotation.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   281
        """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   282
        # first, parse the definition -- auto directives for classes and functions
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   283
        # can contain a signature which is then used instead of an autogenerated one
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   284
        try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   285
            path, base, args, retann = py_sig_re.match(name).groups()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   286
        except:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   287
            self.warn('invalid signature for auto%s (%r)' % (what, name))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   288
            return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   289
        # fullname is the fully qualified name, base the name after the last dot
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   290
        fullname = (path or '') + base
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   291
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   292
        if what == 'module':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   293
            if args or retann:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   294
                self.warn('ignoring signature arguments and return annotation '
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   295
                          'for automodule %s' % fullname)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   296
            return fullname, fullname, [], None, None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   297
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   298
        elif what in ('class', 'exception', 'function'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   299
            if path:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   300
                mod = path.rstrip('.')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   301
            else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   302
                mod = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   303
                # if documenting a toplevel object without explicit module, it can
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   304
                # be contained in another auto directive ...
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   305
                if hasattr(self.env, 'autodoc_current_module'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   306
                    mod = self.env.autodoc_current_module
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   307
                # ... or in the scope of a module directive
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   308
                if not mod:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   309
                    mod = self.env.currmodule
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   310
            return fullname, mod, [base], args, retann
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   311
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   312
        else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   313
            if path:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   314
                mod_cls = path.rstrip('.')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   315
            else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   316
                mod_cls = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   317
                # if documenting a class-level object without path, there must be a
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   318
                # current class, either from a parent auto directive ...
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   319
                if hasattr(self.env, 'autodoc_current_class'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   320
                    mod_cls = self.env.autodoc_current_class
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   321
                # ... or from a class directive
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   322
                if mod_cls is None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   323
                    mod_cls = self.env.currclass
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   324
                # ... if still None, there's no way to know
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   325
                if mod_cls is None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   326
                    return fullname, None, [], args, retann
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   327
            mod, cls = rpartition(mod_cls, '.')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   328
            # if the module name is still missing, get it like above
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   329
            if not mod and hasattr(self.env, 'autodoc_current_module'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   330
                mod = self.env.autodoc_current_module
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   331
            if not mod:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   332
                mod = self.env.currmodule
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   333
            return fullname, mod, [cls, base], args, retann
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   334
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   335
    def format_signature(self, what, name, obj, args, retann):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   336
        """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   337
        Return the signature of the object, formatted for display.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   338
        """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   339
        if what not in ('class', 'method', 'function'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   340
            return ''
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   341
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   342
        err = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   343
        if args is not None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   344
            # signature given explicitly
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   345
            args = "(%s)" % args
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   346
        else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   347
            # try to introspect the signature
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   348
            try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   349
                args = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   350
                getargs = True
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   351
                if what == 'class':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   352
                    # for classes, the relevant signature is the __init__ method's
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   353
                    obj = getattr(obj, '__init__', None)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   354
                    # classes without __init__ method, default __init__ or
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   355
                    # __init__ written in C?
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   356
                    if obj is None or obj is object.__init__ or not \
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   357
                       (inspect.ismethod(obj) or inspect.isfunction(obj)):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   358
                        getargs = False
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   359
                elif inspect.isbuiltin(obj) or inspect.ismethoddescriptor(obj):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   360
                    # can never get arguments of a C function or method
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   361
                    getargs = False
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   362
                if getargs:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   363
                    argspec = inspect.getargspec(obj)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   364
                    if what in ('class', 'method') and argspec[0] and \
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   365
                           argspec[0][0] in ('cls', 'self'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   366
                        del argspec[0][0]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   367
                    args = inspect.formatargspec(*argspec)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   368
            except Exception, e:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   369
                args = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   370
                err = e
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   371
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   372
        result = self.env.app.emit_firstresult('autodoc-process-signature', what,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   373
                                               name, obj, self.options, args, retann)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   374
        if result:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   375
            args, retann = result
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   376
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   377
        if args is not None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   378
            return '%s%s' % (args, retann or '')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   379
        elif err:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   380
            # re-raise the error for perusal of the handler in generate()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   381
            raise RuntimeError(err)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   382
        else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   383
            return ''
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   384
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   385
    def generate(self, what, name, members, add_content, indent=u'', check_module=False):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   386
        """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   387
        Generate reST for the object in self.result.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   388
        """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   389
        fullname, mod, objpath, args, retann = self.resolve_name(what, name)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   390
        if not mod:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   391
            # need a module to import
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   392
            self.warn('don\'t know which module to import for autodocumenting %r '
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   393
                      '(try placing a "module" or "currentmodule" directive in the '
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   394
                      'document, or giving an explicit module name)' % fullname)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   395
            return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   396
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   397
        # the name to put into the generated directive -- doesn't contain the module
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   398
        name_in_directive = '.'.join(objpath) or mod
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   399
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   400
        # now, import the module and get object to document
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   401
        try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   402
            todoc = module = __import__(mod, None, None, ['foo'])
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   403
            if hasattr(module, '__file__') and module.__file__:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   404
                modfile = module.__file__
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   405
                if modfile[-4:].lower() in ('.pyc', '.pyo'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   406
                    modfile = modfile[:-1]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   407
                self.filename_set.add(modfile)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   408
            else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   409
                modfile = None  # e.g. for builtin and C modules
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   410
            for part in objpath:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   411
                todoc = getattr(todoc, part)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   412
        except (ImportError, AttributeError), err:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   413
            self.warn('autodoc can\'t import/find %s %r, it reported error: "%s", '
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   414
                      'please check your spelling and sys.path' %
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   415
                      (what, str(fullname), err))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   416
            return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   417
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   418
        # check __module__ of object if wanted (for members not given explicitly)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   419
        if check_module:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   420
            if hasattr(todoc, '__module__'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   421
                if todoc.__module__ != mod:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   422
                    return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   423
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   424
        # format the object's signature, if any
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   425
        try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   426
            sig = self.format_signature(what, name, todoc, args, retann)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   427
        except Exception, err:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   428
            self.warn('error while formatting signature for %s: %s' %
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   429
                      (fullname, err))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   430
            sig = ''
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   431
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   432
        # make sure that the result starts with an empty line.  This is
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   433
        # necessary for some situations where another directive preprocesses
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   434
        # reST and no starting newline is present
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   435
        self.result.append(u'', '')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   436
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   437
        # now, create the directive header
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   438
        directive = (what == 'method' and is_static_method(todoc)) \
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   439
                    and 'staticmethod' or what
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   440
        self.result.append(indent + u'.. %s:: %s%s' %
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   441
                           (directive, name_in_directive, sig), '<autodoc>')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   442
        if what == 'module':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   443
            # Add some module-specific options
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   444
            if self.options.synopsis:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   445
                self.result.append(indent + u'   :synopsis: ' + self.options.synopsis,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   446
                              '<autodoc>')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   447
            if self.options.platform:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   448
                self.result.append(indent + u'   :platform: ' + self.options.platform,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   449
                              '<autodoc>')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   450
            if self.options.deprecated:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   451
                self.result.append(indent + u'   :deprecated:', '<autodoc>')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   452
        else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   453
            # Be explicit about the module, this is necessary since .. class:: doesn't
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   454
            # support a prepended module name
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   455
            self.result.append(indent + u'   :module: %s' % mod, '<autodoc>')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   456
        if self.options.noindex:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   457
            self.result.append(indent + u'   :noindex:', '<autodoc>')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   458
        self.result.append(u'', '<autodoc>')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   459
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   460
        if self.options.show_inheritance and what in ('class', 'exception'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   461
            if len(todoc.__bases__):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   462
                bases = [b.__module__ == '__builtin__' and
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   463
                         u':class:`%s`' % b.__name__ or
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   464
                         u':class:`%s.%s`' % (b.__module__, b.__name__)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   465
                         for b in todoc.__bases__]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   466
                self.result.append(indent + u'   Bases: %s' % ', '.join(bases),
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   467
                                   '<autodoc>')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   468
                self.result.append(u'', '<autodoc>')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   469
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   470
        # the module directive doesn't have content
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   471
        if what != 'module':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   472
            indent += u'   '
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   473
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   474
        if modfile:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   475
            sourcename = '%s:docstring of %s' % (modfile, fullname)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   476
        else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   477
            sourcename = 'docstring of %s' % fullname
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   478
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   479
        # add content from docstrings
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   480
        for i, line in enumerate(self.get_doc(what, fullname, todoc)):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   481
            self.result.append(indent + line, sourcename, i)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   482
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   483
        # add source content, if present
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   484
        if add_content:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   485
            for line, src in zip(add_content.data, add_content.items):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   486
                self.result.append(indent + line, src[0], src[1])
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   487
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   488
        # document members?
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   489
        if not members or what in ('function', 'method', 'attribute'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   490
            return
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   491
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   492
        # set current namespace for finding members
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   493
        self.env.autodoc_current_module = mod
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   494
        if objpath:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   495
            self.env.autodoc_current_class = objpath[0]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   496
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   497
        # add members, if possible
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   498
        _all = members == ['__all__']
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   499
        members_check_module = False
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   500
        if _all:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   501
            # unqualified :members: given
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   502
            if what == 'module':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   503
                if hasattr(todoc, '__all__'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   504
                    members_check_module = False
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   505
                    all_members = []
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   506
                    for mname in todoc.__all__:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   507
                        try:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   508
                            all_members.append((mname, getattr(todoc, mname)))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   509
                        except AttributeError:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   510
                            self.warn('missing attribute mentioned in __all__: '
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   511
                                      'module %s, attribute %s' %
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   512
                                      (todoc.__name__, mname))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   513
                else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   514
                    # for implicit module members, check __module__ to avoid
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   515
                    # documenting imported objects
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   516
                    members_check_module = True
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   517
                    all_members = inspect.getmembers(todoc)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   518
            else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   519
                if self.options.inherited_members:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   520
                    # getmembers() uses dir() which pulls in members from all
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   521
                    # base classes
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   522
                    all_members = inspect.getmembers(todoc)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   523
                else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   524
                    # __dict__ contains only the members directly defined in the class
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   525
                    all_members = sorted(todoc.__dict__.iteritems())
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   526
        else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   527
            all_members = [(mname, getattr(todoc, mname)) for mname in members]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   528
        for (membername, member) in all_members:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   529
            # ignore members whose name starts with _ by default
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   530
            if _all and membername.startswith('_'):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   531
                continue
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   532
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   533
            # ignore undocumented members if :undoc-members: is not given
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   534
            doc = getattr(member, '__doc__', None)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   535
            skip = not self.options.undoc_members and not doc
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   536
            # give the user a chance to decide whether this member should be skipped
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   537
            if self.env.app:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   538
                # let extensions preprocess docstrings
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   539
                skip_user = self.env.app.emit_firstresult(
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   540
                    'autodoc-skip-member', what, membername, member, skip, self.options)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   541
                if skip_user is not None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   542
                    skip = skip_user
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   543
            if skip:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   544
                continue
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   545
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   546
            if what == 'module':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   547
                if isinstance(member, (types.FunctionType,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   548
                                       types.BuiltinFunctionType)):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   549
                    memberwhat = 'function'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   550
                elif isinstance(member, types.ClassType) or \
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   551
                     isinstance(member, type):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   552
                    if issubclass(member, base_exception):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   553
                        memberwhat = 'exception'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   554
                    else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   555
                        memberwhat = 'class'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   556
                else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   557
                    # XXX: todo -- attribute docs
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   558
                    continue
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   559
            else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   560
                if callable(member):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   561
                    memberwhat = 'method'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   562
                elif isdescriptor(member):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   563
                    memberwhat = 'attribute'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   564
                else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   565
                    # XXX: todo -- attribute docs
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   566
                    continue
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   567
            full_membername = fullname + '.' + membername
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   568
            self.generate(memberwhat, full_membername, ['__all__'], None, indent,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   569
                          check_module=members_check_module)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   570
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   571
        self.env.autodoc_current_module = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   572
        self.env.autodoc_current_class = None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   573
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   574
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   575
def _auto_directive(dirname, arguments, options, content, lineno,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   576
                    content_offset, block_text, state, state_machine):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   577
    what = dirname[4:]  # strip "auto"
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   578
    name = arguments[0]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   579
    genopt = Options()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   580
    members = options.get('members', [])
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   581
    genopt.inherited_members = 'inherited-members' in options
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   582
    if genopt.inherited_members and not members:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   583
        # :inherited-members: implies :members:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   584
        members = ['__all__']
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   585
    genopt.undoc_members = 'undoc-members' in options
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   586
    genopt.show_inheritance = 'show-inheritance' in options
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   587
    genopt.noindex = 'noindex' in options
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   588
    genopt.synopsis = options.get('synopsis', '')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   589
    genopt.platform = options.get('platform', '')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   590
    genopt.deprecated = 'deprecated' in options
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   591
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   592
    generator = RstGenerator(genopt, state.document, lineno)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   593
    generator.generate(what, name, members, content)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   594
    if not generator.result:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   595
        return generator.warnings
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   596
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   597
    # record all filenames as dependencies -- this will at least partially make
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   598
    # automatic invalidation possible
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   599
    for fn in generator.filename_set:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   600
        state.document.settings.env.note_dependency(fn)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   601
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   602
    # use a custom reporter that correctly assigns lines to source and lineno
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   603
    old_reporter = state.memo.reporter
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   604
    state.memo.reporter = AutodocReporter(generator.result, state.memo.reporter)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   605
    if dirname == 'automodule':
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   606
        node = nodes.section()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   607
        nested_parse_with_titles(state, generator.result, node)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   608
    else:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   609
        node = nodes.paragraph()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   610
        state.nested_parse(generator.result, 0, node)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   611
    state.memo.reporter = old_reporter
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   612
    return generator.warnings + node.children
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   613
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   614
def auto_directive(*args, **kwds):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   615
    return _auto_directive(*args, **kwds)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   616
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   617
def automodule_directive(*args, **kwds):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   618
    return _auto_directive(*args, **kwds)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   619
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   620
def autoclass_directive(*args, **kwds):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   621
    return _auto_directive(*args, **kwds)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   622
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   623
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   624
def members_option(arg):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   625
    if arg is None:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   626
        return ['__all__']
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   627
    return [x.strip() for x in arg.split(',')]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   628
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   629
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   630
def setup(app):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   631
    mod_options = {'members': members_option, 'undoc-members': directives.flag,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   632
                   'noindex': directives.flag, 'inherited-members': directives.flag,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   633
                   'show-inheritance': directives.flag, 'synopsis': lambda x: x,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   634
                   'platform': lambda x: x, 'deprecated': directives.flag}
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   635
    cls_options = {'members': members_option, 'undoc-members': directives.flag,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   636
                   'noindex': directives.flag, 'inherited-members': directives.flag,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   637
                   'show-inheritance': directives.flag}
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   638
    app.add_directive('automodule', automodule_directive,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   639
                      1, (1, 0, 1), **mod_options)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   640
    app.add_directive('autoclass', autoclass_directive,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   641
                      1, (1, 0, 1), **cls_options)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   642
    app.add_directive('autoexception', autoclass_directive,
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   643
                      1, (1, 0, 1), **cls_options)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   644
    app.add_directive('autofunction', auto_directive, 1, (1, 0, 1),
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   645
                      noindex=directives.flag)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   646
    app.add_directive('automethod', auto_directive, 1, (1, 0, 1),
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   647
                      noindex=directives.flag)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   648
    app.add_directive('autoattribute', auto_directive, 1, (1, 0, 1),
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   649
                      noindex=directives.flag)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   650
    # deprecated: remove in some future version.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   651
    app.add_config_value('automodule_skip_lines', 0, True)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   652
    app.add_config_value('autoclass_content', 'class', True)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   653
    app.add_event('autodoc-process-docstring')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   654
    app.add_event('autodoc-process-signature')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   655
    app.add_event('autodoc-skip-member')