~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/Twisted-10.0.0/twisted/lore/scripts/lore.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2001-2004 Twisted Matrix Laboratories.
 
2
# See LICENSE for details.
 
3
 
 
4
import sys
 
5
 
 
6
from zope.interface import Interface, Attribute
 
7
 
 
8
from twisted.lore import process, indexer, numberer, htmlbook
 
9
 
 
10
from twisted.python import usage, reflect
 
11
from twisted import plugin as plugin
 
12
 
 
13
class IProcessor(Interface):
 
14
    """
 
15
    """
 
16
 
 
17
    name = Attribute("The user-facing name of this processor")
 
18
 
 
19
    moduleName = Attribute(
 
20
        "The fully qualified Python name of the object defining "
 
21
        "this processor.  This object (typically a module) should "
 
22
        "have a C{factory} attribute with C{generate_<output>} methods.")
 
23
 
 
24
 
 
25
class Options(usage.Options):
 
26
 
 
27
    longdesc = "lore converts documentation formats."
 
28
 
 
29
    optFlags = [["plain", 'p', "Report filenames without progress bar"],
 
30
                ["null", 'n', "Do not report filenames"],
 
31
                ["number", 'N', "Add chapter/section numbers to section headings"],
 
32
]
 
33
 
 
34
    optParameters = [
 
35
                     ["input", "i", 'lore'],
 
36
                     ["inputext", "e", ".xhtml", "The extension that your Lore input files have"],
 
37
                     ["docsdir", "d", None],
 
38
                     ["linkrel", "l", ''],
 
39
                     ["output", "o", 'html'],
 
40
                     ["index", "x", None, "The base filename you want to give your index file"],
 
41
                     ["book", "b", None, "The book file to generate a book from"],
 
42
                     ["prefixurl", None, "", "The prefix to stick on to relative links; only useful when processing directories"],
 
43
                    ]
 
44
 
 
45
    #zsh_altArgDescr = {"foo":"use this description for foo instead"}
 
46
    #zsh_multiUse = ["foo", "bar"]
 
47
    #zsh_mutuallyExclusive = [("foo", "bar"), ("bar", "baz")]
 
48
    #zsh_actions = {"foo":'_files -g "*.foo"', "bar":"(one two three)"}
 
49
    #zsh_actionDescr = {"logfile":"log file name", "random":"random seed"}
 
50
    zsh_extras = ["*:files:_files"]
 
51
 
 
52
    def __init__(self, *args, **kw):
 
53
        usage.Options.__init__(self, *args, **kw)
 
54
        self.config = {}
 
55
 
 
56
    def opt_config(self, s):
 
57
        if '=' in s:
 
58
            k, v = s.split('=', 1)
 
59
            self.config[k] = v
 
60
        else:
 
61
            self.config[s] = 1
 
62
 
 
63
    def parseArgs(self, *files):
 
64
        self['files'] = files
 
65
 
 
66
 
 
67
def getProcessor(input, output, config):
 
68
    plugins = plugin.getPlugins(IProcessor)
 
69
    for plug in plugins:
 
70
        if plug.name == input:
 
71
            module = reflect.namedModule(plug.moduleName)
 
72
            break
 
73
    else:
 
74
        # try treating it as a module name
 
75
        try:
 
76
            module = reflect.namedModule(input)
 
77
        except ImportError:
 
78
            print '%s: no such input: %s' % (sys.argv[0], input)
 
79
            return
 
80
    try:
 
81
        return process.getProcessor(module, output, config)
 
82
    except process.NoProcessorError, e:
 
83
        print "%s: %s" % (sys.argv[0], e)
 
84
 
 
85
 
 
86
def getWalker(df, opt):
 
87
    klass = process.Walker
 
88
    if opt['plain']:
 
89
        klass = process.PlainReportingWalker
 
90
    if opt['null']:
 
91
        klass = process.NullReportingWalker
 
92
    return klass(df, opt['inputext'], opt['linkrel'])
 
93
 
 
94
 
 
95
def runGivenOptions(opt):
 
96
    """Do everything but parse the options; useful for testing.
 
97
    Returns a descriptive string if there's an error."""
 
98
 
 
99
    book = None
 
100
    if opt['book']:
 
101
        book = htmlbook.Book(opt['book'])
 
102
 
 
103
    df = getProcessor(opt['input'], opt['output'], opt.config)
 
104
    if not df:
 
105
        return 'getProcessor() failed'
 
106
 
 
107
    walker = getWalker(df, opt)
 
108
 
 
109
    if opt['files']:
 
110
        for filename in opt['files']:
 
111
            walker.walked.append(('', filename))
 
112
    elif book:
 
113
        for filename in book.getFiles():
 
114
            walker.walked.append(('', filename))
 
115
    else:
 
116
        walker.walkdir(opt['docsdir'] or '.', opt['prefixurl'])
 
117
 
 
118
    if opt['index']:
 
119
        indexFilename = opt['index']
 
120
    elif book:
 
121
        indexFilename = book.getIndexFilename()
 
122
    else:
 
123
        indexFilename = None
 
124
 
 
125
    if indexFilename:
 
126
        indexer.setIndexFilename("%s.%s" % (indexFilename, opt['output']))
 
127
    else:
 
128
        indexer.setIndexFilename(None)
 
129
 
 
130
    ## TODO: get numberSections from book, if any
 
131
    numberer.setNumberSections(opt['number'])
 
132
 
 
133
    walker.generate()
 
134
 
 
135
    if walker.failures:
 
136
        for (file, errors) in walker.failures:
 
137
            for error in errors:
 
138
                print "%s:%s" % (file, error)
 
139
        return 'Walker failures'
 
140
 
 
141
 
 
142
def run():
 
143
    opt = Options()
 
144
    try:
 
145
        opt.parseOptions()
 
146
    except usage.UsageError, errortext:
 
147
        print '%s: %s' % (sys.argv[0], errortext)
 
148
        print '%s: Try --help for usage details.' % sys.argv[0]
 
149
        sys.exit(1)
 
150
 
 
151
    result = runGivenOptions(opt)
 
152
    if result:
 
153
        print result
 
154
        sys.exit(1)
 
155
 
 
156
 
 
157
if __name__ == '__main__':
 
158
    run()
 
159