3 # Module for handling SCons documentation processing.
11 def __init__(self, name):
13 self.sort_name = name.lower()
14 if self.sort_name[0] == '_':
15 self.sort_name = self.sort_name[1:]
18 def cmp_name(self, name):
22 def __cmp__(self, other):
23 return cmp(self.sort_name, other.sort_name)
29 def __init__(self, name):
30 Item.__init__(self, name)
31 self.entity = self.name.replace('+', 'X')
33 class ConstructionVariable(Item):
37 def __init__(self, tag, body=None):
43 body = ''.join(self.body)
44 return "<%s>%s</%s>\n" % (self.tag, body, self.tag)
45 def append(self, data):
46 self.body.append(data)
52 def append(self, data):
53 self.collect.append(data)
55 text = ''.join(self.collect)
56 paras = text.split('\n\n')
60 self.body.append('\n')
62 paras[0] = '\n' + paras[0]
65 paras[-1] = paras[-1] + '\n'
77 self.body.append(last)
78 def begin_chunk(self, chunk):
82 self.body.append(self.collect)
85 class SConsDocHandler(xml.sax.handler.ContentHandler,
86 xml.sax.handler.ErrorHandler):
88 self._start_dispatch = {}
89 self._end_dispatch = {}
90 keys = self.__class__.__dict__.keys()
91 start_tag_method_names = filter(lambda k: k[:6] == 'start_', keys)
92 end_tag_method_names = filter(lambda k: k[:4] == 'end_', keys)
93 for method_name in start_tag_method_names:
95 self._start_dispatch[tag] = getattr(self, method_name)
96 for method_name in end_tag_method_names:
98 self._end_dispatch[tag] = getattr(self, method_name)
101 self.current_object = []
106 def startElement(self, name, attrs):
108 start_element_method = self._start_dispatch[name]
110 self.characters('<%s>' % name)
112 start_element_method(attrs)
114 def endElement(self, name):
116 end_element_method = self._end_dispatch[name]
118 self.characters('</%s>' % name)
124 def characters(self, chars):
125 self.collect.append(chars)
127 def begin_collecting(self, chunk):
129 def end_collecting(self):
132 def begin_chunk(self):
141 def begin_xxx(self, obj):
142 self.stack.append(self.current_object)
143 self.current_object = obj
145 self.current_object = self.stack.pop()
150 def start_scons_doc(self, attrs):
152 def end_scons_doc(self):
155 def start_builder(self, attrs):
156 name = attrs.get('name')
158 builder = self.builders[name]
160 builder = Builder(name)
161 self.builders[name] = builder
162 self.begin_xxx(builder)
163 def end_builder(self):
166 def start_tool(self, attrs):
167 name = attrs.get('name')
169 tool = self.tools[name]
172 self.tools[name] = tool
177 def start_cvar(self, attrs):
178 name = attrs.get('name')
180 cvar = self.cvars[name]
182 cvar = ConstructionVariable(name)
183 self.cvars[name] = cvar
188 def start_summary(self, attrs):
190 self.current_object.summary = summary
191 self.begin_xxx(summary)
192 self.begin_collecting(summary)
193 def end_summary(self):
194 self.current_object.end_para()
197 def start_example(self, attrs):
198 example = Chunk("programlisting")
199 self.current_object.begin_chunk(example)
200 def end_example(self):
201 self.current_object.end_chunk()
203 def start_uses(self, attrs):
204 self.begin_collecting([])
206 self.current_object.uses = ''.join(self.collect).split()
207 self.end_collecting()
209 # Stuff for the ErrorHandler portion.
210 def error(self, exception):
211 linenum = exception._linenum - self.preamble_lines
212 sys.stderr.write('%s:%d:%d: %s (error)\n' % (self.filename, linenum, exception._colnum, ''.join(exception.args)))
214 def fatalError(self, exception):
215 linenum = exception._linenum - self.preamble_lines
216 sys.stderr.write('%s:%d:%d: %s (fatalError)\n' % (self.filename, linenum, exception._colnum, ''.join(exception.args)))
218 def set_file_info(self, filename, preamble_lines):
219 self.filename = filename
220 self.preamble_lines = preamble_lines
222 # lifted from Ka-Ping Yee's way cool pydoc module.
223 def importfile(path):
224 """Import a Python source file or compiled file given its path."""
225 magic = imp.get_magic()
226 file = open(path, 'r')
227 if file.read(len(magic)) == magic:
228 kind = imp.PY_COMPILED
232 filename = os.path.basename(path)
233 name, ext = os.path.splitext(filename)
234 file = open(path, 'r')
236 module = imp.load_module(name, file, path, (ext, 'r', kind))
237 except ImportError, e:
238 sys.stderr.write("Could not import %s: %s\n" % (path, e))