flexget.plugins.cli.doc
Covered: 11 lines
Missed: 29 lines
Skipped 13 lines
Percent: 27 %
 1
import logging
 2
import sys
 3
from flexget.event import event
 4
from flexget.plugin import register_parser_option, plugins
 6
log = logging.getLogger('doc')
 9
def trim(docstring):
10
    if not docstring:
11
        return ''
14
    lines = docstring.expandtabs().splitlines()
16
    indent = sys.maxint
17
    for line in lines[1:]:
18
        stripped = line.lstrip()
19
        if stripped:
20
            indent = min(indent, len(line) - len(stripped))
22
    trimmed = [lines[0].strip()]
23
    if indent < sys.maxint:
24
        for line in lines[1:]:
25
            trimmed.append(line[indent:].rstrip())
27
    while trimmed and not trimmed[-1]:
28
        trimmed.pop()
29
    while trimmed and not trimmed[0]:
30
        trimmed.pop(0)
32
    return '\n'.join(trimmed)
35
@event('manager.startup')
36
def print_doc(manager):
37
    if manager.options.doc:
38
        manager.disable_feeds()
39
        plugin_name = manager.options.doc
40
        plugin = plugins.get(plugin_name, None)
41
        if plugin:
42
            if not plugin.instance.__doc__:
43
                print 'Plugin %s does not have documentation' % plugin_name
44
            else:
45
                print ''
46
                print trim(plugin.instance.__doc__)
47
                print ''
48
        else:
49
            print 'Could not find plugin %s' % plugin_name
51
register_parser_option('--doc', action='store', dest='doc', default=False,
52
                       metavar='PLUGIN', help='Display plugin documentation. See also --plugins.')