CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Formatter.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import os, sys, string
4 
5 from xml.parsers import expat
6 
7 # ================================================================================
8 
10 
11  def __init__(self, title="CMS SDT pages", style=None, outFile=sys.stdout) :
12  self.format = ""
13  self.title = title
14  return
15 
16  def showLine(self) :
17  print "\n================================================================================\n"
18  return
19 
20  def write(self, arg="") :
21  print arg
22  return
23 
24  def writeB(self, arg="") :
25  print arg
26  return
27 
28  def writeBr(self) :
29  print
30  return
31 
32  def writeH1(self, arg="") :
33  print arg
34  return
35 
36  def writeH2(self, arg="") :
37  print arg
38  return
39 
40  def writeH3(self, arg="") :
41  print arg
42  return
43 
44  def startTable(self, colSizes, colLabels) :
45  self.cols = colSizes
46  self.format = ""
47  for fmt in self.cols :
48  self.format += "%"+str(fmt)+"s "
49  print self.format % tuple(colLabels)
50 
51  return
52 
53  def writeRow(self, args) :
54  print self.format % tuple(args)
55 
56  return
57 
58  def endTable(self) :
59  self.format = None
60  self.cols = None
61 
62 
63 # ================================================================================
64 
66 
67  def __init__(self, title="CMS SDT pages ", style=None, outFile=sys.stdout, scriptCode='') :
68  import time
69 
70  self.headersDone = False
71  self.title=title
72  self.style = style
73  self.scriptCode = scriptCode
74  self.outFile = outFile
75  self.format = ""
76 
77  return
78 
79  def addScriptCode(self, scriptCode):
80  self.scriptCode += scriptCode
81 
82  def __del__(self) :
83  if self.headersDone :
84  self.trailers()
85  else: # in case nothing has been written yet, output an empty page ...
86  self.outFile.write( "Content-Type: text/html" + '\n') # HTML is following
87  self.outFile.write( '\n') # blank line, end of headers
88  self.outFile.write( "<html> " + '\n')
89  self.outFile.write( "</html> " + '\n')
90 
91  return
92 
93  def showLine(self) :
94  self.headers()
95  self.outFile.write( "<hr />" + '\n')
96  return
97 
98  def write(self, arg="", bold=False) :
99  self.headers()
100  if bold:
101  self.writeB(arg)
102  else:
103  self.outFile.write( arg + '\n')
104  return
105 
106  def writeBr(self) :
107  self.headers()
108  self.outFile.write( "<br /> <br /> " + '\n')
109  return
110 
111  def writeB(self, arg="") :
112  self.headers()
113  self.outFile.write( "<b> " + arg + " </b>" + '\n')
114  return
115 
116  def writeH1(self, arg="") :
117  self.headers()
118  self.outFile.write( "<h1> " + arg + " </h1>" + '\n')
119  return
120 
121  def writeH2(self, arg="") :
122  self.headers()
123  self.outFile.write( "<h2> " + arg + " </h2>" + '\n')
124  return
125 
126  def writeH3(self, arg="") :
127  self.headers()
128  self.outFile.write( "<h3> " + arg + " </h3>" + '\n')
129  return
130 
131  def writeAnchor(self, ref="") :
132  self.headers()
133  self.outFile.write( '<a name="' + ref + '">&nbsp;</a>')
134  return
135 
136  def startTable(self, colSizes, colLabels, id=None, cls=None, tableAttr=None) :
137  # we assume that html headers are done by now !!
138  tableString = '<table '
139  if tableAttr:
140  tableString += tableAttr
141  if id:
142  tableString += ' id="'+id+'" '
143  if cls:
144  tableString += ' class="'+cls+'" '
145  tableString += '>'
146  self.outFile.write( tableString + '\n')
147 
148  self.outFile.write( " <thead>\n <tr>" + '\n')
149  for col in colLabels :
150  self.outFile.write( " <th> <b>" + col + "</b> </th>" + '\n')
151  self.outFile.write( " </tr>\n</thead>" + '\n')
152  self.outFile.write( " <tbody>" + '\n')
153  return
154 
155  def writeRow(self, args, bold=False, cls=None) :
156  # we assume that headers are done by now !!
157 
158  if cls:
159  self.outFile.write( ' <tr class="'+cls+'"> \n')
160  else:
161  self.outFile.write( " <tr>" + '\n')
162  for arg in args:
163  if string.strip(str(arg)) == "" : arg = "&nbsp;"
164  if bold: self.outFile.write( '<td class=cellbold> ' )
165  else: self.outFile.write( " <td> " )
166  self.outFile.write( arg )
167 
168  self.outFile.write( " </td>" + '\n')
169  self.outFile.write( " </tr> " + '\n')
170 
171  return
172 
173  def writeStyledRow(self, args, styles) :
174  # we assume that headers are done by now !!
175  self.outFile.write( " <tr>" + '\n')
176  for arg, cellStyle in zip(args, styles):
177  if string.strip(str(arg)) == "" : arg = "&nbsp;"
178  cellStyle = cellStyle.strip()
179  if cellStyle != '' : self.outFile.write( '<td class='+cellStyle+'> ' )
180  else: self.outFile.write( " <td> " )
181  self.outFile.write( arg )
182  self.outFile.write( " </td>" + '\n')
183  self.outFile.write( " </tr> " + '\n')
184 
185  return
186 
187  def endTable(self) :
188  # we assume that headers are done by now !!
189  self.outFile.write( "</tbody>" + '\n')
190  self.outFile.write( "</table>" + '\n')
191 
192  # --------------------------------------------------------------------------------
193  def headers(self) :
194  # make sure to write the headers only once ...
195  if not self.headersDone :
196 
197  self.outFile.write( "<html> " + '\n')
198 
199  self.outFile.write( "<head> " + '\n')
200 
201  if self.style:
202  self.outFile.write( self.style + '\n')
203 
204  self.outFile.write( "<TITLE>" + self.title + "</TITLE>" + '\n')
205  if self.scriptCode:
206  self.outFile.write( self.scriptCode + '\n' )
207  self.outFile.write( "</head> " + '\n')
208  self.outFile.write( "<body>" + '\n')
209  self.headersDone = True
210 
211  return
212 
213  # --------------------------------------------------------------------------------
214  def trailers(self) :
215  # write only if headers have been written ...
216  if self.headersDone :
217  self.outFile.write( "</body>" + '\n')
218  self.outFile.write( "</html> " + '\n')
219  pass
220  return
221 
222