#(C)2007 Twm Davies import urllib import re import os import pickle import sys temp = "c:\\temp\\" picklefilename = os.path.join(temp, "kerr_lookupdata.txt") try: os.mkdir(temp) except: pass # common variables def extractErrorInfo(matchstr): rawstr = r"""\s*(.*)\s*\s*\s*(.*)\s*\s*\s*(.*)\s*\s*""" rawstr2 = r"""\s*(.*)\s*\s*\s*(.*)\s*\s*.*""" rawstr3 = r"""\s*(\w*)\s*.*\s*([-0-9]*)\s*""" # method 1: using a compile object compile_obj = re.compile(rawstr) match_obj = compile_obj.search(matchstr) if(match_obj == None): compile_obj2 = re.compile(rawstr2) match_obj = compile_obj2.search(matchstr) if(match_obj == None): compile_obj3 = re.compile(rawstr3) match_obj = compile_obj3.search(matchstr) if(match_obj): # Retrieve group(s) from match_obj all_groups = match_obj.groups() # Retrieve group(s) by index group_1 = match_obj.group(1) group_2 = match_obj.group(2) try: poo = group_3 = match_obj.group(3) except IndexError: return (group_1.strip(), group_2.strip(), "") else: return (group_1.strip(), group_2.strip(), group_3.strip()) else: return None def extractcodes(line): fields = extractErrorInfo(line) if(fields): params = list(extractErrorInfo(line)) params[1] = params[1].replace("--","-") params[0] = params[0].replace("\"","") params[0] = params[0].replace("\'","") params[0] = params[0].replace("\\","") params[2] = params[2].replace("\"","") params[2] = params[2].replace("\'","") params[2] = params[2].replace("\\","") try: params[1] = int(params[1]) except ValueError: return None return params return None def test(): tcs = [""" KErrNotFound -1Unable to find the specified object""", """KErrEtelNotCallOwner -2000 """, """ KErrAvctpBadAddress -6400 AVCTP bad address """, """ KErrAvctpBadAddress --6400 AVCTP bad address """, """ KErrAvctpBadAddress MOO AVCTP bad address """] for testcase in tcs: print extractcodes(testcase) def regenerateErrorFile(): file = urllib.urlopen("http://newlc.com/Symbian-OS-Error-Codes.html") lines = file.readlines() lookupcode = -5 look_up = {} for line in lines: fields = extractcodes(line) if(fields): look_up[fields[1]]= (fields[0],fields[2]) output = open(picklefilename,"wb") pickle.dump(look_up, output) return look_up #test() try: pkl_file = open(picklefilename, 'rb') lookup = pickle.load(pkl_file) pkl_file.close() except IOError: print "Re-Generating file" lookup = regenerateErrorFile() args = sys.argv if(len(args)!=2): print "Usage kerr " print "e.g kerr -1" sys.exit() try: errtupe = lookup[int(args[1])] print "%s [%s]"% errtupe except: print "Unknown error"