F:/thesis_austausch/dissertation/code_docu_doxygen/GZ_Parser/utils_server.py

Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 '''
00004 This module provides basic utilities for the modules server_classes.py and start_server.py.
00005 
00006 Reading University
00007 MSc in Network Centered Computing
00008 a.weise - a.weise@reading.ac.uk - December 2005
00009 '''
00010 
00011 import ConfigParser, string
00012 import time, os
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 check_ip(ip):
00031     '''
00032     This function checks if a given IP is valid.
00033     '''
00034     try:
00035         ip = ip.split(".")
00036     except AttributeError:
00037         return -1
00038     
00039     for i in range(len(ip)):
00040         check =  ip[i].find("0", 0, 1)
00041         if -1 != check and 1 < len(ip[i]):
00042             return -1
00043         try:
00044             ip[i] = int(ip[i])
00045         except ValueError:
00046             return -1
00047         if ip[i] >= 0 and ip[i] <= 255:
00048             pass
00049         else:
00050             return -1
00051         
00052     return 0
00053 
00054 def get_keywords(filus):
00055     '''
00056     This function extracts keywords from a give file!
00057     '''
00058     keys = []
00059     
00060     try:
00061         file_fd = file(filus, 'r')
00062     except IOError, e:
00063         print "Problem with keyword file -> ",  e
00064         return -1
00065     
00066     content = file_fd.readlines()# save file content as list (1 line == 1 entry)
00067 
00068     file_fd.close()
00069 
00070     content = remove_item(content, "#")
00071     content = remove_item(content, "\n")
00072 
00073     for i in range(len(content)):
00074         content[i] = content[i].strip()
00075         content[i] = content[i].rstrip(",")
00076         content[i] = content[i].split(",")
00077         for a in range(len(content[i])):
00078             keys.append(content[i][a])
00079     
00080     for i in range(len(keys)):
00081         keys[i] = keys[i].strip() # remove whitespace
00082         keys[i] = keys[i].split(":")
00083         
00084     return keys
00085         
00086 def remove_item(listus, item):
00087     '''
00088     This function removes an item for a list (2 dimentional) as a rekursive function.
00089     '''
00090     
00091     while(1):
00092         
00093         for i in range(len(listus)):
00094             if -1 != listus[i].find(item, 0, 1):
00095                 del listus[i]
00096                 remove_item(listus, item)
00097                 break
00098         else:
00099             break
00100         
00101     return listus
00102 
00103 def list_to_string(listus):
00104     '''
00105     This function converts the keyword list (2 dimensional array) to a keyword string (keywords comma separated), so the string is writable into the keyword file.
00106     '''
00107     str_listus = ''
00108     
00109     for i in range(len(listus)):
00110         if 1 == len(listus[i]):
00111             str_listus += listus[i][0]+", "
00112         elif 2 == len(listus[i]):
00113             str_listus += listus[i][0]+":"+listus[i][1]+", "
00114             
00115     str_listus = str_listus.strip()
00116     str_listus = str_listus.strip(",")
00117     
00118     return str_listus
00119 
00120 def delete_file(file_name, verbose):
00121     '''
00122     This function deletes a file.
00123     '''
00124     try:
00125         os.remove(file_name)
00126         return 0
00127     except:
00128         if verbose == 1:
00129             print "%s -> could not delete -> \"%s\"" % (time.ctime(), file_name)
00130         return -1
00131     
00132 def usage_exit(progname, msg=None):
00133     '''
00134     This function diplays the usage of this program and terminates the program!
00135     '''
00136     if msg:
00137         print msg
00138         print
00139     print "usage: python %s [ -h|--help -c|--config -v|--verbose -d|--daemon] \n\n" % progname
00140     os._exit(-1)
00141     
00142 def find(search, listus):
00143     '''
00144     This function finds an item within a list (1 dimensional).
00145     '''
00146     for i in range(len(listus)):
00147         if listus[i] == search:
00148             return listus[i]
00149     return None 

Generated on Sun Mar 5 18:04:16 2006 for GZ Parser by  doxygen 1.4.6-NO