#!/usr/bin/env python
#-----------------------------------------------------------
# Copyright Christian Arnault LAL-Orsay CNRS
# author: arnault@lal.in2p3.fr
# modified by: garonne@lal.in2p3.fr
# See the complete license in cmt_license.txt "http://www.cecill.info".
#-----------------------------------------------------------
import sys, os, string, getopt, types
import xml.parsers.expat
import commands, glob
from datetime import date
#------------------------------------------------------------
def checkPythonVersion (major=1,minor=5):
version = float(str(major) +'.'+ str(minor))
thisversion = float(str(sys.version_info[0]) + '.' + str(sys.version_info[1]))
if version > thisversion:
# try to find the good python version >= 2.3
paths = string.split(os.environ['PATH'],':')
print 'hmmm look for the appropriate python version, this one is',\
thisversion,'and the required version is python >=', version
pythons = list()
for path in paths:
pythons = glob.glob1(path, 'python*')
for python in pythons:
cmd = python +' -c "import sys; print sys.version_info"'
status, thisversion = commands.getstatusoutput(cmd)
thisversion = eval (thisversion)
thisversion = float(str(thisversion[0]) +'.'+ str(thisversion[1]))
if (thisversion >= version):
print 'found and load', python
sys.argv.insert (0, python)
os.execvp (python, sys.argv)
print 'The appropriate version of python >=' + version + ' is not here, check your PATH variable or install it'
sys.exit(-1)
#------------------------------------------------------------
# A Section holds
# an id of the form n.m.p
# a title
# the level
#
class Section :
def __init__ (self, id, title, level) :
self.id = id
self.title = title
self.level = level
# end def
# Format the index entry for this section
def show_index_entry (self) :
tab = string.rjust ("", 4*self.level)
tab = tab.replace (" ", " ")
print '
'
# end def
#------------------------------------------------------------
# A Book
# It holds the history of sections and the definitions of internal entities
#
class Book :
# Format one level of a section id as %2.2d
def format_id (self, id) :
r = repr (id)
if id < 10 :
r = ' ' + r
return r
# end def
# Format a complete section id, with all individual level ids
# Currently the implementation is somewhat ugly. This is due
# to the strange behaviour of the ideal form (using a for-loop
# over the elements of self.level_ids). Once we can understand
# the reason of this unexpected behaviour we can improve it.
def build_id (self) :
id = ''
n = len (self.level_ids)
if n >= 1 :
id += self.format_id (self.level_ids[0])
if n >= 2 :
id += '.' + self.format_id (self.level_ids[1])
if n >= 3 :
id += '.' + self.format_id (self.level_ids[2])
if n >= 4 :
id += '.' + self.format_id (self.level_ids[3])
id += ''
return id
# end def
# Create a new Section object
# Register the history of sections (with level hierarchy)
def open_section (self, name) :
self.level_ids [self.level] += 1
i = self.level_ids [self.level]
id = self.build_id ()
self.sections.append (Section (id, name, self.level))
self.level += 1
self.level_ids.append (0)
return id
# end def
# Pop one level in the level hierarchy
def close_section (self) :
self.level_ids.pop ()
self.level -= 1
# end def
# Register the definition of an internal entity in the entity dictionary
def entity_decl (self, name, is_parameter, value, base, systemId, publicId, notation) :
if value :
self.entities['&' + name + ';'] = value
# end def
# Generic parser
def generic_parse (self, p, name) :
p.StartElementHandler = self.start_element
p.EndElementHandler = self.end_element
p.CharacterDataHandler = self.char_data
p.ExternalEntityRefHandler = self.external_entity
p.DefaultHandler = self.dummy
p.EntityDeclHandler = self.entity_decl
#file_name = os.path.join (sys.path[0], name)
f = open (name)
p.ParseFile (f)
f.close ()
# end def
# Top level parser
def parse (self, name) :
self.p = xml.parsers.expat.ParserCreate ()
self.generic_parse (self.p, name)
# end def
# Sub-parser for external entities
def external_entity (self, context, base, system_id, public_id):
#print "external entity " + repr(context) + " " + repr(system_id) + ' ' + sys.path[0]
ep = self.p.ExternalEntityParserCreate (context)
self.generic_parse (ep, system_id)
# end def
# Format a tabulation according to the stored tabulation level
def tab (self) :
return (string.rjust ("", self.tabulation))
# end def
# Flush the internal line buffer and display it using the tabulation
def purge (self) :
if self.line != '':
if self.in_code == 0 :
print self.tab () + self.line
self.line = ''
# end def
#----------------------------------------------------------
#
# All XML element handlers
#
#----------------------------------------------------------
# any element : simply reproduce the element with its attributes
# (ie assume it is a true HTML element)
def default_start (self, name, attrs) :
self.line += '<' + name
if len (attrs) > 0 :
i = len (attrs)
for k, v in attrs.items() :
self.line += ' ' + k + '="' + v + '"'
i -= 1
if i <= 0 : break
self.line += '>'
##### self.purge ()
self.tabulation += 2
# end def
def default_end (self, name) :
self.tabulation -= 2
self.line += '' + name + '>'
self.purge ()
# end def
#
def book_start (self, attrs) :
self.name = attrs['name']
self.title = attrs['title']
self.version = attrs['version']
self.author = attrs['author']
print ' '
print ''
print '' + self.name + ''
print ''
print ' '
print ''
print ''
##print ''
##print ''
print '
' + self.name + '
'
print '
' + self.title + '
'
print '
Version ' + self.version + '
'
print '
' + self.author + '
'
print '
' + attrs['email'] + '
'
self.line += '
Document revision date : ' + date.today().isoformat() + '