Sunday, October 28, 2007

Get a Error Code with Python Script.

The code below Download error code reference from NewLC and stores the error code reference to a c:/temp folder. Forgetting error meaning just pass error code as argument like below.

python geterr -11


The Script is as below


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"""<tr class="\D*"><td>\s*<strong class="\D*">(.*)</strong>\s*</td>\s*<td>\s*(.*)\s*</td>\s*<td>\s*<i class="spip">(.*)</i>\s*</td>\s*</tr>"""
rawstr2 = r"""<tr class="\D*"><td>\s*<strong class="\D*">(.*)</strong>\s*</td>\s*<td>\s*(.*)\s*</td><td>\s*.*"""
rawstr3 = r"""<td>\s*(\w*)\s*</td>.*<td>\s*([-0-9]*)\s*</td>"""

# 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 = ["""<tr class="row_even"><td> <strong class="spip">KErrNotFound</strong> </td><td>-1</td><td><i class="spip">Unable to find the specified object</i></td></tr>""",
"""<tr class="row_odd"><td><strong class="spip">KErrEtelNotCallOwner</strong> </td><td>-2000</td><td> </td></tr>""",
"""<tr class="row_even"><td> KErrAvctpBadAddress</td><td> -6400 </td><td><i class="spip">AVCTP bad address </i> </td></tr>""",
"""<tr class="row_even"><td> KErrAvctpBadAddress</td><td> --6400 </td><td><i class="spip">AVCTP bad address </i> </td></tr>""",
"""<tr class="row_even"><td> KErrAvctpBadAddress</td><td> MOO </td><td><i class="spip">AVCTP bad address </i> </td></tr>"""]

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 <errorcode>"
print "e.g kerr -1"
sys.exit()

try:
errtupe = lookup[int(args[1])]
print "%s [%s]"% errtupe
except:
print "Unknown error"

No comments:

stats counter