|
0
|
1 import sys
|
|
|
2 sys.path.insert(0, '/home/saket/requests-new-urllib3-api/requests/packages/')
|
|
|
3 sys.path.insert(0, '/home/saket/requests-new-urllib3-api')
|
|
|
4
|
|
|
5
|
|
|
6 import requests
|
|
|
7 from functools import wraps
|
|
|
8 import tempfile, shutil,time
|
|
|
9 import os,csv
|
|
|
10 #__url__="http://mutationassessor.org/?cm=var&var=%s&frm=txt&fts=all"
|
|
|
11 __url__="http://mutationassessor.org"
|
|
|
12 def stop_err( msg ):
|
|
|
13 sys.stderr.write( '%s\n' % msg )
|
|
|
14 sys.exit()
|
|
|
15
|
|
|
16 def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
|
|
|
17
|
|
|
18 """Retry calling the decorated function using an exponential backoff.
|
|
|
19
|
|
|
20 http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
|
|
|
21 original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
|
|
|
22
|
|
|
23 :param ExceptionToCheck: the exception to check. may be a tuple of
|
|
|
24 exceptions to check
|
|
|
25 :type ExceptionToCheck: Exception or tuple
|
|
|
26 :param tries: number of times to try (not retry) before giving up
|
|
|
27 :type tries: int
|
|
|
28 :param delay: initial delay between retries in seconds
|
|
|
29 :type delay: int
|
|
|
30 :param backoff: backoff multiplier e.g. value of 2 will double the delay
|
|
|
31 each retry
|
|
|
32 :type backoff: int
|
|
|
33 :param logger: logger to use. If None, print
|
|
|
34 :type logger: logging.Logger instance
|
|
|
35 """
|
|
|
36 def deco_retry(f):
|
|
|
37
|
|
|
38 @wraps(f)
|
|
|
39
|
|
|
40 def f_retry(*args, **kwargs):
|
|
|
41 mtries, mdelay = tries, delay
|
|
|
42 while mtries > 1:
|
|
|
43 try:
|
|
|
44 return f(*args, **kwargs)
|
|
|
45 except ExceptionToCheck, e:
|
|
|
46 #msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
|
|
|
47 msg = "Retrying in %d seconds..." % (mdelay)
|
|
|
48 if logger:
|
|
|
49 logger.warning(msg)
|
|
|
50 else:
|
|
|
51 #print msg
|
|
|
52 pass
|
|
|
53 time.sleep(mdelay)
|
|
|
54 mtries -= 1
|
|
|
55 mdelay *= backoff
|
|
|
56 return f(*args, **kwargs)
|
|
|
57
|
|
|
58 return f_retry # true decorator
|
|
|
59
|
|
|
60 return deco_retry
|
|
|
61 def main(params):
|
|
|
62 with open(params[0],"r") as f:
|
|
|
63 for i,sequence in enumerate(f):
|
|
|
64 #print sequence
|
|
|
65 sequence=sequence.replace("\t",",").replace(" ",",")
|
|
|
66 call=requests.get((__url__)%(sequence))
|
|
|
67 with open(params[1],"a") as w:
|
|
|
68 text = call.content.replace("\\t","\t")
|
|
|
69 if i>1:
|
|
|
70 text_split=text.split("\n")
|
|
|
71 text="\t".join(text_split[1])
|
|
|
72 w.write(text)
|
|
|
73 time.sleep(1)
|
|
|
74 return True
|
|
|
75
|
|
|
76 def main_web(params):
|
|
|
77 tmp_dir = tempfile.mkdtemp()
|
|
|
78 path = os.path.join(tmp_dir,"csv_file")
|
|
|
79 in_txt = csv.reader(open(params[0],"rb"), delimiter="\t")
|
|
|
80 with open(path,"wb") as fp:
|
|
|
81 out_csv = csv.writer(fp,delimiter=",")
|
|
|
82 out_csv.writerows(in_txt)
|
|
|
83 fh = open(path,"rb")
|
|
|
84 readfile=fh.read()
|
|
|
85 fh.close()
|
|
|
86 payload = {"vars":readfile,"tableQ":"","protres":""}
|
|
|
87 request = requests.post(__url__,data=payload)
|
|
|
88 response = request.text
|
|
|
89 temp_file = os.path.join(tmp_dir,"int_file")
|
|
|
90 with open(temp_file,"wb") as w:
|
|
|
91 w.write(response)
|
|
|
92
|
|
|
93 in_txt = csv.reader(open(temp_file,"rb"), delimiter=",")
|
|
|
94 with open(params[1],"wb") as fp:
|
|
|
95 out_csv = csv.writer(fp,delimiter="\t")
|
|
|
96 out_csv.writerows(in_txt)
|
|
|
97 shutil.rmtree(tmp_dir)
|
|
|
98 return True
|
|
|
99 if __name__=="__main__":
|
|
|
100 main_web(sys.argv[1:])
|