Mercurial > repos > melissacline > ucsc_cancer_utilities
comparison synapseGetDataset.py @ 1:ae91153d3fc2
Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive.
author | melissacline |
---|---|
date | Tue, 27 Jan 2015 16:49:56 -0800 |
parents | 60efb9214eaa |
children | d1104ad3646a |
comparison
equal
deleted
inserted
replaced
0:60efb9214eaa | 1:ae91153d3fc2 |
---|---|
3 | 3 |
4 import argparse | 4 import argparse |
5 import json | 5 import json |
6 import synapseclient | 6 import synapseclient |
7 import sys | 7 import sys |
8 import zipfile | |
9 | |
10 class InputError(Exception): | |
11 def __init__(self, value): | |
12 self.value = value | |
13 def __str__(self): | |
14 return repr(self.value) | |
15 | |
8 | 16 |
9 def saveMetadata(entity, metadataPathname): | 17 def saveMetadata(entity, metadataPathname): |
10 fp = open(metadataPathname, "w") | 18 fp = open(metadataPathname, "w") |
11 entityMetadata = dict(entity.properties.items() | 19 entityMetadata = dict(entity.properties.items() |
12 + entity.annotations.items()) | 20 + entity.annotations.items()) |
13 jsonMetadata = json.dumps(entityMetadata) | 21 jsonMetadata = json.dumps(entityMetadata) |
14 fp.write("%s\n" % (jsonMetadata)) | 22 fp.write("%s\n" % (jsonMetadata)) |
15 fp.close() | 23 fp.close() |
16 | 24 |
17 def saveData(entity, dataPathname): | 25 def saveData(entity, dataPathname): |
18 fpIn = open(entity.path) | 26 if entity.properties['contentType'] == "application/zip": |
19 fpOut = open(dataPathname, "w") | 27 zf = zipfile.ZipFile(entity.path) |
20 for row in fpIn: | 28 if len(zf.namelist()) > 1: |
21 fpOut.write(row) | 29 raise InputError(len(zf.namelist())), "Error: more than one input file" |
22 fpIn.close() | 30 else: |
23 fpOut.close() | 31 data = zf.read(zf.namelist()[0]) |
32 fpOut = open(dataPathname, "w") | |
33 fpOut.write(data) | |
34 fpOut.close() | |
35 else: | |
36 fpIn = open(entity.path) | |
37 fpOut = open(dataPathname, "w") | |
38 for row in fpIn: | |
39 fpOut.write(row) | |
40 fpIn.close() | |
41 fpOut.close() | |
24 | 42 |
25 def main(): | 43 def main(): |
26 parser = argparse.ArgumentParser() | 44 parser = argparse.ArgumentParser() |
27 parser.add_argument("entityId", type=str) | 45 parser.add_argument("entityId", type=str) |
28 parser.add_argument("email", type=str) | 46 parser.add_argument("email", type=str) |