Skip to content

Commit 1ab1ef3

Browse files
committed
add delete methods for collections & datasets
1 parent f50609b commit 1ab1ef3

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

pyclowder/collections.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ def create_empty(connector, host, key, collectionname, description, parentid=Non
5858
return collectionid
5959

6060

61+
def delete(connector, host, key, collectionid):
62+
url = "%sapi/collections/%s" % (host, collectionid)
63+
64+
result = requests.delete(url, verify=connector.ssl_verify if connector else True)
65+
result.raise_for_status()
66+
67+
return json.loads(result.text)
68+
69+
6170
def get_child_collections(connector, host, key, collectionid):
6271
"""Get list of child collections in collection by UUID.
6372

pyclowder/datasets.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import tempfile
1010
import requests
1111

12-
from pyclowder.collections import get_datasets, get_child_collections
12+
from pyclowder.collections import get_datasets, get_child_collections, delete as delete_collection
1313
from pyclowder.utils import StatusMessage
1414

1515

@@ -60,6 +60,47 @@ def create_empty(connector, host, key, datasetname, description, parentid=None,
6060
return datasetid
6161

6262

63+
def delete(connector, host, key, datasetid):
64+
"""Delete dataset from Clowder.
65+
66+
Keyword arguments:
67+
connector -- connector information, used to get missing parameters and send status updates
68+
host -- the clowder host, including http and port, should end with a /
69+
key -- the secret key to login to clowder
70+
datasetid -- the dataset to delete
71+
"""
72+
url = "%sapi/datasets/%s" % (host, datasetid)
73+
74+
result = requests.delete(url, verify=connector.ssl_verify if connector else True)
75+
result.raise_for_status()
76+
77+
return json.loads(result.text)
78+
79+
80+
def delete_by_collection(connector, host, key, collectionid, recursive=True, delete_colls=False):
81+
"""Delete datasets from Clowder by iterating through collection.
82+
83+
Keyword arguments:
84+
connector -- connector information, used to get missing parameters and send status updates
85+
host -- the clowder host, including http and port, should end with a /
86+
key -- the secret key to login to clowder
87+
collectionid -- the collection to walk
88+
recursive -- whether to also iterate across child collections
89+
delete_colls -- whether to also delete collections containing the datasets
90+
"""
91+
dslist = get_datasets(connector, host, key, collectionid)
92+
for ds in dslist:
93+
delete(connector, host, key, ds['id'])
94+
95+
if recursive:
96+
childcolls = get_child_collections(connector, host, key, collectionid)
97+
for coll in childcolls:
98+
delete_by_collection(connector, host, key, coll['id'], recursive, delete_colls)
99+
100+
if delete_colls:
101+
delete_collection(connector, host, key, collectionid)
102+
103+
63104
def download(connector, host, key, datasetid):
64105
"""Download dataset to be processed from Clowder as zip file.
65106

0 commit comments

Comments
 (0)