|
9 | 9 | import tempfile |
10 | 10 | import requests |
11 | 11 |
|
12 | | -from pyclowder.collections import get_datasets, get_child_collections |
| 12 | +from pyclowder.collections import get_datasets, get_child_collections, delete as delete_collection |
13 | 13 | from pyclowder.utils import StatusMessage |
14 | 14 |
|
15 | 15 |
|
@@ -60,6 +60,47 @@ def create_empty(connector, host, key, datasetname, description, parentid=None, |
60 | 60 | return datasetid |
61 | 61 |
|
62 | 62 |
|
| 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 | + |
63 | 104 | def download(connector, host, key, datasetid): |
64 | 105 | """Download dataset to be processed from Clowder as zip file. |
65 | 106 |
|
|
0 commit comments