Skip to content

Commit

Permalink
Combine downloadDatasetFiles into the downloadDataset function
Browse files Browse the repository at this point in the history
  • Loading branch information
0leks committed Apr 29, 2020
1 parent cbb5292 commit 162df08
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 42 deletions.
10 changes: 2 additions & 8 deletions Documentation/Datasets.md
Expand Up @@ -40,18 +40,12 @@ When calling `DatasetUtils.downloadDataset('ellipsoid')`, you will get a zip fil

## API

### DatasetUtils.downloadDataset(datasetName, destinationPath='.', asZip = True)
### DatasetUtils.downloadDataset(datasetName, destinationPath='.', asZip = True, fileList = None)
- Parameters:
- **datasetName** is one of the names returned by `DatasetUtils.getDatasetList()`
- **destinationPath** is where the zip file or folder will go once it is downloaded
- **asZip** toggles whether to download as zip or download individual files
- Returns: True on success and False on failure

### DatasetUtils.downloadDatasetFiles(datasetName, fileList, destinationPath='.')
- Parameters:
- **datasetName** is one of the names returned by `DatasetUtils.getDatasetList()`
- **asZip** toggles whether to download as zip or download individual files. (providing a fileList disables this functionality)
- **fileList** is a list of files to download. Example for femur: `['images/m03_1x_hip.nrrd', 'distance_transforms/m03_L_femur.ply']`
- **destinationPath** is where the files will go once they are downloaded
- Returns: True on success and False on failure

### DatasetUtils.uploadNewDataset(datasetName, datasetPath)
Expand Down
32 changes: 0 additions & 32 deletions Python/DatasetUtilsPackage/DatasetUtils/GirderConnector.py
Expand Up @@ -127,37 +127,6 @@ def _downloadDatasetZip(accessToken, datasetName, destinationPath):
return success


apicall = serverAddress + "api/v1/item"
response = requests.get(url = apicall, params = {'folderId': '5e15245f0a02fb02ba24268a', 'name': filename}, headers = {'Girder-Token': accessToken})
data = response.json()
if(len(data) == 0):
print('ERROR finding', filename)
return
data = data[0]
id = data['_id']
apicall = serverAddress + 'api/v1/item/' + id + '/download'
response = requests.get(url = apicall, headers = {'Girder-Token': accessToken}, stream=True)
if response.status_code == 200:
chunkSize = 1048576 # Download 1 MB at a time
totalNumChunks = ceil(int(response.headers['Content-Length']) / chunkSize)
chunkIndex = 0

handle = open(filename, "wb")
for chunk in response.iter_content(chunk_size=chunkSize):
if not chunk: # filter out keep-alive new chunks
continue
handle.write(chunk)

chunkIndex += 1
stdout.write('\r[%d/%d MB]' % (chunkIndex, totalNumChunks))
stdout.write('\n')
handle.close()
return True
else:
print('ERROR Downloading', filename, '! Response code', response.status_code, _CONTACT_SUPPORT_STRING)
return False


# returns True if success
def _downloadFolder(accessToken, path, folder):
# 1 download items in this folder
Expand Down Expand Up @@ -199,7 +168,6 @@ def _parseFileList(fileList):

# returns True if success
def _downloadFolderFiles(accessToken, path, folder, parsedFileList):
print(parsedFileList)
# 1 download items in this folder
items = GirderAPI._listItemsInFolder(serverAddress, accessToken, folder['_id'])
failure = False
Expand Down
8 changes: 6 additions & 2 deletions Python/DatasetUtilsPackage/DatasetUtils/__init__.py
Expand Up @@ -10,17 +10,21 @@ def getDatasetList(loginState = None):
return GirderConnector._getDatasetList(accessToken)


def downloadDataset(datasetName, destinationPath='.', asZip = True, loginState = None):
def downloadDataset(datasetName, destinationPath='.', fileList = None, asZip = True, loginState = None):
GirderConnector._printDataPortalWelcome()
print('Downloading the', datasetName, 'dataset from the ShapeWorks Portal')
accessToken = GirderConnector._login(loginState)
if not accessToken:
return False

if asZip:
if fileList is not None:
print('Downloading', len(fileList), 'specified files')
success = GirderConnector._downloadDatasetFiles(accessToken, datasetName, fileList, destinationPath)
elif asZip:
success = GirderConnector._downloadDatasetZip(accessToken, datasetName, destinationPath)
else:
success = GirderConnector._downloadDataset(accessToken, datasetName, destinationPath)

if success:
print('Downloaded the', datasetName, 'dataset from the ShapeWorks Portal.')
return True
Expand Down

0 comments on commit 162df08

Please sign in to comment.