Please note that the CVS and issue trackers have moved to GitHub. These Trac pages are no longer kept up-to-date.

root/seattle/trunk/deploymentscripts/deploy_html.py@5637

Revision 2520, 11.5 KB (checked in by konp, 10 years ago)

Committed the deployment scripts, just missing complete readme. Moved old files into /old before deletion.

Line 
1"""
2<Program Name>
3  deploy_html.py
4
5<Started>
6  June 2009
7
8<Author>
9  n2k8000@u.washington.edu
10  Konstantin Pik
11
12<Purpose>
13  The purpose of this file is to seperate any html generation required
14  for the webserver files.  This module just provides methods for creating
15  html pages and html tables from dicts and arrays.  See method details for
16  more information.
17
18
19<Usage>
20  This is not to be launched as an independent file, but its methods are to be used
21  as helper methods.
22"""
23
24
25import deploy_main
26
27import time
28import os
29
30
31"""
32Public functions:
33  html_add_to_top
34 
35"""
36# maps different warning levels to colors
37colors_table = ['Red', 'Gold', 'LightGreen', 'White']
38colors_map = {'Error' : 'Red', 'Success': 'LightGreen',
39  'Warning' : 'Gold', 'SmallError' : 'Yellow', 'Normal' : 'White'}
40
41 
42 
43# the acceptable node versions
44target_node_versions = ['0.1h', '0.1i', '0.1j']
45
46
47
48def get_colors_map():
49  # helper method
50  return colors_map
51
52 
53 
54def add_html_column_names(column_names):
55  """
56  <Purpose>
57    Creates a row in a table, but assumes them to be column names and thus
58    makes the entries bold.
59     
60  <Arguments>
61    column_names:
62      an array of string elements to be used as column names. This string
63      may contain HTML.
64   
65  <Exceptions>
66    None.
67
68  <Side Effects>
69    None.
70
71  <Returns>
72    The HTML string for the row.
73  """
74
75  # create a row
76  temp_html = add_row()
77  # add a cell for each entry
78  for each_title in column_names:
79    # and make it bold
80    temp_html += add_cell('<b>'+str(each_title)+'</b>')
81
82  # close the row
83  temp_html += close_row()
84  return temp_html
85
86 
87 
88def start_html():
89  # returns an HTML header
90  return '<html>'
91
92 
93 
94def close_html():
95  # closes the HTML and returns string
96  return '</html>'
97 
98 
99 
100def create_table():
101  # creates the main entry in a table, returns HTML string
102  return '<table border="1">'
103
104 
105 
106def add_cell(cell_value, cell_bg_color = None):
107  """
108  <Purpose>
109    Returns the HTML for a cell (html TD element).
110     
111  <Arguments>
112    cell_value:
113      1.  Can be a simple string that's the cell value (may contain HTML)
114      2.  Can be a tuple in which case the tuple is of form (cell_value, color)
115          cell_value can once again be a string/html and color is the bg color to use.
116    cell_bg_color:
117      The bg color to use for the cell.
118   
119  <Exceptions>
120    None.
121
122  <Side Effects>
123    None.
124
125  <Returns>
126    String. HTML for the newly created cell.
127  """
128
129  # check the type of the cell_value
130  if type(cell_value) == type(()):
131    cell_bg_color = cell_value[1]
132    cell_value = cell_value[0]
133
134  # use a bg color if one is set, and make sure to alight the text to the
135  # middle of the cell
136  if cell_bg_color:
137    cell_html = '<td bgcolor="'+cell_bg_color+'" valign="center">'+str(cell_value)+'</td>'
138  else:
139    cell_html = '<td valign="center">'+str(cell_value)+'</td>'
140 
141  return cell_html
142
143 
144 
145def add_row_of_elements(element_array):
146  # see below
147  return add_node_row(element_array)
148
149
150 
151def add_node_row(iterable_item_list):
152  """
153  <Purpose>
154    Takes in an iterable type, and for each item in that list it'll make a cell
155    and return the HTML for that row of elements.
156   
157    Basically makes a row from the list.
158     
159  <Arguments>
160    iterable_item_list:
161      Something that can be iterated over.  Should contain string-compatible values as
162      each value will be given its own cell.
163   
164  <Exceptions>
165    None.
166
167  <Side Effects>
168    None.
169
170  <Returns>
171    String. HTML for the row created from the list.
172  """
173
174
175  # calls add_row and creates a row with the information (with cells)
176  row_html = add_row()
177 
178  for each_item in iterable_item_list:
179    row_html += add_cell(each_item)
180   
181  # close the row.
182  row_html += close_row()
183 
184  return row_html
185 
186 
187 
188def add_row():
189  # creates a row element.
190  return '<tr>'
191
192 
193 
194def html_add_to_top(html_fn, html_to_add):
195  """
196  <Purpose>
197    Opens a file and adds some content to the top of the file.
198     
199  <Arguments>
200    html_fn:
201      the name of the html file.  note: this MUST be an html file.
202    html_to_add:
203      The HTML that'll be added to the top of the file.
204   
205  <Exceptions>
206    File reading/writing related exceptions.
207
208  <Side Effects>
209    None.
210
211  <Returns>
212    None.
213  """
214  try:
215    # get a handle on the file
216    html_filehandle = open(html_fn, 'r')
217   
218    # read the whole thing in
219    html_file_string = read_whole_file(html_filehandle)
220    html_filehandle.close()
221   
222    # now after closing the file, we'll add our html
223    html_file_string = html_file_string.replace('<html>', '<html><br>'+html_to_add+'<br>')
224   
225    # open the file and overwrite it with the new data
226    html_file_handle = open(html_fn, 'w+')
227    html_file_handle.write(html_file_string)
228    html_file_handle.close()
229   
230  except Exception, e:
231    print e
232 
233
234 
235def close_row():
236  # close the row
237  return '</tr>'
238
239 
240 
241def close_table():
242  # HTML to close the table
243  return '</table>'
244
245 
246 
247def generate_uniq_fn():
248  """
249  <Purpose>
250    Generates a uniq filename based on the current time.
251     
252  <Arguments>
253    None.
254   
255  <Exceptions>
256    None
257
258  <Side Effects>
259    None.
260
261  <Returns>
262    Tuple.  (htmlsummary.[time.time()], time.time())
263      The time.time() is stored in a variable so it'll be the same time.
264  """
265  current_time = int(time.time())
266  return ("htmlsummary."+str(current_time), str(current_time))
267
268 
269
270def read_whole_file(file_handle):
271  """
272  <Purpose>
273    Reads in a whole file given a file handle and returns the file as string.
274     
275  <Arguments>
276    file_handle:
277      the file_handle of the file to read
278   
279  <Exceptions>
280    None
281
282  <Side Effects>
283    None.
284
285  <Returns>
286    String. The contents of the file to which we have a handle to as a string.
287  """
288  temp_str = ""
289  for each_line in file_handle.xreadlines():
290    temp_str += each_line
291  return temp_str
292
293
294
295def make_link_to_detailed(node_name, html_fn):
296  """
297  <Purpose>
298    Generates a link to the detailed node data.
299     
300  <Arguments>
301    node_name:
302      The name of the node
303    html_fn:
304      The html file name which has the following format: htmlsummary.[timestamp]
305   
306  <Exceptions>
307    None
308
309  <Side Effects>
310    None.
311
312  <Returns>
313    String. HTML that's a link to the detailed log for this node.
314  """
315 
316  # split up the html_fn to get the timestamp
317  junk, sep, timestamp = html_fn.rpartition('.')
318 
319  # put it all together and return the html string
320  return '<a href="/detailed/'+node_name+'/'+timestamp+'" target="_blank">Details</a>'
321
322
323
324def format_text_for_html(text):
325  # basically replaces \n's with <br>'s
326  return str(text).replace('\n', '<br>')
327
328 
329 
330def html_table_from_dict2(table_dict, table_headers):
331  """
332  <Purpose>
333    Converts a dict to an HTML table.  The dict is assumed to have
334    1:1 mapping where each key maps to an array.  The key is then
335    created as the first element in that row.  The table_headers is assumed to
336    be the size of 1 + len(array) where each array size is expected to be the same
337    for each key.
338     
339  <Arguments>
340    table_dict:
341      Dict that has the following format:
342        {"stringkey1" : [el_1, el_2, el_n], "stringkey2" : [el_1, el_2, el_n], ... }
343      or
344        {"stringkey1" : "string1", "stringkey2" : "stringx", ... }
345    table_headers:
346      List (expected array, but in theory could be any iterable) that is of length
347      n + 1 where n is the number of elements in the array of stringkeyX (all should be
348      same size).
349   
350  <Exceptions>
351    None
352
353  <Side Effects>
354    None.
355
356  <Returns>
357    HTML for a complete table created from the dict with the headers from the list.
358  """
359 
360  # key -> array. Value printed as string.
361  html_table = create_table()
362 
363  html_table += add_html_column_names(table_headers)
364 
365  # for every key grab the elements
366  for each_key in table_dict:
367 
368    # create a row for this key.
369    html_table += add_row()
370 
371    # add the value of the key as the first element
372    html_table += add_cell(each_key)
373   
374    # if we map to an array
375    if type(table_dict[each_key]) == type([]):
376      # then dump each element of that array to its own cell
377      for each_element in table_dict[each_key]:
378        html_table += add_cell(each_element)
379    else:
380      # otherwise just dump whatever we map to (hopefully a string) to the cell
381      html_table += add_cell(each_key)
382   
383  # close the table
384  html_table += close_table()
385 
386  return html_table
387 
388
389
390def html_table_from_dict(table_dict, table_headers):
391  """
392  <Purpose>
393    Converts a dict to a simple, two column HTML table.  Simple mapping of
394    Key->Value where value is a string. The headers for the table are defined
395    in table_headers
396     
397  <Arguments>
398    table_dict:
399      Dict that has the following format:
400        {"stringkey1" : "string1", "stringkey2" : "stringx", ... }
401    table_headers:
402      List (expected array, but in theory could be any iterable) that is of length
403      n + 1 where n is the number of elements in the array of stringkeyX (all should be
404      same size).
405   
406  <Exceptions>
407    None
408
409  <Side Effects>
410    None.
411
412  <Returns>
413    HTML for a complete table created from the dict with the headers from the list.
414  """
415  # key -> value. Value printed as string
416  html_table = create_table()
417 
418  # generate the column headers
419  html_table += add_html_column_names(table_headers)
420 
421  # for each key
422  for each_key in table_dict:
423    # create a row
424    html_table += add_row()
425   
426    # add the keyname as the first element in the row
427    html_table += add_cell(each_key)
428   
429    # add whatever the key maps to as the second element in the row
430    html_table += add_cell(table_dict[each_key])
431   
432    # close the row
433    html_table += close_row()
434 
435  # close the table
436  html_table += close_table()
437 
438  return html_table
439 
440 
441
442def html_table_from_array(table_array):
443  """
444  <Purpose>
445    Convert an array to a table
446     
447  <Arguments>
448    table_array;
449      an array of the following form:
450      table_array[0] maps to an array where each element is a header
451      table_array[n > 0] maps to an array and each element in that array will be a row
452     
453      assumes table_array is a square array.
454   
455  <Exceptions>
456    None
457
458  <Side Effects>
459    None.
460
461  <Returns>
462    HTML for a complete table created from the array.
463   
464  """
465
466  # get the headers
467  table_headers = table_array[0]
468 
469  # create the table
470  html_table = create_table()
471 
472  # create the column headers
473  html_table += add_html_column_names(table_headers)
474
475  # for the length of the table (-1 is for the 0th index which is the headers)
476  for i in range(len(table_array) - 1):
477    # (+1 is to offset for the column headers @ index 0)
478    html_table += add_row_of_elements(table_array[i+1])
479
480  # close the table
481  html_table += close_table()
482 
483  return html_table
484
485 
486
487def html_write(html_fn, html_to_write):
488  """
489  <Purpose>
490    Appends (or creates) to html_fn the html_to_write.
491     
492  <Arguments>
493    html_fn:
494      the name of the html file to be written to (will append)
495    html_to_write:
496      The HTML that'll be appended
497   
498  <Exceptions>
499    File reading/writing related exceptions.
500
501  <Side Effects>
502    None.
503
504  <Returns>
505    None.
506  """
507 
508  try:
509    # get a file handle to the html_file
510    html_file_handle = open(html_fn, 'a')
511   
512    # opens up master.log and appends the file name of this file to it
513    master_handle = open('./master.log', 'a')
514    master_handle.write('\n'+html_fn)
515    master_handle.close()
516   
517    # writes the html file.
518    html_file_handle.write(start_html())
519    html_file_handle.write(html_to_write)
520    html_file_handle.write(close_html())
521   
522    html_file_handle.close()
523   
524  except Exception, e:
525    print 'Exception in html_write'
526    print e
Note: See TracBrowser for help on using the browser.