Package pylal :: Package dq :: Module dqHTMLUtils
[hide private]
[frames] | no frames]

Source Code for Module pylal.dq.dqHTMLUtils

  1  #!/usr/bin/env python 
  2   
  3  # ============================================================================= 
  4  # Preamble 
  5  # ============================================================================= 
  6   
  7  from __future__ import division 
  8  import re,socket 
  9  from glue import markup 
 10   
 11  from glue import git_version 
 12   
 13  __author__  = "Duncan Macleod <duncan.macleod@astro.cf.ac.uk>" 
 14  __version__ = "git id %s" % git_version.id 
 15  __date__    = git_version.date 
 16   
 17  """ 
 18  This module provides a few extensions to glue.markup to streamline GEO/LIGO detector characterisation tools that use very similar web interfaces 
 19  """ 
 20   
 21  # ============================================================================= 
 22  # Write table 
 23  # ============================================================================= 
 24   
25 -def write_table(page, headers, data, cl=''):
26 27 """ 28 Write table into glue.markup.page object. headers are written with <th>, 29 multiple columns of data are written with <td>. Classes include: 30 * "", default class writes standard table with single column of headers 31 and multiple columns of data 32 * "list", writes table of 'header[i]: data[i]' definition-style entries 33 34 Arguments: 35 36 page : glue.markup.page 37 page object into which to write table 38 headers : list 39 list of table header elements 40 data : list 41 list (or nested list) of table data elements, list of lists used for 42 multiple rows 43 44 Keyword arguments: 45 46 cl : string 47 name for HTML table class, cl='list' treats special case above 48 49 """ 50 51 # open table 52 page.table(class_=cl) 53 54 # list: print two vertical columns of header:data pairs 55 if cl=='list': 56 for i in range(len(headers)): 57 58 page.tr() 59 page.th(str(headers[i])) 60 page.td(str(data[i])) 61 page.tr.close() 62 63 # otherwise print 'standard' table with single header row and multiple data 64 # rows 65 else: 66 page.tr() 67 if len(headers)==1: 68 page.th(str(headers[0]), colspan="100%") 69 else: 70 for n in headers: 71 page.th(str(n)) 72 page.tr.close() 73 74 if data and not re.search('list',str(type(data[0]))): 75 data = [data] 76 77 for row in data: 78 page.tr() 79 for item in map(str, row): 80 page.td(item) 81 82 page.table.close() 83 84 return page
85 86 # ============================================================================= 87 # Write <head> 88 # ============================================================================= 89
90 -def write_head(title, css, js, base=None, refresh=None, jquery=True):
91 92 """ 93 Returns glue.markup.page object with <head> tag filled. 94 95 Arguments: 96 97 title : string 98 text for <title> tag 99 css : string 100 relative path to style sheet 101 js : string 102 relative path to javascript 103 104 Keyword arguments: 105 106 base : string 107 absolute http(s) path of url base 108 refresh : int 109 number of seconds after which to refresh page automatically 110 jquery : [ True | False ] 111 import jquery AJAX script in header, default: True 112 """ 113 114 # generate object 115 page = markup.page(mode="strict_html") 116 page._escape = False 117 118 # open head 119 page.head() 120 # add base 121 if base: 122 page.base(href=base) 123 # add html auto-refresh 124 if refresh: 125 page.meta(http_equiv="refresh", content="%s" % refresh) 126 # link stylesheet 127 if isinstance(css, str): css = [css] 128 for c in css: 129 page.link(media="all", href=c, type="text/css", rel="stylesheet") 130 # add title 131 page.title(title) 132 133 if jquery: 134 page.script("", src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0"\ 135 "/jquery.min.js", type="text/javascript") 136 if isinstance(js, str): js = [js] 137 for j in js: 138 page.script("", src=j, type="text/javascript") 139 page.head.close() 140 141 return page
142 143 # ============================================================================= 144 # Write <div id="header"> 145 # ============================================================================= 146
147 -def write_banner(title, text=""):
148 149 """ 150 Returns glue.markup.page object for <div id="header"> 151 """ 152 153 page = markup.page(mode="strict_html") 154 page._escape = False 155 156 page.div(class_="content", id="header") 157 page.div() 158 page.h1(title) 159 page.h3(text) 160 page.div.close() 161 162 page.div.close() 163 164 return page
165 166 # ============================================================================= 167 # Write <div id="menubar"> 168 # ============================================================================= 169
170 -def write_menu(sections, pages, current=None):
171 172 """ 173 Returns glue.markup.page object for <div id="menubar">, constructing menu 174 in HTML. 175 176 Arguments: 177 178 sections : list 179 ordered list of menu entry names 180 pages : dict 181 dict of section:href pairs holding link paths for each element of 182 sections list 183 """ 184 185 page = markup.page(mode="strict_html") 186 page._escape = False 187 188 page.div(id="menubar") 189 190 for i,sec in enumerate(sections): 191 if sec==current: cl = "menulink selected" 192 else: cl = "menulink" 193 page.a(sec, id="link_%d" % i, class_=cl, href=pages[sec]) 194 195 page.script("",type="text/javascript") 196 197 page.div.close() 198 199 return page
200 201 # ============================================================================= 202 # Initialise page 203 # ============================================================================= 204
205 -def init_page(head, banner, menu, **kwargs):
206 207 """ 208 Initialise html into markup page, including <head> tag, banner and menu. 209 Pass further html elements to the body tag using the kwargs. 210 """ 211 212 # write html 213 page = markup.page() 214 page._escape = False 215 216 # initialise page 217 page.add("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "+\ 218 "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">") 219 page.html(xmlns="http://www.w3.org/1999/xhtml", lang="en",\ 220 **{"xml:lang":"en"}) 221 page.add(head()) 222 223 # open body 224 page.body(**kwargs) 225 226 # open container for page (needed to position footer) 227 page.div(id="container") 228 # add banner 229 page.add(banner()) 230 # open content (tab below banner and above footer) 231 page.div(id="content") 232 page.div() 233 # print menu 234 page.add(menu()) 235 # initialise maintab 236 page.div(id="maintab") 237 238 return page
239 240 # ============================================================================= 241 # Close page 242 # ============================================================================= 243
244 -def close_page(page, footer=False):
245 246 """ 247 Close content, maintab, and container divs, write footer and close 248 <body> and <html> tags. 249 """ 250 251 # close maintab 252 page.div.close() 253 # close content tab 254 page.div.close() 255 page.div.close() 256 # close container 257 page.div.close() 258 # add footer 259 if footer: 260 page.add(footer()) 261 262 # close page 263 page.body.close() 264 page.html.close() 265 266 return page
267 268 # ============================================================================= 269 # Write glossary 270 # ============================================================================= 271
272 -def write_glossary(page, terms):
273 274 """ 275 Write a glossary of DQ terms into the glue.markup.page object page using the 276 list of (term,definition) tuples terms. 277 """ 278 279 page.h2() 280 page.add('Glossary') 281 page.h2.close() 282 page.add('This section gives a glossary of terms used in this analysis') 283 284 i=1 285 286 # write section 287 page = write_h(page, 'Glossary', [i], cl=3) 288 page.div(id="div_%s" % (i), style="display: none;") 289 290 page.add('The LIGO-Virgo acronym wiki can be found on') 291 page.a(href="https://www.lsc-group.phys.uwm.edu/ligovirgo/cbcnote/Acronyms") 292 page.add('this page') 293 page.a.close() 294 295 # write glossary table 296 tmp = {} 297 for (term, text) in terms: 298 tmp[term.title()] = text 299 th = sorted(tmp.keys()) 300 td = [tmp[t] for t in th] 301 page = write_table(page, th, td, 'list') 302 303 page.div.close() 304 305 return page
306 307 # ============================================================================= 308 # Write <hX> 309 # ============================================================================= 310
311 -def write_h(page, title, id, cl=3, toggle=True):
312 313 """ 314 Write hX header into glue.markup.page object page with toggling. Text 315 contained within title is printed while link is constructed and toggled 316 using javascipt toggleVisible function 317 """ 318 319 if not (isinstance(id, list) or isinstance(id, tuple)): 320 id = [id] 321 id = list(map(str, id)) 322 323 kwargs = {} 324 if toggle: 325 kwargs['onclick'] = "toggleVisible('%s');" % '.'.join(id) 326 327 page.input(id="input_%s" % '.'.join(id), type="button", class_="h%s" % cl,\ 328 value=title, **kwargs) 329 330 return page
331 332 # ============================================================================= 333 # Link image 334 # ============================================================================= 335 347 348 # ============================================================================= 349 # Link file 350 # ============================================================================= 351 362 363 # ============================================================================= 364 # Construct HTML base 365 # ============================================================================= 366
367 -def get_ldas_url():
368 """ 369 Returns the url for http access to this host, based on its domain name 370 Returns None-type if you're not on a recognised LIGO-Virgo cluster. 371 Should not be used when not on an LDAS cluster, can't tell the difference 372 between nodes on CDS network at a site, and those on LDAS, for example... 373 374 Example: 375 >>> socket.getfqdn() 376 ldas-pcdev1.ligo-la.caltech.edu 377 >>> dqHTMLUtils.get_ldas_url() 378 'https://ldas-jobs.ligo-la.caltech.edu' 379 """ 380 381 # get fully qualified domain name (FQDN) 382 fqdn = socket.getfqdn() 383 384 # work out web root 385 if re.search('ligo.caltech', fqdn): 386 root = "https://ldas-jobs.ligo.caltech.edu" 387 elif re.search('ligo-wa', fqdn): 388 root = "https://ldas-jobs.ligo-wa.caltech.edu" 389 elif re.search('ligo-la', fqdn): 390 root = "https://ldas-jobs.ligo-la.caltech.edu" 391 elif re.search('atlas', fqdn): 392 match = re.search('atlas[13]', fqdn) 393 if match: 394 host = match.group() 395 root = "https://%s.atlas.aei.uni-hannover.de" % host 396 else: 397 match = "https://atlas1.atlas.aei.uni-hannover.de" 398 elif re.search('phy.syr', fqdn): 399 root = "https://sugar-jobs.phy.syr.edu" 400 elif re.search('astro.cf', fqdn): 401 root = "https://gravity.astro.cf.ac.uk" 402 elif re.search('phys.uwm', fqdn): 403 root = "https://ldas-jobs.phys.uwm.edu" 404 else: 405 root = None 406 407 return root
408