root/galaxy-central/eggs/nose-0.11.1-py2.6.egg/nose/sphinx/pluginopts.py @ 3

リビジョン 3, 5.4 KB (コミッタ: kohda, 14 年 前)

Install Unix tools  http://hannonlab.cshl.edu/galaxy_unix_tools/galaxy.html

行番号 
1"""
2Adds a sphinx directive that can be used to automatically document a plugin.
3
4this::
5
6 .. autoplugin :: nose.plugins.foo
7    :plugin: Pluggy
8   
9produces::
10
11  .. automodule :: nose.plugins.foo
12 
13  Options
14  -------
15
16  .. cmdoption :: --foo=BAR, --fooble=BAR
17
18    Do the foo thing to the new thing.
19
20  Plugin
21  ------
22
23  .. autoclass :: nose.plugins.foo.Pluggy
24     :members:
25
26  Source
27  ------
28
29  .. include :: path/to/nose/plugins/foo.py
30     :literal:
31
32"""
33try:
34    from docutils import nodes
35    from docutils.statemachine import ViewList
36    from docutils.parsers.rst import directives
37except ImportError:
38    pass # won't run anyway
39
40from nose.util import resolve_name
41from nose.plugins.base import Plugin
42from nose.plugins.manager import BuiltinPluginManager
43from nose.config import Config
44from nose.core import TestProgram
45from inspect import isclass
46
47def autoplugin_directive(dirname, arguments, options, content, lineno,
48                         content_offset, block_text, state, state_machine):
49    mod_name = arguments[0]
50    mod = resolve_name(mod_name)
51    plug_name = options.get('plugin', None)
52    if plug_name:
53        obj = getattr(mod, plug_name)
54    else:
55        for entry in dir(mod):
56            obj = getattr(mod, entry)
57            if isclass(obj) and issubclass(obj, Plugin) and obj is not Plugin:
58                plug_name = '%s.%s' % (mod_name, entry)
59                break
60   
61    # mod docstring
62    rst = ViewList()
63    rst.append('.. automodule :: %s\n' % mod_name, '<autodoc>')
64    rst.append('', '<autodoc>')
65   
66    # options
67    rst.append('Options', '<autodoc>')
68    rst.append('-------', '<autodoc>')
69    rst.append('', '<autodoc>')
70
71    plug = obj()
72    opts = OptBucket()
73    plug.options(opts, {})
74    for opt in opts:
75        rst.append(opt.options(), '<autodoc>')
76        rst.append('   \n', '<autodoc>')
77        rst.append('   ' + opt.help + '\n', '<autodoc>')
78        rst.append('\n', '<autodoc>')
79       
80    # plugin class
81    rst.append('Plugin', '<autodoc>')
82    rst.append('------', '<autodoc>')
83    rst.append('', '<autodoc>')
84   
85    rst.append('.. autoclass :: %s\n' % plug_name, '<autodoc>')
86    rst.append('   :members:\n', '<autodoc>')
87    rst.append('   :show-inheritance:\n', '<autodoc>')
88    rst.append('', '<autodoc>')
89   
90    # source
91    rst.append('Source', '<autodoc>')
92    rst.append('------', '<autodoc>')
93    rst.append('.. include :: %s\n' % mod.__file__.replace('.pyc', '.py'),
94               '<autodoc>')
95    rst.append('   :literal:\n', '<autodoc>')
96    rst.append('', '<autodoc>')
97   
98    node = nodes.section()
99    node.document = state.document
100    surrounding_title_styles = state.memo.title_styles
101    surrounding_section_level = state.memo.section_level
102    state.memo.title_styles = []
103    state.memo.section_level = 0
104    state.nested_parse(rst, 0, node, match_titles=1)
105    state.memo.title_styles = surrounding_title_styles
106    state.memo.section_level = surrounding_section_level
107
108    return node.children
109
110
111def autohelp_directive(dirname, arguments, options, content, lineno,
112                       content_offset, block_text, state, state_machine):
113    """produces rst from nose help"""
114    config = Config(parserClass=OptBucket,
115                    plugins=BuiltinPluginManager())
116    parser = config.getParser(TestProgram.usage())
117    rst = ViewList()
118    for line in parser.format_help().split('\n'):
119        rst.append(line, '<autodoc>')
120
121    rst.append('Options', '<autodoc>')
122    rst.append('-------', '<autodoc>')
123    rst.append('', '<autodoc>')
124    for opt in parser:
125        rst.append(opt.options(), '<autodoc>')
126        rst.append('   \n', '<autodoc>')
127        rst.append('   ' + opt.help + '\n', '<autodoc>')
128        rst.append('\n', '<autodoc>')   
129    node = nodes.section()
130    node.document = state.document
131    surrounding_title_styles = state.memo.title_styles
132    surrounding_section_level = state.memo.section_level
133    state.memo.title_styles = []
134    state.memo.section_level = 0
135    state.nested_parse(rst, 0, node, match_titles=1)
136    state.memo.title_styles = surrounding_title_styles
137    state.memo.section_level = surrounding_section_level
138
139    return node.children
140
141   
142class OptBucket(object):
143    def __init__(self, doc=None, prog='nosetests'):
144        self.opts = []
145        self.doc = doc
146        self.prog = prog
147
148    def __iter__(self):
149        return iter(self.opts)
150
151    def format_help(self):
152        return self.doc.replace('%prog', self.prog).replace(':\n', '::\n')
153   
154    def add_option(self, *arg, **kw):
155        self.opts.append(Opt(*arg, **kw))
156
157
158class Opt(object):
159    def __init__(self, *arg, **kw):
160        self.opts = arg
161        self.action = kw.pop('action', None)
162        self.default = kw.pop('default', None)
163        self.metavar = kw.pop('metavar', None)
164        self.help = kw.pop('help', None)
165
166    def options(self):
167        buf = []
168        for optstring in self.opts:
169            desc = optstring
170            if self.action not in ('store_true', 'store_false'):
171                desc += '=%s' % self.meta(optstring)
172            buf.append(desc)
173        return '.. cmdoption :: ' + ', '.join(buf)
174
175    def meta(self, optstring):
176        # FIXME optparser default metavar?
177        return self.metavar or 'DEFAULT'
178
179   
180def setup(app):
181    app.add_directive('autoplugin',
182                      autoplugin_directive, 1, (1, 0, 1),
183                      plugin=directives.unchanged)
184    app.add_directive('autohelp', autohelp_directive, 0, (0, 0, 1))
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。