source: CMT/v1r18p20060107/doc/gendoc.py@ 148

Last change on this file since 148 was 105, checked in by garonne, 20 years ago

add the pyhton check version

  • Property svn:eol-style set to native
File size: 18.5 KB
Line 
1#!/usr/bin/env python
2
3#-----------------------------------------------------------
4# Copyright Christian Arnault LAL-Orsay CNRS
5# author: arnault@lal.in2p3.fr
6# modified by: garonne@lal.in2p3.fr
7# See the complete license in cmt_license.txt "http://www.cecill.info".
8#-----------------------------------------------------------
9
10import sys, os, string, getopt, types
11import xml.parsers.expat
12import commands, glob
13from datetime import date
14
15
16#------------------------------------------------------------
17def checkPythonVersion (major=1,minor=5):
18 version = float(str(major) +'.'+ str(minor))
19 thisversion = float(str(sys.version_info[0]) + '.' + str(sys.version_info[1]))
20 if version > thisversion:
21 # try to find the good python version >= 2.3
22 paths = string.split(os.environ['PATH'],':')
23 print 'hmmm look for the appropriate python version, this one is',\
24 thisversion,'and the required version is python >=', version
25 pythons = list()
26 for path in paths:
27 pythons = glob.glob1(path, 'python*')
28 for python in pythons:
29 cmd = python +' -c "import sys; print sys.version_info"'
30 status, thisversion = commands.getstatusoutput(cmd)
31 thisversion = eval (thisversion)
32 thisversion = float(str(thisversion[0]) +'.'+ str(thisversion[1]))
33 if (thisversion >= version):
34 print 'found and load', python
35 sys.argv.insert (0, python)
36 os.execvp (python, sys.argv)
37 print 'The appropriate version of python >=' + version + ' is not here, check your PATH variable or install it'
38 sys.exit(-1)
39
40
41#------------------------------------------------------------
42# A Section holds
43# an id of the form n.m.p
44# a title
45# the level
46#
47class Section :
48 def __init__ (self, id, title, level) :
49 self.id = id
50 self.title = title
51 self.level = level
52 # end def
53
54 # Format the index entry for this section
55 def show_index_entry (self) :
56 tab = string.rjust ("", 4*self.level)
57 tab = tab.replace (" ", " ")
58 print '<tr>'
59 print '<td width="100">' + self.id + '</td>'
60 print '<td>' + tab + '<a href="#' + self.title + '">' + self.title + '</a></td>'
61 print '</tr>'
62 # end def
63
64#------------------------------------------------------------
65# A Book
66# It holds the history of sections and the definitions of internal entities
67#
68class Book :
69 # Format one level of a section id as %2.2d
70 def format_id (self, id) :
71 r = repr (id)
72 if id < 10 :
73 r = '&nbsp;' + r
74 return r
75 # end def
76
77 # Format a complete section id, with all individual level ids
78 # Currently the implementation is somewhat ugly. This is due
79 # to the strange behaviour of the ideal form (using a for-loop
80 # over the elements of self.level_ids). Once we can understand
81 # the reason of this unexpected behaviour we can improve it.
82 def build_id (self) :
83 id = '<tt>'
84 n = len (self.level_ids)
85 if n >= 1 :
86 id += self.format_id (self.level_ids[0])
87 if n >= 2 :
88 id += '.' + self.format_id (self.level_ids[1])
89 if n >= 3 :
90 id += '.' + self.format_id (self.level_ids[2])
91 if n >= 4 :
92 id += '.' + self.format_id (self.level_ids[3])
93 id += '</tt>'
94 return id
95 # end def
96
97 # Create a new Section object
98 # Register the history of sections (with level hierarchy)
99 def open_section (self, name) :
100 self.level_ids [self.level] += 1
101 i = self.level_ids [self.level]
102 id = self.build_id ()
103 self.sections.append (Section (id, name, self.level))
104 self.level += 1
105 self.level_ids.append (0)
106 return id
107 # end def
108
109 # Pop one level in the level hierarchy
110 def close_section (self) :
111 self.level_ids.pop ()
112 self.level -= 1
113 # end def
114
115 # Register the definition of an internal entity in the entity dictionary
116 def entity_decl (self, name, is_parameter, value, base, systemId, publicId, notation) :
117 if value :
118 self.entities['&' + name + ';'] = value
119 # end def
120
121 # Generic parser
122 def generic_parse (self, p, name) :
123 p.StartElementHandler = self.start_element
124 p.EndElementHandler = self.end_element
125 p.CharacterDataHandler = self.char_data
126 p.ExternalEntityRefHandler = self.external_entity
127 p.DefaultHandler = self.dummy
128 p.EntityDeclHandler = self.entity_decl
129 #file_name = os.path.join (sys.path[0], name)
130 f = open (name)
131 p.ParseFile (f)
132 f.close ()
133 # end def
134
135 # Top level parser
136 def parse (self, name) :
137 self.p = xml.parsers.expat.ParserCreate ()
138 self.generic_parse (self.p, name)
139 # end def
140
141 # Sub-parser for external entities
142 def external_entity (self, context, base, system_id, public_id):
143 #print "external entity " + repr(context) + " " + repr(system_id) + ' ' + sys.path[0]
144 ep = self.p.ExternalEntityParserCreate (context)
145 self.generic_parse (ep, system_id)
146 # end def
147
148 # Format a tabulation according to the stored tabulation level
149 def tab (self) :
150 return (string.rjust ("", self.tabulation))
151 # end def
152
153 # Flush the internal line buffer and display it using the tabulation
154 def purge (self) :
155 if self.line != '':
156 if self.in_code == 0 :
157 print self.tab () + self.line
158 self.line = ''
159 # end def
160
161
162 #----------------------------------------------------------
163 #
164 # All XML element handlers
165 #
166 #----------------------------------------------------------
167
168
169 # any element : simply reproduce the element with its attributes
170 # (ie assume it is a true HTML element)
171 def default_start (self, name, attrs) :
172 self.line += '<' + name
173 if len (attrs) > 0 :
174 i = len (attrs)
175 for k, v in attrs.items() :
176 self.line += ' ' + k + '="' + v + '"'
177 i -= 1
178 if i <= 0 : break
179 self.line += '>'
180 ##### self.purge ()
181 self.tabulation += 2
182 # end def
183
184
185 def default_end (self, name) :
186 self.tabulation -= 2
187 self.line += '</' + name + '>'
188 self.purge ()
189 # end def
190
191
192 # <book>
193 def book_start (self, attrs) :
194 self.name = attrs['name']
195 self.title = attrs['title']
196 self.version = attrs['version']
197 self.author = attrs['author']
198 print '<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> '
199 print '<html>'
200 print '<title>' + self.name + '</title>'
201 print '<head>'
202 print ' <style type="text/css">'
203 print ' body {font-face: Arial, Helvetica, Comic Sans MS, Times; color: #000000}'
204 ##print ' tt {color:#006600; font-weight: normal; background-color: #eeffee}'
205 print ' tt {color:#00AA00; font-weight: normal; background-color: #eeeeee}'
206 print ' pre {color:#FF0000; background-color: #eeeeee; border-style: solid; border-width: 1; border-color: black; padding: 4}'
207 print ' pre.cmt {font-family: courier; color:#00AA00; background-color: #eeeeee; border-style: solid; border-width: 1; border-color: black; padding: 4}'
208 print ' h2 {color:#0000FF}'
209 print ' h3 {color:#00AA00}'
210 print ' h4 {color:#997700}'
211 print ' a {color:#0000FF; background-color: LightGoldenRodYellow; text-decoration: none}'
212 print ' b {font-family: courier; color:#006600; font-weight: normal; %background-color: #eeffee}'
213 print ' td.rule {padding-top: 10}'
214 print ' </style>'
215 print '</head>'
216 print '<body bgcolor="#ffffff" link="#550088" alink="#007777" alink="#007777">'
217 ##print '<font face="Arial, Helvetica" color="#000000">'
218 ##print '<font face="Arial, Helvetica, Comic Sans MS, Times" color="#000000">'
219 print '<h1><center>' + self.name + '</center>'
220 print '<center>' + self.title + '</center></h1>'
221 print '<h2><center>Version ' + self.version + '</center>'
222 print '<center>' + self.author + '</center>'
223 print '<center><tt>' + attrs['email'] + '</tt></center></h2>'
224 self.line += '<center><i>Document revision date : ' + date.today().isoformat() + '</i></center>'
225 self.line += '<hr><h2>'
226 self.line += '<a href="#index">General index</a>'
227 self.line += '</h2>'
228 self.purge ()
229 # end def
230
231 def book_end (self) :
232 print '<hr><h1><A NAME="index"></A>Contents</h1>'
233 print '<blockquote>'
234 print '<table cols="2">'
235 for k in range (len (self.sections)) :
236 self.sections[k].show_index_entry ()
237 print '<tr><td colspan="2">&nbsp;</td></tr>'
238 print '<tr><td colspan="2"><h2>Images</h2></td></tr>'
239 for k in range (len (self.images)) :
240 print '<tr>'
241 print '<td width="100"><tt>' + repr(k+1) + '</tt></td>'
242 print '<td><a href="#' + self.images[k] + '">' + self.images[k] + '</a></td>'
243 print '</tr>'
244 print '</table>'
245 print '</blockquote>'
246 ##print '</font>'
247 print '<address>'
248 print '<i>' + self.author + '</i>'
249 print '</address>'
250 print '</body>'
251 print '</html>'
252 # end def
253
254
255 # <section>
256 def section_start (self, attrs) :
257 title = attrs['title']
258 title = title.replace ('>', '&gt;')
259 title = title.replace ('<', '&lt;')
260 id = self.open_section (title)
261 h_level = repr(self.level+1)
262 self.line += '<hr><h' + h_level + '>'
263 self.line += '<a name="' + title + '"></a>'
264 self.line += '<a href="#index">' + id + '</a> - ' + title
265 self.line += '</h' + h_level + '>'
266 self.purge ()
267 self.line = '<blockquote>'
268 self.purge ()
269 # end def
270
271 def section_end (self) :
272 self.purge ()
273 self.line = '</blockquote>'
274 self.purge ()
275 self.close_section ()
276 # end def
277
278
279 # <code>
280 def code_start (self, attrs) :
281 self.purge ()
282 self.in_code += 1
283 self.line = '<pre>'
284 # end def
285
286 def code_end (self) :
287 self.in_code -= 1
288 print self.line + '</pre>'
289 self.line = ''
290 # end def
291
292
293 # <cmtcode>
294 def cmtcode_start (self, attrs) :
295 self.purge ()
296 self.in_code += 1
297 self.line = '<pre class="cmt">'
298 # end def
299
300 def cmtcode_end (self) :
301 self.in_code -= 1
302 print self.line + '</pre>'
303 self.line = ''
304 # end def
305
306
307 # <syntax>
308 def syntax_start (self, attrs) :
309 print '<center>'
310 print '<table cols="3">'
311 print '<tr>'
312 if 'rule-width' in attrs :
313 print '<td width="' + attrs['rule-width'] + '"></td>'
314 else :
315 print '<td></td>'
316 if 'name' in attrs :
317 self.ruleref_prefix = 'kw' + attrs['name'] + '-'
318 else :
319 self.ruleref_prefix = 'kw-'
320 print '<td width="10"></td>'
321 print '<td></td>'
322 print '</tr>'
323 # end def
324
325 def syntax_end (self) :
326 print '</table>'
327 print '</center>'
328 # end def
329
330
331 # <rule>
332 def rule_start (self, attrs) :
333 self.rule_name = attrs['name']
334 self.rule_started = 0
335 # end def
336
337 def rule_end (self) :
338 self.rule_name = ''
339 self.rule_started = 0
340 # end def
341
342
343 # <alt>
344 def alt_start (self, attrs) :
345 print '<tr>'
346 if self.rule_started == 0 :
347 self.rule_started = 1
348 print '<td class="rule"><font face="courier new, courier" COLOR="#770000"><i><a name="' + self.ruleref_prefix + self.rule_name + '"></a>' + self.rule_name + '</i></font></td>'
349 print '<td class="rule">:</td>'
350 print '<td class="rule">'
351 else :
352 print '<td></td>'
353 print '<td>|</td>'
354 print '<td>'
355 # end def
356
357 def alt_end (self) :
358 print '</td>'
359 print '</tr>'
360 # end def
361
362
363 # <continuation>
364 def continuation_start (self, attrs) :
365 print '<tr>'
366 print '<td></td>'
367 print '<td></td>'
368 print '<td>&nbsp;&nbsp;&nbsp;'
369 # end def
370
371 def continuation_end (self) :
372 print '</td>'
373 print '</tr>'
374 # end def
375
376
377 # <kwd>
378 def kwd_start (self, attrs) :
379 print '<font face="courier new, courier" COLOR="#FF0000">'
380 if 'name' in attrs :
381 name = attrs['name']
382 else :
383 name = self.rule_name
384 if 'value' in attrs :
385 name += '='
386 value = '<font face="courier new, courier" COLOR="#770000"><i>'
387 value += attrs['value']
388 value += '</i></font>'
389 else :
390 value = ''
391 print name + value
392 # end def
393
394 def kwd_end (self) :
395 print '</font>'
396 # end def
397
398
399
400 # <term>
401 def term_start (self, attrs) :
402 print '<font face="courier new, courier" COLOR="#770000"><i>'
403 print attrs['name']
404 # end def
405
406 def term_end (self) :
407 print '</i></font>'
408 # end def
409
410
411
412 # <ruleref>
413 def ruleref_start (self, attrs) :
414 print '<font face="courier new, courier" COLOR="#770000"><i>'
415 print '<a href="#' + self.ruleref_prefix + attrs['name'] + '">' + attrs['name'] + '</a>'
416 # end def
417
418 def ruleref_end (self) :
419 print '</i></font>'
420 # end def
421
422
423 # <option>
424 def option_start (self, attrs) :
425 print '[&nbsp;'
426 # end def
427
428 def option_end (self) :
429 print '&nbsp;]&nbsp;'
430 # end def
431
432
433 # <seq>
434 def seq_start (self, attrs) :
435 i=0
436 # end def
437
438 def seq_end (self) :
439 print '&nbsp;...&nbsp;'
440 # end def
441
442
443 # <optionseq>
444 def optionseq_start (self, attrs) :
445 print '[&nbsp;'
446 # end def
447
448 def optionseq_end (self) :
449 print '&nbsp;...&nbsp;'
450 print '&nbsp;]&nbsp;'
451 # end def
452
453
454
455 # <image>
456 def image_start (self, attrs) :
457 caption = attrs['caption']
458 self.images.append (caption)
459 n = len (self.images)
460 print '<center>'
461 print '<a name="' + caption + '"></a>'
462 print '<img src="' + attrs['src'] + '"/>'
463 print '</center>'
464 print '<center>'
465 print '<tt>' + repr(n) + '</tt> - <i>' + caption + '</i>'
466 # end def
467
468 def image_end (self) :
469 print '</center>'
470 # end def
471
472 def blockquote_start (self, attrs) :
473 print '<blockquote><hr>'
474 # end def
475
476 def blockquote_end (self) :
477 print '<hr></blockquote>'
478 # end def
479
480
481 # Basic element parsing handler
482 def start_element (self, name, attrs):
483 #print 'start_element: name=' + repr(name)
484 try :
485 if self.start_handlers.has_key (name) :
486 self.start_handlers [name] (attrs)
487 else :
488 self.default_start (name, attrs)
489 except TypeError:
490 print 'start_element: (error) name=' + repr(name)
491 self.default_start (name, attrs)
492 # end def
493
494 def end_element (self, name) :
495 #print 'end_element: name=' + repr(name)
496 try :
497 if self.end_handlers.has_key (name) :
498 self.end_handlers [name] ()
499 else :
500 self.default_end (name)
501 except TypeError:
502 #print 'end_element: (error) name=' + repr(name)
503 if name == u'cmt:book' :
504 self.book_end ()
505 else :
506 self.default_end (name)
507 # end def
508
509 # Unhandled elements will be trapped here
510 def dummy (self, data) :
511 if self.entities.has_key (data) :
512 self.char_data (self.entities[data])
513 #print "dummy:[" + repr (data) + "]" + repr (type(data))
514 # end def
515
516 # CDATA handling inside code sections
517 def code_char_data (self, data) :
518 if data == u'\n' :
519 self.line += data
520 else :
521 n = len (data)
522 #
523 if n > 0 :
524 if data == u'<':
525 self.line += '&lt;'
526 elif data == u'>' :
527 self.line += '&gt;'
528 else :
529 self.line += data
530 # end def
531
532 # CDATA handling outside code sections
533 def plain_char_data (self, data) :
534 if data == u'\n' :
535 self.purge ()
536 else :
537 n = len (string.strip (data))
538 #
539 if n > 0 :
540 if data == u'<':
541 self.line += '&lt;'
542 elif data == u'>' :
543 self.line += '&gt;'
544 else :
545 # self.line += string.strip (data)
546 self.line += data
547 # end def
548
549 # CDATA handling
550 def char_data (self, data) :
551 #print '[' + repr(data) + ']' + repr (type (data)) + ' ' + repr(len (string.strip (data)))
552 if self.in_code > 0 :
553 self.code_char_data (data)
554 else :
555 self.plain_char_data (data)
556 # end def
557
558 def __init__ (self) :
559 self.line = ''
560 self.tabulation = 0
561 self.in_code = 0
562 self.sections = []
563 self.level_ids = [0]
564 self.level = 0
565 self.images = []
566 self.entities = {}
567 #
568 self.start_handlers = {}
569 self.start_handlers['cmt:code'] = self.code_start
570 self.start_handlers['cmt:cmtcode'] = self.cmtcode_start
571 self.start_handlers['cmt:section'] = self.section_start
572 self.start_handlers['cmt:book'] = self.book_start
573 self.start_handlers['cmt:syntax'] = self.syntax_start
574 self.start_handlers['cmt:rule'] = self.rule_start
575 self.start_handlers['cmt:alt'] = self.alt_start
576 self.start_handlers['cmt:continuation'] = self.continuation_start
577 self.start_handlers['cmt:kwd'] = self.kwd_start
578 self.start_handlers['cmt:term'] = self.term_start
579 self.start_handlers['cmt:ruleref'] = self.ruleref_start
580 self.start_handlers['cmt:option'] = self.option_start
581 self.start_handlers['cmt:seq'] = self.seq_start
582 self.start_handlers['cmt:optionseq'] = self.optionseq_start
583 self.start_handlers['cmt:image'] = self.image_start
584 self.start_handlers['cmt:blockquote'] = self.blockquote_start
585 #
586 self.end_handlers = {}
587 self.end_handlers['cmt:code'] = self.code_end
588 self.end_handlers['cmt:cmtcode'] = self.cmtcode_end
589 self.end_handlers['cmt:section'] = self.section_end
590 self.end_handlers['cmt:book'] = self.book_end
591 self.end_handlers['cmt:syntax'] = self.syntax_end
592 self.end_handlers['cmt:rule'] = self.rule_end
593 self.end_handlers['cmt:alt'] = self.alt_end
594 self.end_handlers['cmt:continuation'] = self.continuation_end
595 self.end_handlers['cmt:kwd'] = self.kwd_end
596 self.end_handlers['cmt:term'] = self.term_end
597 self.end_handlers['cmt:ruleref'] = self.ruleref_end
598 self.end_handlers['cmt:option'] = self.option_end
599 self.end_handlers['cmt:seq'] = self.seq_end
600 self.end_handlers['cmt:optionseq'] = self.optionseq_end
601 self.end_handlers['cmt:image'] = self.image_end
602 self.end_handlers['cmt:blockquote'] = self.blockquote_end
603 # end def
604
605
606
607#----------------------------------------------------------------------------------
608
609#----------------------------------------------------------------------------------
610#
611# Various top level functions
612#
613#----------------------------------------------------------------------------------
614def usage() :
615 print 'Usage:'
616 print ' gendoc'
617 print 'Try "gendoc --help" for more information.'
618 sys.exit()
619# end def
620
621#----------------------------------------------------------------------------------
622def help() :
623 print "Generates the HTML documentation from Xml"
624# end def
625
626#----------------------------------------------------------------------------------
627def main() :
628 file_name = ''
629 options = []
630 for a in sys.argv[1:] :
631 if a[0] == '-' :
632 options = sys.argv[sys.argv.index(a):]
633 break
634 else :
635 file_name = a
636 try:
637 opts, args = getopt.getopt(options, 'h', ['help'])
638 except getopt.GetoptError:
639 usage()
640 sys.exit(2)
641 for o, a in opts:
642 if o in ('-h', '--help'):
643 help()
644 sys.exit()
645 book = Book ()
646 book.parse (file_name)
647# end def
648
649#---------------------------------------------------------------------
650#print '__name__ = ' + __name__
651if __name__ == "__main__":
652 checkPythonVersion (major=2, minor=3)
653 main()
654# end def
655
Note: See TracBrowser for help on using the repository browser.