Skip to content

Commit 02b1873

Browse files
committed
- refactored deploy, upload_and_deploy and deploy_with_metadata to one single deploy command
- added simple validate_distributions function
1 parent 77dca5a commit 02b1873

File tree

2 files changed

+78
-79
lines changed

2 files changed

+78
-79
lines changed

databusclient/cli.py

Lines changed: 59 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/usr/bin/env python3
22
import json
3+
import os
4+
import re
35

46
import click
57
from typing import List
@@ -25,90 +27,68 @@ def app():
2527
@click.option("--description", required=True, help="Dataset description")
2628
@click.option("--license", "license_url", required=True, help="License (see dalicc.net)")
2729
@click.option("--apikey", required=True, help="API key")
28-
@click.argument(
29-
"distributions",
30-
nargs=-1,
31-
required=True,
32-
)
33-
def deploy(version_id, title, abstract, description, license_url, apikey, distributions: List[str]):
34-
"""
35-
Deploy a dataset version with the provided metadata and distributions.
36-
"""
37-
click.echo(f"Deploying dataset version: {version_id}")
38-
dataid = client.create_dataset(version_id, title, abstract, description, license_url, distributions)
39-
client.deploy(dataid=dataid, api_key=apikey)
40-
41-
42-
@app.command()
43-
@click.option(
44-
"--metadata", "metadata_file",
45-
required=True,
46-
type=click.Path(exists=True),
47-
help="Path to metadata JSON file",
48-
)
49-
@click.option(
50-
"--version-id", "version_id",
51-
required=True,
52-
help="Target databus version/dataset identifier of the form "
53-
"<https://databus.dbpedia.org/$ACCOUNT/$GROUP/$ARTIFACT/$VERSION>",
54-
)
55-
@click.option("--title", required=True, help="Dataset title")
56-
@click.option("--abstract", required=True, help="Dataset abstract max 200 chars")
57-
@click.option("--description", required=True, help="Dataset description")
58-
@click.option("--license", "license_url", required=True, help="License (see dalicc.net)")
59-
@click.option("--apikey", required=True, help="API key")
60-
def deploy_with_metadata(metadata_file, version_id, title, abstract, description, license_url, apikey):
61-
"""
62-
Deploy to DBpedia Databus using metadata json file.
63-
"""
64-
65-
with open(metadata_file, 'r') as f:
66-
metadata = json.load(f)
6730

68-
client.deploy_from_metadata(metadata, version_id, title, abstract, description, license_url, apikey)
31+
@click.option("--metadata", "metadata_file", type=click.Path(exists=True),
32+
help="Path to metadata JSON file (for metadata mode)")
33+
@click.option("--webdav-url", "webdav_url", help="WebDAV URL (e.g., https://cloud.example.com/remote.php/webdav)")
34+
@click.option("--remote", help="rclone remote name (e.g., 'nextcloud')")
35+
@click.option("--path", help="Remote path on Nextcloud (e.g., 'datasets/mydataset')")
6936

70-
71-
@app.command()
72-
@click.option(
73-
"--webdav-url", "webdav_url",
74-
required=True,
75-
help="WebDAV URL (e.g., https://cloud.example.com/remote.php/webdav)",
76-
)
77-
@click.option(
78-
"--remote",
79-
required=True,
80-
help="rclone remote name (e.g., 'nextcloud')",
81-
)
82-
@click.option(
83-
"--path",
84-
required=True,
85-
help="Remote path on Nextcloud (e.g., 'datasets/mydataset')",
86-
)
87-
@click.option(
88-
"--version-id", "version_id",
89-
required=True,
90-
help="Target databus version/dataset identifier of the form "
91-
"<https://databus.dbpedia.org/$ACCOUNT/$GROUP/$ARTIFACT/$VERSION>",
92-
)
93-
@click.option("--title", required=True, help="Dataset title")
94-
@click.option("--abstract", required=True, help="Dataset abstract max 200 chars")
95-
@click.option("--description", required=True, help="Dataset description")
96-
@click.option("--license", "license_url", required=True, help="License (see dalicc.net)")
97-
@click.option("--apikey", required=True, help="API key")
98-
@click.argument(
99-
"files",
100-
nargs=-1,
101-
type=click.Path(exists=True),
102-
)
103-
def upload_and_deploy(webdav_url, remote, path, version_id, title, abstract, description, license_url, apikey,
104-
files: List[str]):
37+
@click.argument("inputs", nargs=-1)
38+
def deploy(version_id, title, abstract, description, license_url, apikey,
39+
metadata_file, webdav_url, remote, path, inputs: List[str]):
10540
"""
106-
Upload files to Nextcloud and deploy to DBpedia Databus.
41+
Flexible deploy to databus command:\n
42+
- Classic dataset deployment\n
43+
- Metadata-based deployment\n
44+
- Upload & deploy via Nextcloud
10745
"""
10846

