-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcli.py
More file actions
116 lines (99 loc) · 4.72 KB
/
cli.py
File metadata and controls
116 lines (99 loc) · 4.72 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
#!/usr/bin/env python3
import json
import os
import re
import click
from typing import List
from databusclient import client
from nextcloudclient import upload
@click.group()
def app():
"""Databus Client CLI"""
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="Dataset title")
@click.option("--abstract", required=True, help="Dataset abstract max 200 chars")
@click.option("--description", required=True, help="Dataset description")
@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("inputs", nargs=-1)
def deploy(version_id, title, abstract, description, license_url, apikey,
metadata_file, webdav_url, remote, path, inputs: List[str]):
"""
Flexible deploy to databus command:\n
- Classic dataset deployment\n
- Metadata-based deployment\n
- Upload & deploy via Nextcloud
"""
# === Mode 1: Upload & Deploy (Nextcloud) ===
if webdav_url and remote and path:
if not inputs:
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 inputs 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 = upload.upload_to_nextcloud(inputs, remote, path, webdav_url)
client.deploy_from_metadata(metadata, version_id, title, abstract, description, license_url, 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)
client.deploy_from_metadata(metadata, version_id, title, abstract, description, license_url, apikey)
return
# === Mode 3: Classic Deploy ===
if inputs:
invalid = client.validate_distributions(inputs)
if invalid:
raise click.UsageError(
f"The following distributions are not in a valid format:\n"
+ "\n".join(invalid)
+ "\nExpected format example:\n"
"https://example.com/file.ttl|format=ttl|gzip|abcdef123456789:12345"
)
click.echo("[MODE] Classic deploy with distributions")
dataid = client.create_dataset(version_id, title, abstract, description, license_url, inputs)
client.deploy(dataid=dataid, api_key=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("--token", help="Path to Vault refresh token file")
@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")
def download(databusuris: List[str], localdir, databus, token, authurl, clientid):
"""
Download datasets from databus, optionally using vault access if vault options are provided.
"""
client.download(
localDir=localdir,
endpoint=databus,
databusURIs=databusuris,
token=token,
auth_url=authurl,
client_id=clientid,
)
if __name__ == "__main__":
app()