changeset 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
files synapseGetDataset.py
diffstat 1 files changed, 24 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/synapseGetDataset.py	Wed Jan 14 13:54:03 2015 -0500
+++ b/synapseGetDataset.py	Tue Jan 27 16:49:56 2015 -0800
@@ -5,6 +5,14 @@
 import json
 import synapseclient
 import sys
+import zipfile
+
+class InputError(Exception):
+    def __init__(self, value):
+        self.value = value
+    def __str__(self):
+        return repr(self.value)
+
 
 def saveMetadata(entity, metadataPathname):
     fp = open(metadataPathname, "w")
@@ -15,12 +23,22 @@
     fp.close()
 
 def saveData(entity, dataPathname):
-    fpIn = open(entity.path)
-    fpOut = open(dataPathname, "w")
-    for row in fpIn:
-        fpOut.write(row)
-    fpIn.close()
-    fpOut.close()
+    if entity.properties['contentType'] == "application/zip":
+        zf = zipfile.ZipFile(entity.path)
+        if len(zf.namelist()) > 1:
+            raise InputError(len(zf.namelist())), "Error: more than one input file"
+        else:
+            data = zf.read(zf.namelist()[0])
+            fpOut = open(dataPathname, "w")
+            fpOut.write(data)
+            fpOut.close()
+    else:
+        fpIn = open(entity.path)
+        fpOut = open(dataPathname, "w")
+        for row in fpIn:
+            fpOut.write(row)
+        fpIn.close()
+        fpOut.close()
 
 def main():
     parser = argparse.ArgumentParser()