|
11 | 11 | import requests |
12 | 12 |
|
13 | 13 | 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 |
15 | 15 | from pyclowder.utils import StatusMessage |
16 | 16 |
|
17 | 17 |
|
@@ -62,6 +62,47 @@ def create_empty(connector, host, key, datasetname, description, parentid=None, |
62 | 62 | return datasetid |
63 | 63 |
|
64 | 64 |
|
| 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 | + |
65 | 106 | def download(connector, host, key, datasetid): |
66 | 107 | """Download dataset to be processed from Clowder as zip file. |
67 | 108 |
|
|
0 commit comments