Mercurial > repos > melissacline > ucsc_xena_platform
comparison xenaGetDataset.py @ 52:bb840cc2603d
better error handling
author | jingchunzhu |
---|---|
date | Thu, 13 Aug 2015 17:30:19 -0700 |
parents | 3167c1a26101 |
children |
comparison
equal
deleted
inserted
replaced
51:47926759295a | 52:bb840cc2603d |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 | 2 |
3 import argparse | 3 import argparse |
4 import re | 4 import re |
5 import urllib2 | 5 import urllib2 |
6 | 6 import sys |
7 | 7 |
8 def main(): | 8 def main(): |
9 parser = argparse.ArgumentParser() | 9 try: |
10 parser.add_argument("dataHub", type=str) | 10 parser = argparse.ArgumentParser() |
11 parser.add_argument("datasetId", type=str) | 11 parser.add_argument("dataHub", type=str) |
12 parser.add_argument("datafile", type=str) | 12 parser.add_argument("datasetId", type=str) |
13 args = parser.parse_args() | 13 parser.add_argument("datafile", type=str) |
14 parser.add_argument("GALAXY_URL", type=str) | |
15 args = parser.parse_args() | |
16 except: | |
17 print "too few input parameters" | |
18 sys.exit(2) | |
14 | 19 |
20 data = open(args.datafile, "w") | |
15 datasetUrlHost = re.sub("/proj/", "/download/", args.dataHub) | 21 datasetUrlHost = re.sub("/proj/", "/download/", args.dataHub) |
16 datasetIdTokens = re.split("/", args.datasetId) | 22 datasetIdTokens = re.split("/", args.datasetId) |
17 datasetUrl = datasetUrlHost + "/" + "/".join(datasetIdTokens[1:]) | 23 datasetUrl = datasetUrlHost + "/" + "/".join(datasetIdTokens[1:]) |
18 | 24 |
19 data = open(args.datafile, "w") | |
20 | 25 |
21 try: | 26 try: |
22 dd = urllib2.urlopen(datasetUrl) | 27 dd = urllib2.urlopen(datasetUrl) |
23 data.write(dd.read()) | 28 data.write(dd.read()) |
24 dd.close() | 29 dd.close() |
25 data.close() | 30 data.close() |
26 except: | 31 except: |
27 data.wriet("You entered dataset id: %s\n" % (args.datasetId)) | 32 data.write("There is an error\n\n") |
28 data.write("Dataset does not exist. check typo.") | 33 data.write("You entered dataset id: %s\n" % (args.datasetId)) |
34 data.write("Possible causes for error:\n") | |
35 data.write("1. Dataset does not exist. check type.\n") | |
29 data.close() | 36 data.close() |
30 sys.exit(1) | 37 sys.exit(1) |
31 | 38 |
32 if __name__ == "__main__": | 39 if __name__ == "__main__": |
33 main() | 40 main() |