Package glue :: Module cbcwebpage
[hide private]
[frames] | no frames]

Source Code for Module glue.cbcwebpage

  1  # Copyright (C) 2009 Chad Hanna 
  2  # 
  3  # This program is free software; you can redistribute it and/or modify it 
  4  # under the terms of the GNU General Public License as published by the 
  5  # Free Software Foundation; either version 3 of the License, or (at your 
  6  # option) any later version. 
  7  # 
  8  # This program is distributed in the hope that it will be useful, but 
  9  # WITHOUT ANY WARRANTY; without even the implied warranty of 
 10  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
 11  # Public License for more details. 
 12  # 
 13  # You should have received a copy of the GNU General Public License along 
 14  # with this program; if not, write to the Free Software Foundation, Inc., 
 15  # 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. 
 16   
 17  # 
 18  # ============================================================================= 
 19  # 
 20  #                                   Preamble 
 21  # 
 22  # ============================================================================= 
 23  # 
 24   
 25  from glue import markup 
 26  from glue.markup import oneliner as e 
 27  from glue import git_version 
 28   
 29  import subprocess 
 30  import os, sys, time, socket, glob, math 
 31  import shutil,urllib 
 32   
 33  __author__ = "Chad Hanna <channa@caltech.edu>" 
 34  __version__ = "git id %s" % git_version.id 
 35  __date__ = git_version.date 
 36   
 37   
 38  ############################################################################### 
 39  ##### UTILITY FUNCTIONS ####################################################### 
 40  ############################################################################### 
 41   
 42  # THIS SHOULD MOST CERTAINLY NEVER BE USED :) 
