1
2
3
4
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
23
24
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
52 page.table(class_=cl)
53
54
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
64
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
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
115 page = markup.page(mode="strict_html")
116 page._escape = False
117
118
119 page.head()
120
121 if base:
122 page.base(href=base)
123
124 if refresh:
125 page.meta(http_equiv="refresh", content="%s" % refresh)
126
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
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
145
146
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
168
169
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
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
213 page = markup.page()
214 page._escape = False
215
216
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
224 page.body(**kwargs)
225
226
227 page.div(id="container")
228
229 page.add(banner())
230
231 page.div(id="content")
232 page.div()
233
234 page.add(menu())
235
236 page.div(id="maintab")
237
238 return page
239
240
241
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
252 page.div.close()
253
254 page.div.close()
255 page.div.close()
256
257 page.div.close()
258
259 if footer:
260 page.add(footer())
261
262
263 page.body.close()
264 page.html.close()
265
266 return page
267
268
269
270
271
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
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
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
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
334
335
336 -def link_image(page, href, src, alt, title, **kwargs):
337
338 """
339 Link image into glue.markup.page object page with standard options
340 """
341
342 page.a(href=href, title=title, **kwargs)
343 page.img(src=src, alt=alt, **kwargs)
344 page.a.close()
345
346 return page
347
348
349
350
351
353
354 """
355 Link file into glue.markup.page object page, with associated text and
356 options.
357 """
358
359 page.a(text, href=href, rel="external")
360
361 return page
362
363
364
365
366
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
382 fqdn = socket.getfqdn()
383
384
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