1. Jason Pellerin
  2. nose2

Source

nose2 / nose2 / sphinxext.py

jpellerin cc311ea 
jpellerin 471affe 
















jpellerin cc311ea 
jpellerin 471affe 





jpellerin cc311ea 












jpellerin 471affe 
















from unittest2.events import PluginsLoadedEvent
from unittest2.util import getObjectFromName

from docutils import nodes
from docutils.statemachine import ViewList
from docutils.parsers.rst import Directive


class AutoPlugin(Directive):
    required_arguments = 1
    optional_arguments = 0
    final_argument_whitespace = False
    has_content = False
    option_spec = {}

    def run(self):
        plugin_name = self.arguments[0]
        parent, plugin = getObjectFromName(plugin_name)
        # FIXME this is too naive
        mod_name = plugin_name[0:plugin_name.index(plugin.__name__)-1]

        rst = ViewList()
        rst.append('.. automodule :: %s\n' % mod_name, '<autodoc>')
        rst.append('', '<autodoc>')

        # FIXME: monkeypatch/hook in somehow so we can replace
        # plugin's config/options with recording mocks. This will
        # have to wait until the new plugin initialization stuff
        # lands.

        # command-line options

        # config options

        # class __doc__
        rst.append(' .. autoclass :: %s\n' % plugin_name, '<autodoc>')
        rst.append('', '<autodoc>')

        print rst

        # parse rst and generate new nodelist
        state = self.state
        node = nodes.section()
        node.document = state.document
        surrounding_title_styles = state.memo.title_styles
        surrounding_section_level = state.memo.section_level
        state.memo.title_styles = []
        state.memo.section_level = 0
        state.nested_parse(rst, 0, node, match_titles=1)
        state.memo.title_styles = surrounding_title_styles
        state.memo.section_level = surrounding_section_level

        return node.children

def setup(app):
    app.add_directive('autoplugin', AutoPlugin)