root / trunk / twisted / lore / scripts / lore.py

Revision 25788, 4.6 kB (checked in by exarkun, 6 months ago)

Merge remove-old-plugins-1911

Author: exarkun
Reviewer: therve
Fixes: #1911

Remove the old Twisted plugin API and adjust the two remaining users of it so that
they only use the new Twisted plugin API.

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