Skip to content

Commit edf58e6

Browse files
committed
Merge pull request #49 in CATS/pyclowder2 from add-delete-methods to develop
* commit '321a79081f8bc16454a2154b93a285841d94d9c9': add delete methods for collections & datasets
2 parents 0ccb930 + 321a790 commit edf58e6

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
@@ -59,6 +59,15 @@ def create_empty(connector, host, key, collectionname, description, parentid=Non
5959
return collectionid
6060

6161

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

pyclowder/datasets.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import requests
1212

1313
from pyclowder.client import ClowderClient
14-
from pyclowder.collections import get_datasets, get_child_collections
14+
from pyclowder.collections import get_datasets, get_child_collections, delete as delete_collection
1515
from pyclowder.utils import StatusMessage
1616

1717

@@ -62,6 +62,47 @@ def create_empty(connector, host, key, datasetname, description, parentid=None,
6262
return datasetid
6363

6464

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

0 commit comments

Comments
 (0)