F:/thesis_austausch/dissertation/code_docu_doxygen/Visualiser/gui_utils.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 '''
00004 This module provides small utility methods that are used by the gui_classes.py and gui.py.
00005 
00006 Reading University
00007 MSc in Network Centered Computing
00008 a.weise - a.weise@reading.ac.uk - December 2005
00009 '''
00010 import os, time, ConfigParser, string
00011 import gui_classes
00012 import calendar
00013 
00014 def LoadConfig(file_name, config={}):
00015     """
00016     returns a dictionary with key's of the form
00017     <section>.<option> and the values
00018     
00019     source: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65334
00020     """
00021     config = config.copy()
00022     cp = ConfigParser.ConfigParser()
00023     cp.read(file_name)
00024     for sec in cp.sections():
00025         name = string.lower(sec)
00026         for opt in cp.options(sec):
00027             config[name + "." + string.lower(opt)] = string.strip(cp.get(sec, opt))
00028     return config
00029 
00030 def usage_exit(progname, msg = None, color = 0):
00031     '''
00032     This function gives usage help and exits script.
00033     '''
00034     if msg:
00035         if 1 == color and msg != None:
00036             color_obj = gui_classes.Colour()
00037             print color_obj.red(msg)
00038         else:
00039             print msg
00040         print # lf cr
00041     
00042     text = "usage: python %s -c config_file [optional commands] \n\n" % progname
00043     if 1 == color:
00044         print color_obj.red(text)
00045     else:
00046         print text
00047     os._exit(-1)
00048 
00049 def check_time(timus):
00050     '''
00051     This functions checks if a given time with the format hour:minute:second (12:45:46) is valid.
00052     '''
00053     timus = timus.split(':')
00054     if int(timus[0]) < 24 and int(timus[1]) < 60 and int(timus[2]) < 60:
00055         return 0
00056     else:
00057         return -1
00058 
00059 def check_date(datus):
00060     '''
00061     This function checks if a given date is valid.
00062     '''
00063     datus = datus.split(".")
00064     tup1 = (int(datus[2]), int(datus[1]), int(datus[0]), 0, 0, 0, 0, 0, 0)
00065     try:
00066         date = time.mktime (tup1)
00067         tup2 = time.localtime (date)
00068         if tup1[:2] != tup2[:2]:
00069             return -1
00070         else:
00071             return 0
00072     except OverflowError:
00073         return -1
00074     except ValueError:
00075         return -1
00076     
00077 def convert_date(datus):
00078     '''
00079     This function converts a date like 01.10.2005 into database conform date like 2005-10-01.
00080     '''
00081     datus = datus.split(".")
00082     return "%s-%s-%s" % (datus[2], datus[1], datus[0])
00083     
00084 def convert_date_readable(datus):
00085     '''
00086     This function converts a date like 2005-10-10 into are readable format 01.10.2005.
00087     '''
00088     datus = datus.split("-")
00089     return "%s.%s.%s" % (datus[2], datus[1], datus[0])    
00090     
00091 def check_ip(ip):
00092     '''
00093     This function checks if a given IP is valid.
00094     '''
00095     try:
00096         ip = ip.split(".")
00097     except AttributeError:
00098         return -1
00099     
00100     for i in range(len(ip)):
00101         check =  ip[i].find("0", 0, 1)
00102         if -1 != check and 1 < len(ip[i]):
00103             return -1
00104         try:
00105             ip[i] = int(ip[i])
00106         except ValueError:
00107             return -1
00108         if ip[i] >= 0 and ip[i] <= 255:
00109             pass
00110         else:
00111             return -1
00112         
00113     return 0
00114 
00115 def find_item(search, listus):
00116     '''
00117     This function find an item within a list (2 dimensional)
00118     '''
00119     for i in range(len(listus)):
00120         if 1 == len(listus[i]):
00121             if listus[i] == search:
00122                 return i
00123         elif 2 == len(listus[i]):
00124             if listus[i][0] == search:
00125                 return i
00126         elif 3 == len(listus[i]):
00127             if listus[i][0][0] == search:
00128                 return i
00129     return None
00130 
00131 def help_context(color):
00132     '''
00133     This function provides the help context.
00134     '''
00135     
00136     color_obj = gui_classes.Colour()
00137     msg = ''
00138     if color == 1:
00139         msg += color_obj.green("\n-----------   Help   ----------\n\n\n")
00140     else:
00141         msg += "\n-----------   Help   ----------\n\n\n"
00142         
00143     note = "PLEASE NOTE, if you give parameter values, please do not enter characters like \" \" (space) or \"!\", because this could be characters which are interpreted by the terminal. If you have to enter such characters, please escape them like \"\!\".\n\n"
00144     if color == 1:
00145         msg += color_obj.purple(note)
00146     else:
00147         msg += note
00148         
00149     msg += "-c or --config\t\t\t-> defines config file, if no config file given, default values are used\n"\
00150         "-v or --verbose\t\t\t-> activates printing of messages [debug option]\n"\
00151         "-h or --help\t\t\t-> print this help\n"\
00152         "-g or --graph\t\t\t-> show output additionally as a diagram\n"\
00153         "--nocolor\t\t\t-> no colored console output\n"\
00154         "--file <string>\t\t\t-> dump output into a file (file name has to be given)\n"
00155   
00156     if color == 1:
00157         msg += color_obj.green("\n-----  database commands  -----\n\n")
00158     else:
00159         msg += "\n-----  database commands  -----\n\n"
00160     msg += "--sql_host\t\t\t-> show all hosts\n"\
00161         "--sql_project\t\t\t-> show all projects\n"\
00162         "--sql_error\t\t\t-> show errors (additional parameters possible)\n"\
00163         "--sql_error_freq\t\t-> show only frequency of errors (additional parameters possible)\n"
00164     if color == 1:
00165         msg += color_obj.green("\n-----  additional parameters  -----\n")
00166     else:
00167         msg += "\n-----  additional parameters  -----\n"
00168     msg += "\n--start_date <date>\t\t-> start date (e.g. 23.12.2005)\n"\
00169         "--end_date <date>\t\t-> end date (e.g. 23.01.2006)\n"\
00170         "--start_time <time>\t\t-> start time (e.g. 23:12:19)\n"\
00171         "--end_time <time>\t\t-> end time (e.g. 23:12:59)\n"\
00172         "--ip <ip>\t\t\t-> host IP (e.g. 127.0.0.1)\n"\
00173         "--project <string>\t\t-> specify a certain project\n"\
00174         "--error <int,int...>\t\t-> specify a certain error (comma seperated list)\n"
00175     if color == 1:
00176         msg += color_obj.green("\n-----  examples  -----\n\n")
00177         msg += color_obj.blue("python gui.py -c config_gui.ini --sql_project\n")
00178         msg += color_obj.yellow("\t-> show all projects\n\n")
00179         
00180         msg += color_obj.blue("python gui.py -c config_gui.ini --sql_host\n")
00181         msg += color_obj.yellow("\t-> show all host and the corresponding project\n\n")        
00182         
00183         msg += color_obj.blue("python gui.py -c config_gui.ini --sql_error --start_date 01.01.2005 --end_date 01.03.2005 --ip 127.0.0.1\n")
00184         msg += color_obj.yellow("\t-> show all errors of localhost between 01.01.2005 and 01.03.2005\n\n")
00185         
00186         msg += color_obj.blue("python gui.py -c config_gui.ini --sql_error --start_date 01.01.2005  --project mySRBproject\n")
00187         msg += color_obj.yellow("\t-> show all errors between 01.01.2005 and now for the project \"mySRBproject\"\n\n")
00188 
00189         msg += color_obj.blue("python gui.py -c config_gui.ini --sql_error --start_date 22.10.2005 --end_date 22.10.2005\n--start_time 12:00:00 --end_time 18:00:00  --ip 127.0.0.1 --file test.txt\n")
00190         msg += color_obj.yellow("\t-> show all errors on the 22.10.2005 between 12 h and 18 h on localhost and\n\t   save output in file \"test.txt\"\n\n")        
00191         
00192         msg += color_obj.blue("python gui.py -c config_gui.ini --sql_error_freq --error -1023 --ip 127.0.0.1 -g \n")
00193         msg += color_obj.yellow("\t-> show error frequency for the error -1023 from host 127.0.0.1 and display diagram\n\n")
00194         
00195 
00196     else:
00197         msg += "\n-----  examples  -----\n\n"
00198         msg += "python gui.py -c config_gui.ini --sql_project\n"
00199         msg += "\t-> show all projects\n\n"
00200         
00201         msg += "python gui.py -c config_gui.ini --sql_host\n"
00202         msg += "\t-> show all host and the corresponding project\n\n"
00203         
00204         msg += "python gui.py -c config_gui.ini --sql_error --start_date 01.01.2005 --end_date 01.03.2005 --ip 127.0.0.1\n\t-> show all errors of localhost between 01.01.2005 and 01.03.2005\n\n"
00205         
00206         msg += "python gui.py -c config_gui.ini --sql_error --start_date 01.01.2005  --project mySRBproject\n"
00207         msg += "\t-> show all errors between 01.01.2005 and now for the project \"mySRBproject\"\n\n"
00208 
00209         msg += "python gui.py -c config_gui.ini --sql_error --start_date 22.10.2005 --end_date 22.10.2005\n--start_time 12:00:00 --end_time 18:00:00  --ip 127.0.0.1 --file test.txt\n"
00210         msg += "\t-> show all errors on the 22.10.2005 between 12 h and 18 h on localhost and\n\t   save output in file \"test.txt\"\n\n"
00211         
00212         msg += "python gui.py -c config_gui.ini --sql_error_freq --error -1023 --ip 127.0.0.1 -g \n"
00213         msg += "\t-> show error frequency for the error -1023 from host 127.0.0.1 and display diagram\n\n"
00214         
00215     msg += "\n"
00216     
00217     return msg
00218 
00219 def complete_hours(label, field):
00220     '''
00221     This function completes the missing hours within an array
00222     '''
00223     new_hours = []
00224     new_values = []
00225         
00226     count = 0
00227     for i in range(len(label)):
00228         temp = "%d" % count
00229         while(count < 24):
00230             if temp == label[i][1]:
00231                 break
00232             new_hours.append([count, temp])
00233             new_values.append([count, 0])
00234             count += 1
00235             temp = "%d" % count
00236             
00237         new_hours.append([count, label[i][1]])
00238         new_values.append([count, field[i][1]])
00239             
00240         count += 1
00241     
00242     # last hours
00243     while(count <= 23):
00244         temp = "%d" % count
00245         new_hours.append([count, temp])
00246         new_values.append([count, 0])
00247         count += 1        
00248     
00249     return new_hours, new_values
00250 
00251 def complete_days(days, value_field):
00252     '''
00253     This function completes the missing dates within an array.
00254     '''
00255     if len(days) == 1:
00256         # if only one day in the field
00257         return days, value_field
00258     
00259     new_days = [] # new array with the completed days
00260     new_values = []
00261     
00262     for i in range(len(days)):
00263         day1 = days[i][1].split("-")
00264         day2 = days[i+1][1].split("-")
00265 
00266         for x in range(len(day1)):
00267             day1[x] = int(day1[x])
00268             day2[x] = int(day2[x])
00269         
00270         if day1[0] == day2[0] and day1[1] == day2[1] and (day1[2]+1) == day2[2]:
00271             # save day1 in new array
00272             temp1 = "%d" % day1[2]
00273             temp2 = "%d" % day1[1]
00274             date = "%d-" % day1[0]
00275             if len(temp2) == 1:
00276                 date += "0%d-" % day1[1]
00277             else:
00278                 date += "%d-" % day1[1]
00279                 
00280             if len(temp1) == 1:
00281                 date += "0%d" % (day1[2])
00282             else:
00283                 date += "%d" % (day1[2])
00284                 
00285             if len(new_days) == 0:
00286                 number = 1
00287             else: 
00288                 number = int(new_days[len(new_days)-1][0])+1
00289             new_days.append([number, date])
00290             new_values.append([number, value_field[i][1]])
00291         else:
00292             # not the following day
00293             if day1[0] == day2[0] and day1[1] == day2[1]:
00294                 #year and month the same
00295                 new_days, new_values = complete_d(new_days, day1, day2, new_values, value_field[i][1])
00296 
00297             elif day1[0] == day2[0]:
00298                 # year the same
00299                 new_days, new_values = complete_m(new_days, day1, day2, new_values, value_field[i][1])
00300                 
00301             else:
00302                 # year change
00303                 new_days, new_values = complete_y(new_days, day1, day2, new_values, value_field[i][1])                    
00304 
00305         if (i+2) == len(days):
00306             break
00307         
00308     # add last date
00309     temp1 = "%d" % day2[2]
00310     temp2 = "%d" % day2[1]
00311     date = "%d-" % day2[0]
00312     
00313     if len(temp2) == 1:
00314         date += "0%d-" % day2[1]
00315     else:
00316         date += "%d-" % day2[1]
00317         
00318     if len(temp1) == 1:
00319         date += "0%d" % day2[2]
00320     else:
00321         date += "%d" % day2[2]
00322     
00323     if len(new_days) == 0:
00324         number = 1
00325     else: 
00326         number = int(new_days[len(new_days)-1][0])+1
00327     new_days.append([number, date])
00328     new_values.append([number, value_field[i+1][1]])
00329         
00330     return new_days, new_values
00331     
00332 def complete_d(new_array, start_date, end_date, new_field, value):
00333     '''
00334     Add missing dates within a month
00335     '''
00336     
00337     month = calendar.monthcalendar(start_date[0], start_date[1])
00338     # run through matrix and save all dates between day1 and day2 in new_days array
00339     found = 0
00340     terminate = 0
00341     for x in range(len(month)):
00342         if terminate != 0:
00343             break
00344         for y in range(len(month[x])):
00345             # go to current day1
00346             if terminate != 0:
00347                 break
00348             if start_date[2] == month[x][y] and found == 0:
00349                 #save date1 in new_days
00350                 temp1 = "%d" % start_date[2]
00351                 temp2 = "%d" % start_date[1]
00352                 
00353                 date = "%d-" % start_date[0]
00354                 
00355                 if len(temp2) == 1:
00356                     date += "0%d-" % start_date[1]
00357                 else:
00358                     date += "%d-" % start_date[1]
00359                     
00360                 if len(temp1) == 1:
00361                     date += "0%d" % start_date[2]
00362                 else:
00363                     date += "%d" % start_date[2]
00364                 
00365                 if (0 < len(new_array)):
00366                     number = int(new_array[len(new_array)-1][0])+1
00367                 else:
00368                     number = 0
00369                 new_array.append([number, date])
00370                 new_field.append([number, value])
00371                 
00372                 found = 1
00373             elif found == 1:
00374                 # add new dates
00375                 if end_date[2] == month[x][y]:
00376                     terminate = 1
00377                 else:
00378                     # save dates
00379                     temp1 = "%d" %  month[x][y]
00380                     temp2 = "%d" % end_date[1]
00381                     
00382                     date = "%d-" % end_date[0]
00383                     
00384                     if len(temp2) == 1:
00385                         date += "0%d-" % end_date[1]
00386                     else:
00387                         date += "%d-" % end_date[1]
00388                         
00389                     if len(temp1) == 1:
00390                         date += "0%d" %  month[x][y]
00391                     else:
00392                         date += "%d" %  month[x][y]
00393                     
00394                     # get next entry number in arrays
00395                     if (0 < len(new_array)):
00396                         number = int(new_array[len(new_array)-1][0])+1
00397                     else:
00398                         number = 0
00399                     new_array.append([number, date])
00400                     new_field.append([number, 0])
00401             else:
00402                 pass
00403      
00404     return new_array, new_field
00405 
00406 def complete_m(new_array, start_date, end_date, new_field, value):
00407     '''
00408     This function adds missing dates within a year.
00409     '''
00410     start_month = start_date[1]
00411     end_month = end_date[1]
00412     
00413     #current month
00414     month = calendar.monthrange(start_date[0], start_date[1])
00415     temp_date2 = [start_date[0], start_date[1], month[1]]
00416     
00417     new_array, new_field = complete_d(new_array, start_date, temp_date2, new_field, value)
00418       
00419     start_month += 1
00420     
00421     # month in between
00422     while(start_month < end_month):
00423         
00424         month = calendar.monthrange(start_date[0], start_month)
00425         temp_date2 = [start_date[0], start_month, month[1]]
00426         temp_date1 = [start_date[0], start_month, 1]
00427         
00428         new_array, new_field = complete_d(new_array, temp_date1, temp_date2, new_field, 0)
00429         
00430         start_month += 1
00431 
00432     # last month
00433     temp_date1 = [start_date[0], start_month, 1]
00434     
00435     new_array, new_field = complete_d(new_array, temp_date1, end_date, new_field, 0)
00436       
00437     return new_array, new_field
00438 
00439 def complete_y(new_array, start_date, end_date, new_field, value):
00440     '''
00441     This function adds missing dates within many years
00442     '''
00443     start_year = start_date[0]
00444     end_year = end_date[0]
00445 
00446     # current year
00447     temp_date2 = [start_date[0], 12, 31]
00448     new_array, new_field = complete_m(new_array, start_date, temp_date2, new_field, value)
00449 
00450     start_year += 1
00451     
00452     # years in between
00453     while(start_year < end_year):
00454         
00455         temp_date1 = [start_year, 1, 1]
00456         temp_date2 = [start_year, 12, 31]
00457         new_array, new_field = complete_m(new_array, temp_date1, temp_date2, new_field, 0)
00458         start_year += 1
00459 
00460     # last year
00461     temp_date1 = [start_year, 1, 1]
00462     new_array, new_field = complete_m(new_array, temp_date1, end_date, new_field, 0)
00463     
00464     return new_array, new_field    
00465     
00466 def complete_ticks(label, values):
00467     '''
00468     This function adds bins, so that the dot in the time diagram are between two hours.
00469     '''
00470     new_label = []
00471     new_values = []
00472     
00473     count = 0
00474     for i in range(2*len(label)):
00475         if (i%2) != 0:
00476             new_values.append([i, values[count][1]])
00477             new_label.append([i, ""])
00478             count += 1
00479         else:
00480             new_label.append([i, label[count][1]])
00481 
00482     return new_label, new_values
00483 
00484 def second(t1, t2 ):
00485     '''
00486     This function works with sort and the field gets sorted descending, but the second value within the array is taking into account !!!
00487     '''
00488     # sort descending
00489     return t2[1] - t1[1]
00490 
00491 def second_string_to_int(t1, t2):
00492     '''
00493     This function works with sort and the field gets sorted ascending, but the second value within the array is taking into account !!! (The values to be sort are number as strings.)
00494     '''
00495     # sort ascending  
00496     return int(t1[1]) - int(t2[1])
00497 
00498 def second_string_only(t1, t2):
00499     '''
00500     This function works with sort and the field gets sorted ascending, but the second value within the array is taking into account !!! (The values to be sort are strings.)
00501     '''        
00502     # sort ascending
00503     return cmp(t1[1], t2[1])

Generated on Sun Mar 5 23:04:01 2006 for Virtualiser by  doxygen 1.4.6-NO