CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/Utilities/ReleaseScripts/python/cmsCodeRules/Formatter.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 import os, sys, string
00004 
00005 from xml.parsers import expat
00006 
00007 # ================================================================================
00008 
00009 class SimpleAsciiFormatter :
00010 
00011     def __init__(self, title="CMS SDT pages", style=None, outFile=sys.stdout) :
00012         self.format = ""
00013         self.title = title
00014         return
00015     
00016     def showLine(self) :
00017         print "\n================================================================================\n"
00018         return
00019     
00020     def write(self, arg="") :
00021         print arg
00022         return
00023 
00024     def writeB(self, arg="") :
00025         print arg
00026         return
00027 
00028     def writeBr(self) :
00029         print 
00030         return
00031 
00032     def writeH1(self, arg="") :
00033         print arg
00034         return
00035     
00036     def writeH2(self, arg="") :
00037         print arg
00038         return
00039 
00040     def writeH3(self, arg="") :
00041         print arg
00042         return
00043     
00044     def startTable(self, colSizes, colLabels) :
00045         self.cols = colSizes
00046         self.format = ""
00047         for fmt in self.cols :
00048             self.format += "%"+str(fmt)+"s "
00049         print self.format % tuple(colLabels)
00050 
00051         return
00052 
00053     def writeRow(self, args) :
00054         print self.format % tuple(args)
00055 
00056         return
00057 
00058     def endTable(self) :
00059         self.format = None
00060         self.cols   = None
00061         
00062     
00063 # ================================================================================
00064 
00065 class SimpleHTMLFormatter :
00066 
00067     def __init__(self, title="CMS SDT pages ", style=None, outFile=sys.stdout, scriptCode='') :
00068         import time
00069 
00070         self.headersDone = False
00071         self.title=title
00072         self.style = style
00073         self.scriptCode = scriptCode
00074         self.outFile = outFile
00075         self.format = ""
00076 
00077         return
00078 
00079     def addScriptCode(self, scriptCode):
00080         self.scriptCode += scriptCode
00081 
00082     def __del__(self) :
00083         if self.headersDone :
00084             self.trailers()
00085         else: # in case nothing has been written yet, output an empty page ...
00086             self.outFile.write( "Content-Type: text/html" + '\n')     # HTML is following
00087             self.outFile.write( '\n')   # blank line, end of headers
00088             self.outFile.write( "<html> " + '\n')
00089             self.outFile.write( "</html> " + '\n')
00090         
00091         return
00092 
00093     def showLine(self) :
00094         self.headers()
00095         self.outFile.write( "<hr />" + '\n')
00096         return
00097     
00098     def write(self, arg="", bold=False) :
00099         self.headers()
00100         if bold:
00101             self.writeB(arg)
00102         else:
00103             self.outFile.write( arg + '\n')
00104         return
00105 
00106     def writeBr(self) :
00107         self.headers()
00108         self.outFile.write( "<br /> <br /> " + '\n')
00109         return
00110     
00111     def writeB(self, arg="") :
00112         self.headers()
00113         self.outFile.write( "<b> " + arg + " </b>" + '\n')
00114         return
00115     
00116     def writeH1(self, arg="") :
00117         self.headers()
00118         self.outFile.write( "<h1> " + arg + " </h1>" + '\n')
00119         return
00120     
00121     def writeH2(self, arg="") :
00122         self.headers()
00123         self.outFile.write( "<h2> " + arg + " </h2>" + '\n')
00124         return
00125 
00126     def writeH3(self, arg="") :
00127         self.headers()
00128         self.outFile.write( "<h3> " + arg + " </h3>" + '\n')
00129         return
00130     
00131     def writeAnchor(self, ref="") :
00132         self.headers()
00133         self.outFile.write( '<a name="' + ref + '">&nbsp;</a>')
00134         return
00135     
00136     def startTable(self, colSizes, colLabels, id=None, cls=None, tableAttr=None) :
00137         # we assume that html headers are done by now !!
00138         tableString = '<table '
00139         if tableAttr:
00140             tableString += tableAttr
00141         if id:
00142             tableString += ' id="'+id+'" '
00143         if cls:
00144             tableString += ' class="'+cls+'" '
00145         tableString += '>'
00146         self.outFile.write( tableString + '\n')
00147 
00148         self.outFile.write( " <thead>\n  <tr>"  + '\n')
00149         for col in colLabels :
00150             self.outFile.write( "   <th> <b>" + col + "</b> </th>" + '\n')
00151         self.outFile.write( "  </tr>\n</thead>" + '\n')
00152         self.outFile.write( "  <tbody>" + '\n')
00153         return
00154 
00155     def writeRow(self, args, bold=False, cls=None) :
00156         # we assume that headers are done by now !!
00157 
00158         if cls:
00159             self.outFile.write( ' <tr class="'+cls+'"> \n')
00160         else:
00161             self.outFile.write( " <tr>" + '\n')
00162         for arg in args:
00163             if string.strip(str(arg)) == "" : arg = "&nbsp;"
00164             if bold: self.outFile.write( '<td class=cellbold> ' )
00165             else:    self.outFile.write( "  <td> " )
00166             self.outFile.write( arg )
00167             
00168             self.outFile.write( " </td>" + '\n')
00169         self.outFile.write( " </tr> " + '\n')
00170 
00171         return
00172 
00173     def writeStyledRow(self, args, styles) :
00174         # we assume that headers are done by now !!
00175         self.outFile.write( " <tr>" + '\n')
00176         for arg, cellStyle in zip(args, styles):
00177             if string.strip(str(arg)) == "" : arg = "&nbsp;"
00178             cellStyle = cellStyle.strip()
00179             if cellStyle != '' : self.outFile.write( '<td class='+cellStyle+'> ' )
00180             else:    self.outFile.write( "  <td> " )
00181             self.outFile.write( arg )
00182             self.outFile.write( " </td>" + '\n')
00183         self.outFile.write( " </tr> " + '\n')
00184 
00185         return
00186 
00187     def endTable(self) :
00188         # we assume that headers are done by now !!
00189         self.outFile.write( "</tbody>" + '\n')
00190         self.outFile.write( "</table>" + '\n')
00191         
00192     # --------------------------------------------------------------------------------
00193     def headers(self) :
00194         # make sure to write the headers only once ...
00195         if not self.headersDone :
00196         
00197             self.outFile.write( "<html> " + '\n')
00198 
00199             self.outFile.write( "<head> " + '\n')
00200 
00201             if self.style:
00202                 self.outFile.write( self.style + '\n')
00203             
00204             self.outFile.write( "<TITLE>" + self.title + "</TITLE>" + '\n')
00205             if self.scriptCode:
00206                 self.outFile.write( self.scriptCode + '\n' )
00207             self.outFile.write( "</head> " + '\n')
00208             self.outFile.write( "<body>" + '\n')
00209         self.headersDone = True
00210 
00211         return
00212 
00213     # --------------------------------------------------------------------------------
00214     def trailers(self) :
00215         # write only if headers have been written ...
00216         if self.headersDone :
00217             self.outFile.write( "</body>" + '\n')
00218             self.outFile.write( "</html> " + '\n')
00219             pass
00220         return
00221 
00222