|
| 1 | +import click |
| 2 | +from rich.console import Console |
| 3 | +from rich.table import Column, Table |
| 4 | + |
| 5 | +from cli.client import init_client |
| 6 | +from cli.helpers.nucleus_url import nucleus_url |
| 7 | +from cli.helpers.web_helper import launch_web_or_invoke |
| 8 | + |
| 9 | + |
| 10 | +@click.group("datasets", invoke_without_command=True) |
| 11 | +@click.option("--web", is_flag=True, help="Launch browser") |
| 12 | +@click.pass_context |
| 13 | +def datasets(ctx, web): |
| 14 | + """Datasets are the base collections of items in Nucleus |
| 15 | +
|
| 16 | + https://dashboard.scale.com/nucleus/datasets |
| 17 | + """ |
| 18 | + launch_web_or_invoke( |
| 19 | + sub_url="datasets", ctx=ctx, launch_browser=web, command=list_datasets |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +@datasets.command("list") |
| 24 | +@click.option( |
| 25 | + "-m", "--machine-readable", is_flag=True, help="Removes pretty printing" |
| 26 | +) |
| 27 | +def list_datasets(machine_readable): |
| 28 | + """List all available Datasets""" |
| 29 | + console = Console() |
| 30 | + with console.status("Finding your Datasets!", spinner="dots4"): |
| 31 | + client = init_client() |
| 32 | + all_datasets = client.datasets |
| 33 | + if machine_readable: |
| 34 | + table_params = {"box": None, "pad_edge": False} |
| 35 | + else: |
| 36 | + table_params = { |
| 37 | + "title": ":fire: Datasets", |
| 38 | + "title_justify": "left", |
| 39 | + } |
| 40 | + |
| 41 | + table = Table( |
| 42 | + "id", "Name", Column("url", overflow="fold"), **table_params |
| 43 | + ) |
| 44 | + for ds in all_datasets: |
| 45 | + table.add_row(ds.id, ds.name, nucleus_url(ds.id)) |
| 46 | + console.print(table) |
| 47 | + |
| 48 | + |
| 49 | +@datasets.command("delete") |
| 50 | +@click.option("--id", prompt=True) |
| 51 | +@click.option( |
| 52 | + "--no-confirm-deletion", |
| 53 | + is_flag=True, |
| 54 | + help="WARNING: No confirmation for deletion", |
| 55 | +) |
| 56 | +@click.pass_context |
| 57 | +def delete_dataset(ctx, id, no_confirm_deletion): |
| 58 | + """Delete a Dataset""" |
| 59 | + console = Console() |
| 60 | + client = init_client() |
| 61 | + id = id.strip() |
| 62 | + dataset = client.get_dataset(id) |
| 63 | + delete_string = "" |
| 64 | + if not no_confirm_deletion: |
| 65 | + delete_string = click.prompt( |
| 66 | + click.style( |
| 67 | + f"Type 'DELETE' to delete dataset: {dataset}", fg="red" |
| 68 | + ) |
| 69 | + ) |
| 70 | + if no_confirm_deletion or delete_string == "DELETE": |
| 71 | + client.delete_dataset(dataset.id) |
| 72 | + console.print(f":fire: :anguished: Deleted {id}") |
| 73 | + else: |
| 74 | + console.print( |
| 75 | + f":rotating_light: Refusing to delete {id}. Received '{delete_string}' instead of 'DELETE'" |
| 76 | + ) |
| 77 | + ctx.abort() |
0 commit comments