Skip to content

Commit 0b37039

Browse files
committed
Initial commit
1 parent e933b3a commit 0b37039

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

pyclowder/api/v2/datasets.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,32 @@ def upload_thumbnail(connector, client, datasetid, thumbnail):
349349
return result.json()["thumbnail_id"]
350350
else:
351351
logger.error("unable to upload thumbnail %s to dataset %s", thumbnail, datasetid)
352+
353+
def create_folder(connector, client, datasetid, foldername, parent_folder=None):
354+
"""Create a new folder in Clowder.
355+
356+
Keyword arguments:
357+
connector -- connector information, used to get missing parameters and send status updates
358+
client -- ClowderClient containing authentication credentials
359+
datasetid -- the dataset that the folder should be associated with
360+
foldername -- the name of the folder to create
361+
parent_folder -- the id of the parent folder, if not provided, the folder will be created at the root of the dataset
362+
"""
363+
364+
url = posixpath.join(client.host, 'api/v2/datasets/%s/folders' % datasetid)
365+
headers = {"X-API-KEY": client.key,
366+
"Content-Type": "application/json"}
367+
368+
if parent_folder is not None:
369+
folder_data = json.dumps({"name": foldername, "parent_folder": parent_folder})
370+
else:
371+
folder_data = json.dumps({"name": foldername})
372+
373+
result = requests.post(url, headers=headers, json=folder_data,
374+
verify=connector.ssl_verify if connector else True)
375+
result.raise_for_status()
376+
folder_id = result.json()["id"]
377+
logger.debug("created folder id = [%s]", folder_id)
378+
return folder_id
379+
380+

pyclowder/api/v2/files.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,40 @@ def _upload_to_dataset_local(connector, client, datasetid, filepath):
406406
return uploadedfileid
407407
else:
408408
logger.error("unable to upload local file %s (not found)", filepath)
409+
410+
411+
def upload_multiple_files(connector, client, datasetid, filepaths, folder_id=None):
412+
"""Upload multiple files to existing Clowder dataset.
413+
414+
Keyword arguments:
415+
connector -- connector information, used to get missing parameters and send status updates
416+
client -- ClowderClient containing authentication credentials
417+
datasetid -- the dataset that the files should be associated with
418+
filepaths -- list of file paths to upload
419+
folder_id -- the folder that the files should be uploaded to
420+
"""
421+
logger = logging.getLogger(__name__)
422+
423+
files = []
424+
for filepath in filepaths:
425+
if os.path.exists(filepath):
426+
files.append(os.path.basename(filepath), open(filepath, 'rb'))
427+
else:
428+
logger.error("unable to upload file %s (not found)", filepath)
429+
return None
430+
431+
432+
url = posixpath.join(client.host, 'api/v2/datasets/%s/filesMultiple' % datasetid)
433+
if folder_id is not None:
434+
url = '%s?folder_id=%s' % (url, folder_id)
435+
436+
headers = {"X-API-KEY": client.key}
437+
response = connector.post(url, files=files, headers=headers,
438+
verify=connector.ssl_verify if connector else True)
439+
440+
if response.status_code == 200:
441+
return response.json()
442+
else:
443+
logger.error("Error uploading files to dataset %s", datasetid)
444+
return None
445+

pyclowder/datasets.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,3 +260,23 @@ def upload_thumbnail(connector, host, key, datasetid, thumbnail):
260260

261261
client = ClowderClient(host=host, key=key)
262262
return datasets.upload_thumbnail(connector, client, datasetid, thumbnail)
263+
264+
265+
def create_folder(connector, host, key, datasetid, foldername, parent_folder_id=None):
266+
"""Create a new folder in Clowder.
267+
268+
Keyword arguments:
269+
connector -- connector information, used to get missing parameters and send status updates
270+
host -- the clowder host, including http and port, should end with a /
271+
key -- the secret key to login to clowder
272+
datasetid -- the dataset that the folder should be associated with
273+
foldername -- the name of the folder to create
274+
parent_folder_id -- the id of the parent folder, if not provided, the folder will be created at the root of the dataset
275+
"""
276+
logger = logging.getLogger(__name__)
277+
if clowder_version == 2:
278+
client = ClowderClient(host=host, key=key)
279+
return datasets.create_folder(connector, client, datasetid, foldername, parent_folder_id)
280+
else:
281+
logger.error("Function not supported for Clowder version %s", clowder_version)
282+
return None

pyclowder/files.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,3 +322,22 @@ def _upload_to_dataset_local(connector, host, key, datasetid, filepath):
322322
client = ClowderClient(host=host, key=key)
323323
uploadedfileid = files._upload_to_dataset_local(connector, client, datasetid, filepath)
324324
return uploadedfileid
325+
326+
def upload_multiple_files(connector, host, key, datasetid, filepaths, folder_id=None):
327+
"""Upload multiple files to existing Clowder dataset.
328+
329+
Keyword arguments:
330+
connector -- connector information, used to get missing parameters and send status updates
331+
host -- the clowder host, including http and port, should end with a /
332+
key -- the secret key to login to clowder
333+
datasetid -- the dataset that the files should be associated with
334+
filepaths -- list of file paths to upload
335+
folder_id -- the folder that the files should be uploaded to
336+
"""
337+
client = ClowderClient(host=host, key=key)
338+
if clowder_version == 2:
339+
return files.upload_multiple_files(connector, client, datasetid, filepaths, folder_id)
340+
else:
341+
logger = logging.getLogger(__name__)
342+
logger.error("Function not supported for Clowder version %s", clowder_version)
343+
return None

0 commit comments

Comments
 (0)