Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
137 changes: 9 additions & 128 deletions src/hdx_cli/cli_interface/dictionary/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
from hdx_cli.cli_interface.common.rest_operations import delete as command_delete
from hdx_cli.cli_interface.common.rest_operations import list_ as command_list
from hdx_cli.cli_interface.common.rest_operations import show as command_show
from hdx_cli.cli_interface.common.undecorated_click_commands import (
basic_create,
basic_create_file,
basic_delete,
)
from hdx_cli.cli_interface.common.undecorated_click_commands import basic_create
from hdx_cli.library_api.common.exceptions import (
MissingSettingsException,
ResourceNotFoundException,
Expand All @@ -24,15 +20,15 @@
skip_group_logic_on_help,
target_cluster_options,
)
from hdx_cli.library_api.utility.file_handling import read_bytes_from_file, read_json_from_file
from hdx_cli.library_api.utility.file_handling import read_json_from_file
from hdx_cli.models import ProfileUserContext

from .operations import download_dictionary_file
from .files import files_group as files

logger = get_logger()


@click.group(cls=HdxGroup)
@click.group(cls=HdxGroup, name="dictionary")
@click.option(
"--project",
"project_name",
Expand Down Expand Up @@ -74,7 +70,7 @@ def dictionary(ctx: click.Context, project_name: str, dictionary_name: str):
ctx.obj = {"resource_path": resource_path, "usercontext": user_profile}


@click.command(cls=HdxCommand)
@click.command(cls=HdxCommand, name="create")
@click.argument(
"dict_settings_file_path",
metavar="DICT_SETTINGS_FILE_PATH",
Expand Down Expand Up @@ -118,7 +114,7 @@ def create(
logger.info(f"Created {ctx.parent.command.name} {resource_name}")


@click.command(cls=HdxCommand)
@click.command(cls=HdxCommand, name="migrate")
@click.argument("target_project_name", metavar="TARGET_PROJECT_NAME")
@click.argument("new_dictionary_name", metavar="NEW_DICTIONARY_NAME")
@target_cluster_options
Expand Down Expand Up @@ -192,125 +188,10 @@ def migrate(
logger.info("All resources migrated successfully")


@click.command(cls=HdxCommand, name="download")
@click.argument("dictionary_filename", metavar="DICTIONARY_FILENAME")
@click.option(
"--output",
"-o",
"output_path",
help="Path to save the file, including the new filename. "
"If not provided, saves to the current directory with the original name.",
type=click.Path(dir_okay=False, writable=True),
default=None,
)
@click.pass_context
@report_error_and_exit(exctype=Exception)
def download_file(ctx: click.Context, dictionary_filename: str, output_path: str):
"""
Download a dictionary data file to your local machine.
This command retrieves a dictionary file and saves it
to a specified path, or the current directory by default.

\b
Examples:
# Download 'countries' to the current directory
hdxcli dictionary --project my_proj files download countries

\b
# Download 'countries' but save it as 'country_list.csv' in the current dir
hdxcli dictionary --project my_proj files download countries.csv -o country_list.csv

\b
# Download 'countries' to a specific 'data' folder with 'countries.csv' as the new filename
hdxcli dictionary --project my_proj files download countries.csv -o ./data/countries.csv
"""
profile = ctx.parent.obj["usercontext"]
# The resource path is for 'files', which is what the operation function expects
resource_path = ctx.parent.obj["resource_path"]
download_dictionary_file(profile, resource_path, dictionary_filename, output_path)


@click.group(cls=HdxGroup)
@click.pass_context
@skip_group_logic_on_help
@report_error_and_exit(exctype=Exception)
def files(ctx: click.Context):
"""Manage dictionary data files."""
user_profile = ctx.parent.obj["usercontext"]
resource_path = f'{ctx.obj["resource_path"]}files'
ctx.obj = {"resource_path": resource_path, "usercontext": user_profile}


@click.command(cls=HdxCommand)
@click.argument(
"file_path_to_upload",
metavar="FILE_PATH_TO_UPLOAD",
type=click.Path(exists=True, readable=True),
)
@click.argument("dict_file_name", metavar="DICT_FILE_NAME")
@click.option(
"--body-from-file-type",
"-t",
type=click.Choice(("json", "verbatim"), case_sensitive=False),
help="How to interpret the body from the file. Defaults to 'json'.",
default="json",
)
@click.pass_context
@report_error_and_exit(exctype=Exception)
def files_upload(
ctx: click.Context,
file_path_to_upload: str,
dict_file_name: str,
body_from_file_type: str,
):
"""Upload a dictionary data file.

\b
Examples:
# Upload a local CSV file to be used as a data source for a dictionary
hdxcli dictionary --project my_project files upload ./local_countries.csv countries -t verbatim
"""
profile = ctx.parent.obj["usercontext"]
resource_path = ctx.parent.obj["resource_path"]
file_content = read_bytes_from_file(file_path_to_upload)
basic_create_file(
profile,
resource_path,
dict_file_name,
file_content=file_content,
file_type=body_from_file_type,
)
logger.info(f"Uploaded dictionary file {dict_file_name}")


@click.command(cls=HdxCommand)
@click.argument("file_name", metavar="FILE_NAME")
@click.pass_context
@report_error_and_exit(exctype=Exception)
def files_delete(ctx: click.Context, file_name: str):
"""Delete a dictionary data file.

\b
Examples:
# Delete the file named 'my_dictionary_file'
hdxcli dictionary --project my_project files delete my_dictionary_file
"""
profile = ctx.parent.obj["usercontext"]
resource_path = ctx.parent.obj["resource_path"]
hostname = profile.hostname
scheme = profile.scheme
resource_url = f"{scheme}://{hostname}{resource_path}/{file_name}"
basic_delete(profile, resource_path, file_name, url=resource_url)
logger.info(f"Deleted dictionary file {file_name}")


dictionary.add_command(create, name="create")
# Subgroup
dictionary.add_command(files)
files.add_command(files_upload, name="upload")
files.add_command(command_list)
files.add_command(download_file)
files.add_command(files_delete, name="delete")

# Commands
dictionary.add_command(create)
dictionary.add_command(command_list)
dictionary.add_command(command_delete)
dictionary.add_command(command_show)
Expand Down
Loading