1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
40
41
42
44 host = socket.getfqdn()
45 pl = path.rstrip('/').split('/')
46
47
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
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
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
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
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
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
170
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
181
182
183
184
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
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
207
208
209 -class _link(markup.page):
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):
228
230 - def __init__(self, imageurl, thumburl, tag="img", width=240):
231 markup.page.__init__(self, mode="strict_html")
232 self.add('<a class="fancybox" href=%s target="_blank"><img src=%s width=%d></a>' % (imageurl, thumburl, width))
233
234 - def get_content(self):
236
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
243
244 imagepath, headers = urllib.urlretrieve(imagepath)
245 imgname = os.path.split(imagepath.rstrip('/'))[1]
246 shutil.copy(imagepath, 'Images/')
247 if not thumbpath:
248
249
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):
266
267
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):
289
290
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
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
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
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
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
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
466 - def add_link(self, **kwargs):
467 link = _link(**kwargs)
468 self.content.extend(link.get_content())
469 return self
470
471 - def add_text(self, **kwargs):
472 text = _text(**kwargs)
473 self.content.extend(text.get_content())
474 return self
475