Package pylal :: Module webUtils
[hide private]
[frames] | no frames]

Source Code for Module pylal.webUtils

  1  """ 
  2  followup Web page classes 
  3   
  4  This 
  5  """ 
  6   
  7  __author__ = 'Chad Hanna <channa@phys.lsu.edu>' 
  8   
  9  import ConfigParser 
 10  import os,re # added for cacheStructure 
 11   
 12  from pylal import git_version 
 13   
 14  ############################################################################## 
 15  # Cachefying class 
 16  ############################################################################## 
 17   
18 -class cacheStructure(object):
19
20 - def __init__(self):
21 self.mainCache = {}
22
23 - def appendCache(self,cacheType,outputPath=None):
24 try: 25 self.mainCache.keys().index(cacheType) 26 except: 27 self.mainCache[cacheType] = [[],outputPath] 28 self.mainCache[cacheType][0].append('### ' + cacheType + ' CACHE FILE ###\n')
29
30 - def appendSubCache(self,cacheType,cacheEntry):
31 self.mainCache[cacheType][0].append(cacheEntry)
32
33 - def writeCache(self):
34 if len(self.mainCache) > 0: 35 mainFile = open('main.cache','w') 36 for subcache in self.mainCache.items(): 37 fileName = self.fillMainCache(subcache,mainFile) 38 if len(subcache[1][0]) > 1: 39 cache_file = open(fileName,'w') 40 for cacheEntry in subcache[1][0]: 41 cache_file.write(cacheEntry) 42 cache_file.close() 43 mainFile.close() 44 else: pass
45
46 - def fillMainCache(self,subcache,fileToWrite):
47 fileNames = [] 48 if subcache[1][1]: 49 fileName = subcache[1][1] + subcache[0] + '.cache' 50 else: 51 fileName = subcache[0] + '.cache' 52 if subcache[1][1]: 53 self.getCacheList(fileNames,subcache[1][1]) 54 try: 55 fileNames.index(fileName) 56 except: 57 if len(subcache[1][0]) > 1: 58 fileNames.append(fileName) 59 for file in fileNames: 60 fileToWrite.write(subcache[0] + ' ' + file + '\n') 61 return fileName
62
63 - def getCacheList(self,list,inputdir):
64 list_files = os.listdir(inputdir) 65 for element in list_files: 66 if re.search('.cache',element): 67 list.append(inputdir + element) 68 else: pass
69 70 ############################################################################## 71 # The webifying stuff starts here 72 ############################################################################## 73
74 -class Content:
75
76 - def __init__(self):
77 self.contentList = [] 78 self.table = [] 79 self.lastTable = None
80 #self.root = '' 81 85
86 - def verbatim(self,text):
87 thisVerbatim = Verbatim(text) 88 self.contentList.append(thisVerbatim)
89
90 - def text(self,text,type='',color=''):
91 thisText = Text(text,type,color) 92 self.contentList.append(thisText)
93
94 - def list(self,list):
95 thisList = List(list) 96 self.contentList.append(thisList)
97
98 - def image(self, image, link=None, width = 400):
99 thisImage = Image(image, self.root, link, width) 100 self.contentList.append(thisImage)
101
102 - def linebreak(self, times = 1):
103 thisBreak = Break(times) 104 self.contentList.append(thisBreak)
105
106 - def appendTable(self,rows,columns,border=0,width=800):
107 number = len(self.table) 108 thisTable = Table(rows,columns,border,width,number,self.root) 109 self.contentList.append(thisTable) 110 self.table.append(thisTable) 111 self.lastTable = self.table[number]
112 113 114 # Currently the write/read methods for talkBack don't work for lists or tables.
115 -class talkBack(Content):
116
117 - def __init__(self,outputWebFile):
118 self.fileName = outputWebFile.replace('html','ini') 119 self.summaryPlot = '' 120 self.summaryPlotCaption = '' 121 self.summaryText = '' 122 self.contentList = []
123 - def addSummaryPlot(self,plot,caption):
124 self.summaryPlot = plot 125 self.summaryPlotCaption = caption
126 - def addSummaryText(self,text):
127 self.summaryText = text
128
129 - def write(self):
130 file = open(self.fileName,'w') 131 file.write('[talkBack]\n') 132 file.write('\nsummaryPlot='+self.summaryPlot) 133 file.write('\nsummaryPlotCaption='+self.summaryPlotCaption) 134 file.write('\nsummaryText='+self.summaryText+'\n') 135 for content in range(len(self.contentList)): 136 self.contentList[content].write(file, 'talkBack',content) 137 file.close()
138
139 - def read(self):
140 cp = ConfigParser.ConfigParser() 141 cp.read(self.fileName) 142 try: self.summaryPlot = cp.get('talkBack','summaryPlot') 143 except: pass 144 try: self.summaryPlotCaption = cp.get('talkBack','summaryPlotCaption') 145 except: pass 146 try: self.summaryText = cp.get('talkBack','summaryText') 147 except: pass
148 149 #append talkback stuff to a valid webpage class (or section/subsection/table)
150 - def readAppend(self,webpage):
151 cp = ConfigParser.ConfigParser() 152 cp.read(self.fileName) 153 for section in cp.sections(): 154 if section.endswith('Link'): 155 try: link = cp.get(section,'link') 156 except: pass 157 try: text = cp.get(section,'text') 158 except: pass 159 webpage.link(link,text) 160 if section.endswith('Text'): 161 type = None 162 color = None 163 try: text = cp.get(section,'text') 164 except: pass 165 try: type = cp.get(section,'type') 166 except: pass 167 try: color = cp.get(section,'color') 168 except: pass 169 webpage.text(text,type,color) 170 if section.endswith('Verbatim'): 171 try: text = cp.get(section,'text') 172 except: pass 173 webpage.verbatim(text) 174 if section.endswith('Image'): 175 try: link = cp.get(section,'link') 176 except: link=None 177 try: image = cp.get(section,'image') 178 except: pass 179 try: width = cp.get(section,'width') 180 except: width = 400 181 webpage.image(image,link,width) 182 if section.endswith('Linebreak'): 183 try: times = cp.get(section,'linebreak') 184 except: pass 185 webpage.linebreak(times)
186 187 188 #Add sub pages and provide slick mechanism for writing the whole tree!?! 189 #this will help with the followup dag web generation... 190 # ADD CONCEPT OF RELATIVE PATH TO GIVE MORE FLEXIBILITY TO DIRECTORY STRUCTURE
191 -class WebPage(Content):
192 """ 193 Class to store the web page output of a followup program 194 """
195 - def __init__(self,title,filename,root=''):
196 #Content will be written before sections, which themselves may have content 197 self.contentList = [] 198 self.table = [] 199 self.section = [] 200 self.lastSection = None 201 self.title = title 202 self.root = root 203 self.subPage = [] 204 self.lastPage = None 205 self.filename = filename
206
207 - def appendSection(self,heading):
208 number = len(self.section) 209 self.section.append( Section( heading,number,self.filename,self.root ) ) 210 self.lastSection = self.section[number]
211 212
213 - def appendSubPage(self,title, file, root=''):
214 self.subPage.append(WebPage(title, file, root)) 215 self.lastPage = self.subPage[len(self.subPage)]
216
217 - def linkNewSubPage(self,title,file, text='', root=''):
218 if text: 219 self.link(root+file,text) 220 else: 221 self.link(root+file, title) 222 self.appendSubPage(title,file, root)
223 224
225 - def write(self,type):
226 self.writeHeader(self.file,type) 227 self.writeTitle(self.file,type) 228 self.writeTableOfContents(self.file,type) 229 # first do the content in this page 230 for content in self.contentList: 231 content.write(self.file,type) 232 for section in self.section: 233 section.write(self.file,type) 234 # now do the sub pages recursively 235 for page in self.subPage: 236 page.cleanWrite(type)
237 238
239 - def writeTitle(self, file, type):
240 if type == 'IUL': 241 pass # cause this is done in the header for IUL type
242
243 - def writeHeader(self, file, type):
244 if type == 'IUL': 245 file.write('<%method title>' + self.title + '</%method>\n') 246 file.write('<%method headline>' + self.title + '</%method>\n') 247 file.write('<%method cvsid>' + git_version.id + '</%method>\n')
248 #file.write('<h1>'+self.title+'</h1>\n') 249
250 - def writeTableOfContents(self,file,type):
251 if type == 'IUL': 252 file.write('<h2 id="fuwebtoc">Table of contents</h2>\n') 253 sectionTOC = [] 254 255 for section in self.section: 256 link = section.toclink 257 subSectionTOC = [] 258 for subsection in section.subSection: 259 subSectionTOC.append( [Link(subsection.toclink, subsection.heading)]) 260 if subSectionTOC: 261 sectionTOC.append( [Link(section.toclink, section.heading), List(subSectionTOC)] ) 262 else: 263 sectionTOC.append( [Link(section.toclink, section.heading)] ) 264 TOC = List(sectionTOC) 265 TOC.write(file,type)
266 267 # def cleanWrite(self, filename, type):
268 - def cleanWrite(self,type):
269 self.file = open(self.filename,'w') 270 self.write(type) 271 self.file.close()
272 273 # This class shouldn't really be used without a webpage as it's parent
274 -class Section(Content):
275 """ 276 Class to store a section of a webpage 277 """
278 - def __init__(self,heading,secNumber,filename,root=''):
279 self.contentList = [] 280 self.table = [] 281 self.subSection = [] 282 self.lastSub = None 283 self.heading = heading 284 self.secNumber = secNumber 285 #self.toclink = root + '/'+filename.split('/')[-1]+'#section' + str(self.secNumber) 286 self.toclink = root + filename.split('/')[-1]+'#section' + str(self.secNumber) 287 self.root = root 288 self.filename = filename
289
290 - def appendSubSection(self,heading):
291 number = len(self.subSection) 292 self.subSection.append( SubSection( heading,self.secNumber,number, self.filename,self.root ) ) 293 self.lastSub = self.subSection[number]
294
295 - def write(self,file,type):
296 self.writeSectionHeader(file,type) 297 for content in self.contentList: 298 content.write(file,type) 299 for subSection in self.subSection: 300 subSection.write(file,type)
301
302 - def writeSectionHeader(self,file,type):
303 if type == 'IUL': 304 file.write('<h2 id="section'+str(self.secNumber)+'">'+str(self.secNumber+1)+'. ' + self.heading+'\n') 305 #file.write('<a href="'+self.root+'/'+file.name.split('/')[-1]+'#fuwebtoc">[Back to TOC]</a></h2>\n') 306 file.write('<a href="'+self.root+file.name.split('/')[-1]+'#fuwebtoc">[Back to TOC]</a></h2>\n')
307 308 # This class shouldn't really be used without a section as its parent, which 309 # itself has a webpage as its parent
310 -class SubSection(Content):
311 """ 312 Class to store a subsection of a webpage 313 """
314 - def __init__(self,heading,secNumber,subNumber, filename,root=''):
315 self.contentList = [] 316 self.table = [] 317 self.heading = heading 318 self.secNumber = secNumber 319 self.subNumber = subNumber 320 self.root = root 321 self.filename = filename 322 #self.toclink = root + '/'+filename.split('/')[-1]+'#subsection' + str(self.secNumber) + '.' + str(self.subNumber) 323 self.toclink = root + filename.split('/')[-1]+'#subsection' + str(self.secNumber) + '.' + str(self.subNumber)
324
325 - def write(self,file,type):
326 self.writeSubSectionHeader(file,type) 327 for content in self.contentList: 328 content.write(file,type)
329
330 - def writeSubSectionHeader(self,file,type):
331 if type == 'IUL': 332 file.write('<h3 id="subsection'+str(self.secNumber)+'.'+str(self.subNumber)+'">'+str(self.secNumber+1)+'.'+str(self.subNumber+1)+'. '+self.heading+'\n') 333 #file.write('<a href="'+self.root+'/'+file.name.split('/')[-1]+'#fuwebtoc">[Back to TOC]</a></h3>\n') 334 file.write('<a href="'+self.root+file.name.split('/')[-1]+'#fuwebtoc">[Back to TOC]</a></h3>\n')
335 336 337 338 # here is where the real 'content' is. Remember that all content is 339 # contained inside of a table. Table allows you to create rows, columns 340 # and cells, there shouldn't be any normal reason to use those classes 341 # by themselves - currently this just does tables of form (m x n)
342 -class Table:
343 """ 344 Class to store the web page output of a followup program 345 """
346 - def __init__(self,rows,columns,border,width,number,root):
347 self.row = [] 348 self.number = number 349 self.border = border 350 self.width = width 351 self.root = root 352 for i in range(rows): 353 self.row.append(Row(columns,root))
354
355 - def write(self,file,type):
356 if type == 'IUL': 357 file.write('<table border=' + str(self.border)+' width='+str(self.width)+'>\n') 358 for row in self.row: 359 row.write(file,type) 360 file.write('</table>\n')
361 362 # You shouldn't need to use this class - best to use Table
363 -class Row:
364 """ 365 Class to store a table row 366 """
367 - def __init__(self,columns,root):
368 self.cell = [] 369 self.root = root 370 for j in range(columns): 371 self.cell.append(Cell(root))
372
373 - def write(self,file,type):
374 if type == 'IUL': 375 file.write('<tr>') 376 for cell in self.cell: 377 cell.write(file,type) 378 file.write('</tr>\n')
379 380 # You shouldn't need to use this class - best to use Table
381 -class Cell(Content):
382 """ 383 Class to store a table cell 384 """
385 - def __init__(self,root):
386 self.contentList = [] 387 self.table = [] 388 self.root = root
389
390 - def write(self, file, type):
391 if type == 'IUL': 392 file.write('<td>') 393 for content in self.contentList: 394 content.write(file,type) 395 file.write('</td>')
396 397 398 # finally something that does something. (but not very much) 413
414 -class Verbatim:
415
416 - def __init__(self, text):
417 self.text = text
418
419 - def write(self, file, type,number=0):
420 if type == 'IUL': 421 file.write('\n<br><pre>' + self.text + '</pre><br>\n') 422 if type == 'talkBack': 423 file.write('['+str(number)+'.Verbatim]\n') 424 file.write('text='+self.text+'\n')
425
426 -class Text:
427
428 - def __init__(self,text,type='',color=''):
429 self.text = text 430 self.color = color 431 self.type = type
432
433 - def write(self, file, type,number =0):
434 if type == 'IUL': 435 file.write('<p>') 436 if self.color: 437 file.write('<font color=' + self.color + '>') 438 if self.type: 439 if isinstance(self.type, str): 440 file.write('<'+self.type+'>') 441 if isinstance(self.type, list): 442 for i in self.type: 443 file.write('<'+i+'>') 444 file.write(self.text) 445 if self.type: 446 if isinstance(self.type, str): 447 file.write('</'+self.type+'>') 448 if isinstance(self.type, list): 449 for i in self.type: 450 file.write('</'+i+'>') 451 if self.color: 452 file.write('</font>') 453 file.write('</p>\n') 454 455 if type==('talkBack'): 456 file.write('['+str(number)+'.Text]\n') 457 if self.type: 458 if isinstance(self.type, str): 459 file.write('type='+self.type+'\n') 460 if isinstance(self.type, list): 461 iStr = '' 462 for i in self.type: 463 iStr += i+',' 464 file.write('type='+iStr[0:-1]+'\n') 465 if self.color: file.write('color='+self.color+'\n') 466 file.write('text='+self.text +'\n')
467
468 -class List:
469
470 - def __init__(self,list):
471 # valid objects in this list are lists of Link, Verbatim, Text, List, Image 472 # or any custom object with a compliant write method. 473 # remember this expects a list of lists! 474 self.list = list
475
476 - def write(self, file, type,number=0):
477 if type == 'IUL': 478 file.write('<ol>\n') 479 for item in self.list: 480 file.write('<li>') 481 for element in item: 482 element.write(file,type) 483 file.write('\n') 484 file.write('</ol>\n')
485
486 -class Image:
487
488 - def __init__(self,image,root,link=None,width=400):
489 self.link = link 490 self.image = root+image 491 self.width = width
492 - def write(self, file, type, number = 0):
493 if type == 'IUL': 494 if self.link: 495 file.write('<a href="'+self.link+'"><img src="'+self.image+'" width = '+str(self.width)+'></a>') 496 else: 497 file.write('<img src="'+self.image+'" width = '+str(self.width)+'>') 498 499 if type == 'talkBack': 500 file.write('['+str(number)+'.Image]\n') 501 file.write('image='+self.image+'\n') 502 if self.link: file.write('link='+self.link+'\n') 503 if self.width: file.write('width='+self.width+'\n')
504 505
506 -class Break:
507
508 - def __init__(self, times = 1):
509 self.times = times 510 self.timesList = range(times)
511
512 - def write(self, file, type,number=0):
513 if type == 'IUL': 514 for time in self.timesList: 515 file.write('<br>') 516 if type == 'talkBack': 517 file.write('['+str(number)+'.Linebreak]\n') 518 file.write('times='+str(self.times)+'\n')
519