Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from DLHub-Argonne/search_functions
Adding Search Capabilities to CLI
- Loading branch information
Showing
32 changed files
with
490 additions
and
204 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
from dlhub_cli.main import cli_root | ||
|
||
|
||
__all__ = ['cli_root'] | ||
__all__ = ['cli_root'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
from dlhub_cli.commands.describe import describe_cmd | ||
from dlhub_cli.commands.init import init_cmd | ||
from dlhub_cli.commands.publish import publish_cmd | ||
from dlhub_cli.commands.update import update_cmd | ||
from dlhub_cli.commands.run import run_cmd | ||
from dlhub_cli.commands.ls import ls_cmd | ||
from dlhub_cli.commands.status import status_cmd | ||
from dlhub_cli.commands.login import login_cmd | ||
from dlhub_cli.commands.logout import logout_cmd | ||
from dlhub_cli.commands.ls import ls_cmd | ||
from dlhub_cli.commands.methods import methods_cmd | ||
from dlhub_cli.commands.publish import publish_cmd | ||
from dlhub_cli.commands.run import run_cmd | ||
from dlhub_cli.commands.search import search_cmd | ||
from dlhub_cli.commands.servables import servables_cmd | ||
from dlhub_cli.commands.describe import describe_cmd | ||
from dlhub_cli.commands.status import status_cmd | ||
from dlhub_cli.commands.whoami import whoami_cmd | ||
|
||
__all__ = ['init_cmd', 'publish_cmd', 'servables_cmd', | ||
'update_cmd', 'run_cmd', 'ls_cmd', | ||
'run_cmd', 'ls_cmd', 'methods_cmd', | ||
'status_cmd', 'login_cmd', 'logout_cmd', | ||
'describe_cmd'] | ||
'describe_cmd', 'whoami_cmd', 'search_cmd'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,89 @@ | ||
from datetime import datetime | ||
import click | ||
import yaml | ||
|
||
from dlhub_cli.config import get_dlhub_client | ||
from dlhub_cli.printing import format_output | ||
from dlhub_cli.parsing import dlhub_cmd | ||
|
||
|
||
HELP_STR = """\ | ||
_unwanted_fields = [ | ||
('dlhub', 'build_location'), | ||
('dlhub', 'ecr_arn'), | ||
('dlhub', 'ecr_uri'), | ||
('dlhub', 'id'), | ||
('dlhub', 'transfer_method'), | ||
('dlhub', 'user_id') | ||
] | ||
|
||
Describe a servable. | ||
\b | ||
Options: | ||
--id TEXT The UUID of a servable. | ||
--name TEXT The name of a servable. | ||
-h, --help Show this message and exit. | ||
-v, --version Show the version and exit.""" | ||
|
||
def _remove_field(metadata, field): | ||
"""Remove a certain field from the metadata | ||
@dlhub_cmd('describe', help='Describe a servable.') | ||
@click.option('--id', | ||
show_default=True, required=False, | ||
help='The UUID of a servable.') | ||
@click.option('--name', | ||
show_default=True, required=False, | ||
help='The name of a servable.') | ||
def describe_cmd(id, name): | ||
Args: | ||
metadata (dict): Metadata to be pruned | ||
field ([string]): Coordinates of fields to be removed | ||
""" | ||
|
||
if len(field) == 1: | ||
if field[0] in metadata: | ||
del metadata[field[0]] | ||
else: | ||
if field[0] in metadata: | ||
subset = metadata[field[0]] | ||
return _remove_field(subset, field[1:]) | ||
|
||
|
||
def _preprocess_metadata(metadata): | ||
"""Clean up a metadata record to make it more useful to humans | ||
Args: | ||
metadata (metadata): Metadata record to be cleaned | ||
""" | ||
# Prune internal-only fields | ||
for field in _unwanted_fields: | ||
_remove_field(metadata, field) | ||
|
||
# Turn Timestamp (epoch time in ms) into a String | ||
metadata['dlhub']['publication_date'] = \ | ||
datetime.fromtimestamp(int(metadata['dlhub']['publication_date']) / 1000) \ | ||
.strftime('%Y-%m-%d %H:%M') | ||
|
||
|
||
@dlhub_cmd('describe', short_help="Get the description of a servable", | ||
help="""Get the description of a servable | ||
OWNER is the username of the owner of the servable, and NAME is the name of the servable. | ||
You can optionally specify both owner and servable name as a single argument using | ||
a "owner_name/servable_name" format | ||
""") | ||
@click.argument('owner', default=None) | ||
@click.argument('name', default="") | ||
def describe_cmd(owner, name): | ||
"""Use DLHub to get a description of the servable. | ||
Args: | ||
id (string): The uuid of the servable | ||
owner (string): The owner of the servable | ||
name (string): The name of the servable | ||
Returns: | ||
(dict) a set of information regarding the servable | ||
""" | ||
|
||
if not any([id, name]): | ||
format_output(HELP_STR) | ||
return | ||
# Check if owner contains both | ||
if name == "": | ||
temp = owner.split("/") | ||
if len(temp) != 2: | ||
raise click.BadArgumentUsage('Model name missing') | ||
owner, name = temp | ||
|
||
# Retrieve the metadata | ||
client = get_dlhub_client() | ||
res = "Unable to describe a servable (name: {0}, id: {1})".format(name, id) | ||
if id: | ||
res = client.describe_servable(servable_id=id) | ||
elif name: | ||
res = client.describe_servable(servable_name=name) | ||
res = client.describe_servable(owner, name) | ||
|
||
# Clean up the metadata fields | ||
_preprocess_metadata(res) | ||
|
||
format_output("{0}".format(res)) | ||
# Print it to screen | ||
format_output(yaml.dump(res)) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import yaml | ||
import click | ||
|
||
from dlhub_cli.parsing import dlhub_cmd | ||
from dlhub_cli.printing import format_output | ||
from dlhub_cli.config import get_dlhub_client | ||
|
||
|
||
@dlhub_cmd('methods', short_help='Print method information', | ||
help='''Print methods for a certain servable | ||
OWNER is the username of the servable owner, NAME is the name of the servable. | ||
METHOD is optional. If provided, this command will only print the information | ||
for the method with that name. Otherwise, it will print the information for all | ||
methods implemented by the servable. | ||
You can optionally specify the servable name is owner_name/model_name format | ||
''') | ||
@click.argument('owner', default=None) | ||
@click.argument('name', default='') | ||
@click.argument('method', default='') | ||
def methods_cmd(owner, name, method): | ||
"""Print out the methods of a servable | ||
Args: | ||
owner (str): Name of the servable's owner | ||
name (str): Name of the servable | ||
method (str): Name of the method (optional) | ||
""" | ||
|
||
# Check if the owner sent model information in owner/model format | ||
if '/' in owner: | ||
# Get the owner and model name | ||
temp = owner.split('/') | ||
if len(temp) != 2: | ||
raise click.BadArgumentUsage('Expected owner_name/model_name format') | ||
|
||
# If "name" is provided, it is actually the method name | ||
if name != '': | ||
method = name | ||
|
||
owner, name = temp | ||
|
||
# Make sure the name was provided | ||
if name == '': | ||
raise click.BadArgumentUsage('Model name not provided') | ||
|
||
# If the method name is blank, make it None (for compatibility with client) | ||
if method == '': | ||
method = None | ||
|
||
# Get the DLHub client | ||
client = get_dlhub_client() | ||
|
||
# Get the method details | ||
method_details = client.describe_methods(owner, name, method) | ||
|
||
# Print them in YAML format | ||
format_output(yaml.dump(method_details, default_flow_style=False)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.