Author: jerome Date: 2009-03-19 13:52:39 +0100 (Thu, 19 Mar 2009) New Revision: 4150
Modified: software_suite_v2/libraries/python/webRadioDiscover/trunk/webRadioDiscover/webRadiosListDeployer.py Log: * Added ping functions to check web radios availability. * Added commands lines to generate a pickle list. * works on Linux and windblows. Modified: software_suite_v2/libraries/python/webRadioDiscover/trunk/webRadioDiscover/webRadiosListDeployer.py =================================================================== --- software_suite_v2/libraries/python/webRadioDiscover/trunk/webRadioDiscover/webRadiosListDeployer.py 2009-03-19 11:30:46 UTC (rev 4149) +++ software_suite_v2/libraries/python/webRadioDiscover/trunk/webRadioDiscover/webRadiosListDeployer.py 2009-03-19 12:52:39 UTC (rev 4150) @@ -7,8 +7,14 @@ # http://www.gnu.org/copyleft/gpl.html import pickle +import sys +import socket +import urllib2 from os import path +from string import replace +__version__ = "Jerome Conan" +__author__ = "0.1" class Generator(object): ''' @@ -16,37 +22,26 @@ @mail : [email protected] @version : 0.1 ''' - + __pickleFile = None __pickleStruct = {} + __pickleStructPath = "" def __init__(self, pickleStructPath): ''' Init radio list generator. ''' - #Check if one list has already been serialized. - if path.isfile(pickleStructPath): - self.__pickleFile = file(pickleStructPath, "r") - self.__pickleStruct = pickle.load(self.__pickleFile) - #opening the target file. - - else: - #no pickle struct was found, so create it. - self.__generateStruct(pickleStructPath) - - + self.__pickleStructPath = pickleStructPath - - def __generateStruct(self, path): + def __generateStruct(self, path, ping=False): ''' Generate pickle struct from registered web radios. ''' - dictGenerator = GeneratorDict(path) + dictGenerator = GeneratorDict(path, ping) self.__pickleStruct = dictGenerator.webRadios - print(self.__pickleStruct) @@ -56,6 +51,39 @@ ''' return self.__pickleStruct + + + def generate(self, Check=False): + ''' + Generate the pickle we radios struct. + ''' + #create it. + self.__generateStruct(self.__pickleStructPath, ping=Check) + + + + def load(self): + ''' + Load the current pickle struct. + ''' + #Check if one list has already been serialized. + if path.isfile(self.__pickleStructPath): + self.__pickleFile = file(self.__pickleStructPath, "r") + #Loading serialized object. + self.__pickleStruct = pickle.load(self.__pickleFile) + + + + def toPrettyFormat(self, dico): + ''' + Print a dictionnary in a pretty format. + ''' + keys = dico.keys() + if(len(keys) > 0): + for keyName in keys: + + print(" " + keyName + "\n\r " + dico[keyName]) + @@ -66,21 +94,60 @@ webRadios = {} storePath = "" + __ping = False - def __init__(self, storePath): + def __init__(self, storePath, ping): ''' Initialize dictionary. ''' self.storePath = file(storePath, "w") + self.__ping = ping + if self.storePath != None: ''' Then initialize object. ''' self.__getObjectAsDict() - + + if ping: + self.ping() + pickle.dump(self.webRadios, self.storePath) self.storePath.close() + + + + def ping(self): + ''' + Verify links integrity. + ''' + keys = self.webRadios.keys() + if(len(keys) > 0): + for radio in keys: + print("Checking: " + radio) + # Save the old default connection timeout + old_timeout = socket.getdefaulttimeout() + # Set the default connection timeout + socket.setdefaulttimeout(10.) + # Initialize the result dictionary + delete = False + # Attempt to connect to stringUrl + try: + f = urllib2.urlopen(self.webRadios[radio]) + f.close() + except urllib2.HTTPError, exc: + delete = True + except urllib2.URLError, exc: + delete = True + except: + delete = True + # Restore the old default connection timeout + socket.setdefaulttimeout(old_timeout) + # Return the result + if delete: + del self.webRadios[radio] + print("Deleted: " + radio) @@ -1275,7 +1342,124 @@ +def usage(): + ''' + ''' + print("\n*********************************************************") + print("** This script generate a serialized structure based **") + print("** on pickle object. The generated structure is a **") + print("** dictonnary containing some web radios and their url **") + print("** Author : " + __author__ + " **") + print("** Version: " + __version__ + " **") + print("*********************************************************") + print("\n") + print(" --help (-h) Dislay this help\n") + print(" --version (-v) Show current version\n") + print(" --generate-globals (-g) Generate a serialized web radios") + print(" object from internal script datas") + print(" (use --target to set the target path\n") + print(" --print-struct (-p) Print the current pickle struct object") + print(" (use --target to set the target path\n") + print(" --target (-t) Set the target path (for -g or -p options)") + print(" --ping (-pg) Check url integrity before create structure") + print(" (Used with --generate-struct)") + + +def overrideRequest(ping): + ''' + Ask for an override. + ''' + print("Warning: file exists, would you like to override it? y:n") + request = sys.stdin.readline() + request = replace(request, "\n", "") + + #Overriding existing file. + if request in ["y", "yes"]: + gen = Generator(target) + gen.generate(Check=ping) + + #Do not override, quit the script. + elif request in ["n", "no"]: + print("Quit script.") + else: + #Default entry for other inputs. + print("This input doesn't match any operation.") + + + if __name__ == "__main__": - Generator("webRadios.wrl") + + #Show usage. + if (len(sys.argv) < 2) or (sys.argv[1] in ["--help", "-h"]): + usage() + #Show version. + elif sys.argv[1] in ["--version", "-v"]: + print("Version: " + __version__) + + #Generate pickle struct. + elif sys.argv[1] in ["--generate-globals", "-g"]: + + if (len(sys.argv) > 2) and (sys.argv[2] in ["--target", "-t"]): + target = sys.argv[3] + + ping = False + #Check if ping urls is required. + if(len(sys.argv) > 4 and (sys.argv[4] in ["--ping", "-pg"])): + ping = True + + if path.isfile(target): + overrideRequest(ping) + #Target doesn't exists, start creating pickle struct. + else: + gen = Generator(target) + gen.generate(ping) + + else: + #--target was not specified, asking enter one. + print("Specify the target file:") + target = sys.stdin.readline() + target = replace(target, "\n", "") + + ping = False + #Check if ping is required. + if(len(sys.argv) > 3) and (sys.argv[3] in ["--ping", "-pg"]): + ping = True + + gen = None + if not (path.isfile(target)): + gen = Generator(target) + gen.generate(Check=ping) + else: + overrideRequest(Check=ping) + + + + elif sys.argv[1] in ["--print-struct","-p"]: + if (len(sys.argv) > 2) and (sys.argv[2] in ["--target", "-t"]): + target = sys.argv[3] + if(path.isfile(target)): + gen = Generator(target) + gen.load() + gen.toPrettyFormat(gen.getDict()) + else: + print("This file doesn't exists.") + + else: + print("Specify the target file:") + target = sys.stdin.readline() + target = replace(target, "\n", "") + if path.isfile(target): + gen = Generator(target) + gen.load() + gen.toPrettyFormat(gen.getDict()) + else: + print("This file doesn't exists") + + else: + if sys.argv[1] in ["--target", "-t"]: + print("Make use of --generate-globals OR print-struct first") + + print("No matching arguments.") + ------------------------------------------------------------------------------ Apps built with the Adobe(R) Flex(R) framework and Flex Builder(TM) are powering Web 2.0 with engaging, cross-platform capabilities. Quickly and easily build your RIAs with Flex Builder, the Eclipse(TM)based development software that enables intelligent coding and step-through debugging. Download the free 60 day trial. http://p.sf.net/sfu/www-adobe-com _______________________________________________ Tux-droid-svn mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/tux-droid-svn
