CMS 3D CMS Logo

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