43 -def web_path_to_url(path):
44 host = socket.getfqdn() 45 pl = path.rstrip('/').split('/') 46 47 #FIXME add more hosts as you need them 48 if 'ligo.caltech.edu' in host: return "https://ldas-jobs.ligo.caltech.edu/~" +pl[pl.index('public_html')-1] + '/' + '/'.join(pl[pl.index('public_html')+1:]) 49 if 'ligo-la.caltech.edu' in host: return "https://ldas-jobs.ligo-la.caltech.edu/~" +pl[pl.index('public_html')-1] + '/' + '/'.join(pl[pl.index('public_html')+1:]) 50 if 'ligo-wa.caltech.edu' in host: return "https://ldas-jobs.ligo-wa.caltech.edu/~" +pl[pl.index('public_html')-1] + '/' + '/'.join(pl[pl.index('public_html')+1:]) 51 if 'phys.uwm.edu' in host: return "https://ldas-jobs.phys.uwm.edu/~" + pl[pl.index('public_html')-1] + '/' + '/'.join(pl[pl.index('public_html')+1:]) 52 if 'phy.syr.edu' in host: return "https://sugar-jobs.phy.syr.edu/~" + pl[pl.index('public_html')-1] + '/' + '/'.join(pl[pl.index('public_html')+1:]) 53 if 'aei.uni-hannover.de' in host: return "https://atlas.atlas.aei.uni-hannover.de/~" + pl[pl.index('WWW')-1] + '/' + '/'.join(pl[pl.index('WWW')+1:]) 54 sys.stderr.write("WARNING: could not find web server, returning empty string\n") 55 return ''
56 57
58 -def create_toggle(filename="toggle.js"):
59 """ 60 This function is just an alias to create a javascript for the toggle on/off. 61 62 @return: nothing 63 """ 64 fname = open(filename, "w") 65 fname.write(""" 66 function get_url_vars() { 67 var st = window.location.href.split('?'), 68 obj = {}, //an object to store properties and values in 69 eq, 70 i; 71 if (st[1]) { //if a ( ? ) was found in the split, use the second part after the ? 72 st = unescape(st[1]).split('&'); //split st into array of strings containing url variables=values 73 for (i = 0; i < st.length; i++) { 74 eq = st[i].split('='); //get values from both sides of ( = ) sign 75 obj[eq[0]] = eq[1]; //insert properties and values into object 76 } 77 return obj; 78 } 79 return false; 80 } 81 82 $(document).ready(function() { 83 $(".fancybox").fancybox(); 84 }); 85 86 window.onload = function () { 87 var vars = get_url_vars(), prop; 88 for (url in vars) { 89 loadURL(url) 90 } 91 92 }; 93 94 function toggle2(showHideDiv, switchTextDiv) { 95 var ele = document.getElementById(showHideDiv); 96 var text = document.getElementById(switchTextDiv); 97 if(ele.style.display == "block") { 98 ele.style.display = "none"; 99 } 100 else { 101 ele.style.display = "block"; 102 } 103 } 104 105 function afterLoadFrame() { 106 $('#iframecontent a[rel="external"]').attr('target','_blank'); 107 $('#iframecontent input').hide(); 108 $('#iframecontent p:first').hide(); 109 } 110 111 function loadFrame(sourceURL) { 112 $("#iframecontent").load(sourceURL,{},afterLoadFrame); 113 str = window.location.href.split('?') 114 window.location.href = str[0] + "?" + sourceURL 115 } 116 117 function loadURL(sourceURL) { 118 $("#iframecontent").load(sourceURL,{},afterLoadFrame); 119 } 120 121 function toggleAllOpen() { 122 var tags = document.getElementsByTagName('div'); 123 for (t in tags) 124 tags[t].style.display = "block"; 125 } 126 """) 127 fname.close() 128 return filename
129
130 -def script_dict(fname):
131 script = {} 132 tog = os.path.split(create_toggle(fname))[1] 133 script[tog] = 'javascript' 134 script['https://ldas-jobs.ligo-wa.caltech.edu/~detchar/html/fancybox/source/jquery.fancybox.pack.js?v=2.1.5'] = 'javascript' 135 script['https://ldas-jobs.ligo-wa.caltech.edu/~detchar/html/jquery-1.10.2.min.js'] = 'javascript' 136 return (script, [tog])
137 138
139 -def which(prog):
140 which = subprocess.Popen(['which',prog], stdout=subprocess.PIPE) 141 out = which.stdout.read().strip() 142 if not out: 143 sys.stderr.write("ERROR: could not find %s in your path, have you built the proper software and source the proper env. scripts?\n" % (prog,prog)) 144 raise ValueError 145 sys.exit(1) 146 return out
147
148 -def user_and_date():
149 tmstr = "/".join([str(i) for i in time.gmtime()[0:3]]) 150 tmstr += " " + ":".join([str(i) for i in time.gmtime()[3:5]]) 151 return "%s - %s" % (os.environ['USER'], tmstr)
152
153 -def image_glob(pat,cols=3,ignore_thumb=True, width=240):
154 image_list = [] 155 for image in glob.glob(pat): 156 if 'thumb' in image and ignore_thumb: continue 157 # add the absolute path 158 else: image_list.append(os.path.abspath(image)) 159 image_list.sort() 160 plot_list = [_imagelinkcpy(plot,width=width) for plot in image_list] 161 cols = int(cols) 162 return [plot_list[i*cols:i*cols+cols] for i in range(int(math.ceil(len(plot_list) / float(cols))))]
163
164 -def image_table_from_url_table(table):
165 out = [] 166 for col in table: 167 row = [_imagelinkcpy(url) for url in col] 168 out.append(row) 169 return out
170
171 -def image_table_from_cache(inputcache,cols=3,ignore_thumb=True):
172 image_list = [] 173 for image in inputcache: 174 image_list.append(image.url) 175 image_list.sort() 176 plot_list = [_imagelinkcpy(plot) for plot in image_list] 177 cols = int(cols) 178 return [plot_list[i*cols:i*cols+cols] for i in range(int(math.ceil(len(plot_list) / float(cols))))]
179
180 -def wiki_table_parse(file):
181 #FIXME assumes table files of the form 182 # === title === 183 # ||data||data|| 184 # ||data||data|| 185 186 tabs = [] 187 titles = [] 188 tab = [] 189 for line in open(file).readlines(): 190 if '===' in line: 191 titles.append(line.replace("=","")) 192 if tab: tabs.append(tab) 193 tab = [] 194 if '||' in line: tab.append(line.split('||')[1:]) 195 tabs.append(tab) 196 return tabs, titles
197 198 # PROBABLY DOES NOT EVER NEED TO BE USED DIRECTLY, BUT IS USED IN cbcpage
199 -class _subpage_id(object):
200 - def __init__(self, id, link_text, closed_flag=0):
201 self.id = id 202 self.link_text = link_text 203 self.closed_flag = closed_flag
204 205 ############################################################################### 206 ##### CBC WEB PAGE CLASSES #################################################### 207 ############################################################################### 208 217
218 -class _text(markup.page):
219 - def __init__(self, txt="", bold=False, italic=False):
220 markup.page.__init__(self, mode="strict_html") 221 if bold: self.b() 222 if italic: self.i() 223 self.add(txt) 224 if bold: self.b.close() 225 if italic: self.i.close()
226 - def get_content(self):
227 return self.content
228 236
237 -class _imagelinkcpy(markup.page):
238 - def __init__(self, imagepath, thumbpath=None, tag="img", width=240):
239 markup.page.__init__(self, mode="strict_html") 240 try: os.mkdir('Images') 241 except: pass 242 #So that you can give it a url 243 #imagepath.replace('file://localhost','').strip() 244 imagepath, headers = urllib.urlretrieve(imagepath) 245 imgname = os.path.split(imagepath.rstrip('/'))[1] 246 shutil.copy(imagepath, 'Images/') 247 if not thumbpath: 248 # FIXME we cannot assume convert is installed everywhere 249 # Is there a python library to do this? 250 thumbname = 'Images/' + "thumb_" + imgname 251 command = 'convert Images/%s -resize %dx%d -antialias -sharpen 2x2 %s' % (imgname, width, width, thumbname) 252 popen = subprocess.Popen(command.split()) 253 popen.communicate() 254 status = popen.returncode 255 imgname = 'Images/' + imgname 256 else: 257 thumbpath.replace('file://localhost','').strip() 258 thumbname = os.path.split(thumbpath.rstrip('/'))[1] 259 shutil.copy(thumbpath, 'Images/') 260 thumbname = 'Images/' + thumbname 261 imgname = 'Images/' + imgname 262 self.add('<a class="fancybox" href=%s target="_blank"><img src=%s width=%d></a>' % (imgname, thumbname, width))
263
264 - def get_content(self):
265 return self.content
266 267
268 -class _table(markup.page):
269 - def __init__(self, two_d_data, title="", caption="", tag="table", num="1"):
270 markup.page.__init__(self, mode="strict_html") 271 self.add("<br>") 272 if title: 273 self.b("%s. %s" %(num, title.upper()) ) 274 275 self.table() 276 for row in two_d_data: 277 self.add('<tr>') 278 tdstr = "" 279 for col in row: 280 tdstr += "<td>%s</td>" % (str(col),) 281 self.add(tdstr) 282 self.add('</tr>') 283 self.table.close() 284 if self.caption: self.i("%s. %s" %(num, caption)) 285 self.add("<br>")
286
287 - def get_content(self):
288 return self.content
289 290 # PROBABLY DOES NOT EVER NEED TO BE USED DIRECTLY, BUT IS USED IN cbcpage
291 -class _section(markup.page):
292 - def __init__(self, tag, title="", secnum="1", pagenum="1", level=2, open_by_default=False):
293 markup.page.__init__(self, mode="strict_html") 294 self.pagenum = pagenum 295 self.secnum = secnum 296 self._title = title 297 self.sections = {} 298 self.section_ids = [] 299 self.level = level 300 self.tag = tag 301 self.id = tag + self.secnum 302 self.tables = 0 303 self.add('<div class="contenu"><h%d id="toggle_%s" onclick="javascript:toggle2(\'div_%s\', \'toggle_%s\');"> %s.%s %s </h%d>' % (level, self.id, secnum, self.id, pagenum, secnum, title, level) ) 304 if open_by_default: 305 style = 'display:block;' 306 else: 307 style = 'display:none;' 308 self.div(id="div_"+secnum , style=style)
309
310 - def add_section(self, tag, title="", open_by_default=False):
311 secnum = "%s.%d" % (self.secnum, len(list(self.sections.values()))+1) 312 self.sections[tag] = _section(tag, title=title, secnum=secnum, pagenum=self.pagenum, level=self.level+1, open_by_default=open_by_default) 313 self.section_ids.append([len(list(self.sections.values())), tag]) 314 return self.sections[tag]
315
316 - def get_content(self):
317 self.section_ids.sort() 318 out = self.content 319 self.div.close() 320 self.div.close() 321 for num, key in self.section_ids: 322 out.extend(self.sections[key].get_content()) 323 return out
324
325 - def add_table(self, two_d_data, title="", caption="", tag="table", num=0):
326 self.tables += 1 327 tabnum = "%s %s.%s.%s" % ("Table", self.pagenum, self.secnum, str(self.tables)) 328 table = _table(two_d_data, title=title, caption=caption, tag="table", num=tabnum) 329 self.content.extend(table.get_content()) 330 return self
331 336
337 - def add_text(self, **kwargs):
338 text = _text(**kwargs) 339 self.content.extend(text.get_content()) 340 return self
341 342 # MAIN CBC WEB PAGE CLASS
343 -class cbcpage(markup.page):
344
345 - def __init__(self, title="cbc web page", path='./', css=["//versions.ligo.org/cgit/lalsuite/plain/glue/etc/cbcwebpage.css","https://ldas-jobs.ligo-wa.caltech.edu/~detchar/html/fancybox/source/jquery.fancybox.css?v=2.1.5"], script=None, pagenum=1, verbose=False):
346 """ 347 """ 348 scdict = script_dict(fname='%s/%s' % (path,"toggle.js")) 349 if not script: script = scdict[0] 350 self.front = "" 351 scriptfiles = scdict[1] 352 self.verbose = verbose 353 self._style = css 354 self._title = title 355 self._script = script 356 self.path = path 357 self.pagenum = pagenum 358 359 markup.page.__init__(self, mode="strict_html") 360 self._escape = False 361 doctype="""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">""" 362 doctype+="""\n<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">""" 363 self.init(title=self._title, css=self._style, script=self._script , doctype=doctype) 364 365 self.subpages = {} 366 self.subpage_ids = [] 367 self.external_frames = [] 368 369 self.sections = {} 370 self.section_ids = [] 371 372 self.tables = 1 373 self.fnames = scriptfiles
374
375 - def add_subpage(self, tag, title, link_text=None):
376 """ 377 """ 378 379 subpage_num = len(list(self.subpages.values())) + 1 380 if not link_text: link_text=str(subpage_num) 381 382 # tuple including number so it can be sorted later 383 self.subpages[tag] = cbcpage(title=title,path=self.path,css=self._style,script=self._script,pagenum=subpage_num) 384 self.subpages[tag].add('<table align=right><tr><td align=right onclick="javascript:toggleAllOpen();"><b>Toggle Open</b></td></tr></table>') 385 self.subpage_ids.append( [subpage_num, _subpage_id(tag, link_text)] ) 386 return self.subpages[tag]
387
388 - def close_subpage(self,id=None):
389 #SECTIONS WILL AUTOMATICALLY BE CLOSED IN WRITE METHOD IF NOT DONE EXPLICITELY 390 self.subpage_ids.sort() 391 if not id: id = subpage_ids[-1][1].id 392 393 self.subpages[id].div.close() 394 self.subpages[id].add("<!-- close div contenu-->") 395 self.subpages[id].div.close() 396 self.subpages[id].add_footer()
397 401
402 - def add_external_frame(self, linkurl, linktext):
403 self.external_frames.append([linkurl, linktext])
404
405 - def write(self, file_name="index", image = "https://www.lsc-group.phys.uwm.edu/ligovirgo/cbc/public/segments/S5/thomasLegacy.jpg", tag = "CBC"):
406 407 if self.subpage_ids: 408 409 self.div(id_="wrapper") 410 self.div(id_="menubar") 411 self.div(id_="menu") 412 self.subpage_ids.sort() 413 # do the sub pages 414 for num,secid in self.subpage_ids: 415 id = secid.id 416 if secid.closed_flag == 0: self.close_subpage(id) 417 secfname = file_name + "_" + id 418 self.fnames.append(self.subpages[id].write(secfname)) 419 self.div(class_="menuitem") 420 self.add('\t<a class="menulink" href="javascript:loadFrame(\'%s.html\');"> %d: %s </a>\n' % (secfname, num, secid.link_text) ) 421 self.div.close() 422 for i, ext_frame in enumerate(self.external_frames): 423 self.div(class_="menuitem") 424 self.add('\t<a class="menulink" href=%s# onclick="javascript:loadFrame(\'%s\');"> %d: %s </a>\n' % (ext_frame[0], ext_frame[0], num+i, ext_frame[1]) ) 425 self.div.close() 426 self.div.close() 427 self.div.close() 428 self.div(id_="ihope") 429 self.add('<h2> %s </h2>'%tag) 430 self.add('<img width=90 src="%s">'%image) 431 self.div.close() 432 self.div(id_='header') 433 self.add('<h1>' + self._title +' </h1>') 434 self.add('<h3> ' + user_and_date() + ' </h3>') 435 self.div.close() 436 self.div(id_='iframecontent') 437 if self.front: self.add(self.front) 438 self.add('<p id="placeholder">Please select a report section on the left.</p>') 439 self.div.close() 440 self.div.close() 441 442 # do the sections 443 self.section_ids.sort() 444 for num, key in self.section_ids: 445 self.content.extend(self.sections[key].get_content()) 446 self.fnames.append('%s/%s.html' % (self.path, file_name)) 447 pagefile = open('%s/%s.html' % (self.path, file_name), 'w') 448 pagefile.write(str(self)) 449 pagefile.close() 450 return '%s/%s.html' % (self.path, file_name)
451
452 - def add_section(self, tag, title="", level=2, open_by_default=False):
453 """ 454 """ 455 secnum = len(list(self.sections.values())) + 1 456 self.section_ids.append([secnum, tag]) 457 self.sections[tag] = _section(title=title, tag=tag, secnum=str(secnum), pagenum=str(self.pagenum), level=level, open_by_default=open_by_default) 458 return self.sections[tag]
459
460 - def add_table(self, two_d_data, title="", caption="", tag="table"):
461 self.tables += 1 462 table = _table(two_d_data, title=title, caption=caption, tag="table", num=str(self.pagenum) + " Table "+str(self.tables)) 463 self.content.extend(table.get_content()) 464 return self
465 470
471 - def add_text(self, **kwargs):
472 text = _text(**kwargs) 473 self.content.extend(text.get_content()) 474 return self
475