Changeset View
Changeset View
Standalone View
Standalone View
swh/deposit/cli/client.py
Show First 20 Lines • Show All 480 Lines • ▼ Show 20 Lines | def print_result(data: Dict[str, Any], output_format: Optional[str]) -> None: | ||||
import yaml | import yaml | ||||
if output_format == "json": | if output_format == "json": | ||||
click.echo(json.dumps(data)) | click.echo(json.dumps(data)) | ||||
elif output_format == "yaml": | elif output_format == "yaml": | ||||
click.echo(yaml.dump(data)) | click.echo(yaml.dump(data)) | ||||
else: | else: | ||||
logger.info(data) | logger.info(data) | ||||
@deposit.command("metadata-only") | |||||
@click.option( | |||||
"--url", | |||||
default="https://deposit.softwareheritage.org", | |||||
help="(Optional) Deposit server api endpoint. By default, " | |||||
"https://deposit.softwareheritage.org/1", | |||||
) | |||||
@click.option("--username", required=True, help="(Mandatory) User's name") | |||||
@click.option( | |||||
"--password", required=True, help="(Mandatory) User's associated password" | |||||
ardumont: I really wish we stop doing that redundancy parameter dance in each subcommand...
And push it… | |||||
Not Done Inline ActionsYes. You canalso use a decorator, to deduplicate: def authenticator_decorator(f): f = click.option("--username", required=True, help="(Mandatory) User's name")(f) f = click.option( "--password", required=True, help="(Mandatory) User's associated password" )(f) return f vlorentz: Yes. You canalso use a decorator, to deduplicate:
```
def authenticator_decorator(f):
f =… | |||||
Done Inline ActionsThanks, that's a more dry step. I still wish we stop duplicating those flags though. ardumont: Thanks, that's a more dry step.
I still wish we stop duplicating those flags though. | |||||
Done Inline ActionsAs a first step D4748. ardumont: As a first step D4748. | |||||
) | |||||
@click.option( | |||||
"--metadata", | |||||
"metadata_path", | |||||
type=click.Path(exists=True), | |||||
required=True, | |||||
help="Path to xml metadata file", | |||||
) | |||||
@click.option( | |||||
"-f", | |||||
"--format", | |||||
"output_format", | |||||
default="logging", | |||||
type=click.Choice(["logging", "yaml", "json"]), | |||||
help="Output format results.", | |||||
) | |||||
@click.pass_context | |||||
def metadata_only(ctx, url, username, password, metadata_path, output_format): | |||||
"""Deposit metadata only upload | |||||
""" | |||||
from swh.deposit.client import PublicApiDepositClient | |||||
from swh.deposit.utils import parse_swh_reference, parse_xml | |||||
# Parse to check for a swhid presence within the metadata file | |||||
with open(metadata_path, "r") as f: | |||||
metadata_raw = f.read() | |||||
actual_swhid = parse_swh_reference(parse_xml(metadata_raw)) | |||||
if not actual_swhid: | |||||
raise InputError("A SWHID must be provided for a metadata-only deposit") | |||||
with trap_and_report_exceptions(): | |||||
client = PublicApiDepositClient(url=_url(url), auth=(username, password)) | |||||
collection = _collection(client) | |||||
result = client.deposit_metadata_only(collection, metadata_path) | |||||
print_result(result, output_format) | |||||
Done Inline ActionsThis will be dealt with in another diff [1] [1] D4738 ardumont: This will be dealt with in another diff [1]
[1] D4738 | |||||
Done Inline ActionsIs that reasonable at all? (instead of mutating the user's file, i can also copy it in temporary location, i still don't know if the overall approach is sound though) ardumont: Is that reasonable at all?
(instead of mutating the user's file, i can also copy it in… |
I really wish we stop doing that redundancy parameter dance in each subcommand...
And push it within the scope of the deposit command.
And even open a --config-path (or --config-file) as other commands do...
Thing is, i don't know how to do that without breaking the current interface the users
know...
Is deprecating the old ways (--url --username --password) and start pushing for the new ones ok?
Any better idea?
@vlorentz ^