109-
click.echo(f"Uploading data to nextcloud: {remote}")
110-
metadata = upload.upload_to_nextcloud(files, remote, path, webdav_url)
111-
client.deploy_from_metadata(metadata, version_id, title, abstract, description, license_url, apikey)
47+
# === Mode 1: Upload & Deploy (Nextcloud) ===
48+
if webdav_url and remote and path:
49+
if not inputs:
50+
raise click.UsageError("Please provide files to upload when using WebDAV/Nextcloud mode.")
51+
52+
#Check that all given paths exist and are files or directories.#
53+
invalid = [f for f in inputs if not os.path.exists(f)]
54+
if invalid:
55+
raise click.UsageError(f"The following input files or folders do not exist: {', '.join(invalid)}")
56+
57+
click.echo(f"[MODE] Upload & Deploy to DBpedia Databus via Nextcloud")
58+
click.echo(f"→ Uploading to: {remote}:{path}")
59+
metadata = upload.upload_to_nextcloud(inputs, remote, path, webdav_url)
60+
client.deploy_from_metadata(metadata, version_id, title, abstract, description, license_url, apikey)
61+
return
62+
63+
# === Mode 2: Metadata File ===
64+
if metadata_file:
65+
click.echo(f"[MODE] Deploy from metadata file: {metadata_file}")
66+
with open(metadata_file, 'r') as f:
67+
metadata = json.load(f)
68+
client.deploy_from_metadata(metadata, version_id, title, abstract, description, license_url, apikey)
69+
return
70+
71+
# === Mode 3: Classic Deploy ===
72+
if inputs:
73+
invalid = client.validate_distributions(inputs)
74+
if invalid:
75+
raise click.UsageError(
76+
f"The following distributions are not in a valid format:\n"
77+
+ "\n".join(invalid)
78+
+ "\nExpected format example:\n"
79+
"https://example.com/file.ttl|format=ttl|gzip|abcdef123456789:12345"
80+
)
81+
click.echo(f"[MODE] Classic deploy with distributions")
82+
dataid = client.create_dataset(version_id, title, abstract, description, license_url, inputs)
83+
client.deploy(dataid=dataid, api_key=apikey)
84+
return
85+
86+
raise click.UsageError(
87+
"No valid input provided. Please use one of the following modes:\n"
88+
" - Classic deploy: pass distributions as arguments\n"
89+
" - Metadata deploy: use --metadata <file>\n"
90+
" - Upload & deploy: use --webdav-url, --remote, --path, and file arguments"
91+
)
11292

11393

11494
@app.command()

databusclient/client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,25 @@ def create_distributions_from_metadata(metadata: List[Dict[str, Union[str, int]]
279279
return distributions
280280

281281

282+
def validate_distributions(distros: List[str]) -> List[str]:
283+
"""
284+
Check that all distributions follow the pattern:
285+
url|key=value|[format]|[compression]|[sha256:len]
286+
287+
Parameters
288+
----------
289+
List[str]
290+
List of distribution identifiers to validate
291+
292+
Returns
293+
-------
294+
List[str]
295+
List of invalid distribution identifier strings
296+
"""
297+
pattern = re.compile(r"^https?://[^|]+\|.+$")
298+
return [d for d in distros if not pattern.match(d)]
299+
300+
282301
def create_dataset(
283302
version_id: str,
284303
title: str,

0 commit comments

Comments
 (0)