-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcli.py
More file actions
301 lines (267 loc) · 10.1 KB
/
cli.py
File metadata and controls
301 lines (267 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python3
import json
import os
from typing import List
import click
import databusclient.api.deploy as api_deploy
from databusclient.api.delete import delete as api_delete
from databusclient.api.download import download as api_download, DownloadAuthError
from databusclient.extensions import webdav
def parse_distribution_str(dist_str: str):
"""
Parses a distribution string with format:
URL|key=value|...|.extension
Returns a dictionary suitable for the deploy API.
"""
parts = dist_str.split('|')
url = parts[0].strip()
variants = {}
format_ext = None
compression = None
# Iterate over the modifiers (everything after the URL)
for part in parts[1:]:
part = part.strip()
# Case 1: Extension (starts with .)
if part.startswith('.'):
# purely heuristic: if it looks like compression (gz, zip, br), treat as compression
# otherwise treat as format extension
if part.lower() in ['.gz', '.zip', '.br', '.tar', '.zst']:
compression = part.lstrip('.') # remove leading dot for API compatibility if needed
else:
format_ext = part.lstrip('.')
# Case 2: Content Variant (key=value)
elif '=' in part:
key, value = part.split('=', 1)
variants[key.strip()] = value.strip()
# Case 3: Standalone tag (treat as boolean variant or ignore?
# For now, we assume it's a value for a default key or warn)
else:
print(f"WARNING: Unrecognized modifier '{part}' in distribution. Expected '.ext' or 'key=val'.")
return {
"url": url,
"variants": variants,
"formatExtension": format_ext,
"compression": compression
}
@click.group()
def app():
"""Databus Client CLI.
Provides `deploy`, `download`, and `delete` commands for interacting
with the DBpedia Databus.
"""
pass
@app.command()
@click.option(
"--version-id",
"version_id",
required=True,
help="Target databus version/dataset identifier of the form "
"<https://databus.dbpedia.org/$ACCOUNT/$GROUP/$ARTIFACT/$VERSION>",
)
@click.option("--title", required=True, help="Artifact & Version Title: used for BOTH artifact and version. Keep stable across releases; identifies the data series.")
@click.option("--abstract", required=True, help="Artifact & Version Abstract: used for BOTH artifact and version (max 200 chars). Updating it changes both artifact and version metadata.")
@click.option("--description", required=True, help="Artifact & Version Description: used for BOTH artifact and version. Supports Markdown. Updating it changes both artifact and version metadata.")
@click.option(
"--license", "license_url", required=True, help="License (see dalicc.net)"
)
@click.option("--apikey", required=True, help="API key")
@click.option(
"--metadata",
"metadata_file",
type=click.Path(exists=True),
help="Path to metadata JSON file (for metadata mode)",
)
@click.option(
"--webdav-url",
"webdav_url",
help="WebDAV URL (e.g., https://cloud.example.com/remote.php/webdav)",
)
@click.option("--remote", help="rclone remote name (e.g., 'nextcloud')")
@click.option("--path", help="Remote path on Nextcloud (e.g., 'datasets/mydataset')")
@click.argument("distributions", nargs=-1)
def deploy(
version_id,
title,
abstract,
description,
license_url,
apikey,
metadata_file,
webdav_url,
remote,
path,
distributions: List[str],
):
"""
Flexible deploy to Databus command supporting three modes:\n
- Classic deploy (distributions as arguments)\n
- Metadata-based deploy (--metadata <file>)\n
- Upload & deploy via Nextcloud (--webdav-url, --remote, --path)
"""
# Sanity checks for conflicting options
if metadata_file and any([distributions, webdav_url, remote, path]):
raise click.UsageError(
"Invalid combination: when using --metadata, do not provide --webdav-url, --remote, --path, or distributions."
)
if any([webdav_url, remote, path]) and not all([webdav_url, remote, path]):
raise click.UsageError(
"Invalid combination: when using WebDAV/Nextcloud mode, please provide --webdav-url, --remote, and --path together."
)
# === Mode 1: Classic Deploy ===
if distributions and not (metadata_file or webdav_url or remote or path):
click.echo("[MODE] Classic deploy with distributions")
click.echo(f"Deploying dataset version: {version_id}")
# --- CHANGE START ---
# Parse the input strings into structured objects
parsed_distributions = [parse_distribution_str(d) for d in distributions]
# Note: api_deploy.create_dataset now accepts this list of dicts
dataid = api_deploy.create_dataset(
version_id, title, abstract, description, license_url, parsed_distributions
version_id=version_id,
artifact_version_title=title,
artifact_version_abstract=abstract,
artifact_version_description=description,
license_url=license_url,
distributions=distributions
)
# --- CHANGE END ---
api_deploy.deploy(dataid=dataid, api_key=apikey)
return
# === Mode 2: Metadata File ===
if metadata_file:
click.echo(f"[MODE] Deploy from metadata file: {metadata_file}")
with open(metadata_file, "r") as f:
metadata = json.load(f)
api_deploy.deploy_from_metadata(
metadata, version_id, title, abstract, description, license_url, apikey
)
return
# === Mode 3: Upload & Deploy (Nextcloud) ===
if webdav_url and remote and path:
if not distributions:
raise click.UsageError(
"Please provide files to upload when using WebDAV/Nextcloud mode."
)
# Check that all given paths exist and are files or directories.
invalid = [f for f in distributions if not os.path.exists(f)]
if invalid:
raise click.UsageError(
f"The following input files or folders do not exist: {', '.join(invalid)}"
)
click.echo("[MODE] Upload & Deploy to DBpedia Databus via Nextcloud")
click.echo(f"→ Uploading to: {remote}:{path}")
metadata = webdav.upload_to_webdav(distributions, remote, path, webdav_url)
api_deploy.deploy_from_metadata(
metadata, version_id, title, abstract, description, license_url, apikey
)
return
raise click.UsageError(
"No valid input provided. Please use one of the following modes:\n"
" - Classic deploy: pass distributions as arguments\n"
" - Metadata deploy: use --metadata <file>\n"
" - Upload & deploy: use --webdav-url, --remote, --path, and file arguments"
)
@app.command()
@click.argument("databusuris", nargs=-1, required=True)
@click.option(
"--localdir",
help="Local databus folder (if not given, databus folder structure is created in current working directory)",
)
@click.option(
"--databus",
help="Databus URL (if not given, inferred from databusuri, e.g. https://databus.dbpedia.org/sparql)",
)
@click.option("--vault-token", help="Path to Vault refresh token file")
@click.option(
"--databus-key", help="Databus API key to download from protected databus"
)
@click.option(
"--all-versions",
is_flag=True,
help="When downloading artifacts, download all versions instead of only the latest",
)
@click.option(
"--authurl",
default="https://auth.dbpedia.org/realms/dbpedia/protocol/openid-connect/token",
show_default=True,
help="Keycloak token endpoint URL",
)
@click.option(
"--clientid",
default="vault-token-exchange",
show_default=True,
help="Client ID for token exchange",
)
@click.option(
"--convert-to",
type=click.Choice(["bz2", "gz", "xz"], case_sensitive=False),
help="Target compression format for on-the-fly conversion during download (supported: bz2, gz, xz)",
)
@click.option(
"--convert-from",
type=click.Choice(["bz2", "gz", "xz"], case_sensitive=False),
help="Source compression format to convert from (optional filter). Only files with this compression will be converted.",
)
@click.option(
"--validate-checksum",
is_flag=True,
help="Validate checksums of downloaded files"
)
def download(
databusuris: List[str],
localdir,
databus,
vault_token,
databus_key,
all_versions,
authurl,
clientid,
convert_to,
convert_from,
validate_checksum,
):
"""
Download datasets from databus, optionally using vault access if vault options are provided.
Supports on-the-fly compression format conversion using --convert-to and --convert-from options.
"""
try:
api_download(
localDir=localdir,
endpoint=databus,
databusURIs=databusuris,
token=vault_token,
databus_key=databus_key,
all_versions=all_versions,
auth_url=authurl,
client_id=clientid,
convert_to=convert_to,
convert_from=convert_from,
validate_checksum=validate_checksum,
)
except DownloadAuthError as e:
raise click.ClickException(str(e))
@app.command()
@click.argument("databusuris", nargs=-1, required=True)
@click.option(
"--databus-key", help="Databus API key to access protected databus", required=True
)
@click.option(
"--dry-run", is_flag=True, help="Perform a dry run without actual deletion"
)
@click.option(
"--force", is_flag=True, help="Force deletion without confirmation prompt"
)
def delete(databusuris: List[str], databus_key: str, dry_run: bool, force: bool):
"""
Delete a dataset from the databus.
Delete a group, artifact, or version identified by the given databus URI.
Will recursively delete all data associated with the dataset.
"""
api_delete(
databusURIs=databusuris,
databus_key=databus_key,
dry_run=dry_run,
force=force,
)
if __name__ == "__main__":
app()