# HG changeset patch # User melissacline # Date 1422406196 28800 # Node ID ae91153d3fc23ac32f5f114adea8e1447b7794a5 # Parent 60efb9214eaa020c5870f51b7e66efafeb0eb10a Updated synapseGetDataset.py to automatically unzip if the file downloaded is a zip archive. diff -r 60efb9214eaa -r ae91153d3fc2 synapseGetDataset.py --- 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()