def describe_cmd(owner, name): """Use DLHub to get a description of the servable. Args: owner (string): The owner of the servable name (string): The name of the servable Returns: (dict) a set of information regarding the servable """ # 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 = client.describe_servable(owner, name) # Clean up the metadata fields _preprocess_metadata(res) # Print it to screen format_output(yaml.dump(res)) return res
def run_cmd(servable, input): """Invoke a servable. The input data will be read with json.loads(input) and passed to the servable. Args: servable (string): The servable to invoke, e.g., "ryan_globusid/noop" input (dict): Input to pass into the servable Returns: (dict) resulting data. The output from executing the servable. """ if not any([servable, input]): format_output(HELP_STR) return client = get_dlhub_client() data = json.loads(input) namespace = servable.split("/")[0] model = servable.split("/")[1] res = client.run(namespace, model, data) format_output(res) return res
def methods_cmd(name, method): """Print out the methods of a servable Args: name (string): DLHub name of the servable of the form <user>/<servable_name> method (str): Name of the method (optional) """ # Check if name is proper format if len(name.split("/")) < 2: raise click.BadArgumentUsage('Please enter name in the form <user>/<servable_name>') if name.split("/")[0] == "" or name.split("/")[1] == "": raise click.BadArgumentUsage('Please enter name in the form <user>/<servable_name>') # 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(name, method) # Print them in YAML format format_output(yaml.dump(method_details, default_flow_style=False))
def init_cmd(force, filename, author, title, name, skip_run): """ Initial step in creating a DLHub servable Creates a Python script that a user can edit to create a description for a servable Args: force (bool): Whether to overwrite an existing template file filename (string): Name of the template file to create author ([string]): List of authors and affiliations title (string): Title for the servable name (string): Name of the servable skip_run (bool): Whether the skip executing the init script """ format_output("Initializing") # Check if a file would be overwritten if os.path.isfile(filename) and not force: format_output("There is already a file named '{}'." " Use --force to overwrite".format(filename)) return 1 # Prepare a dict of objects to be replaced subs = { 'authors': '[{}]'.format(', '.join('"{}"'.format(x[0]) for x in author)), 'affiliations': '[{}]'.format(', '.join('["{}"]'.format(x[1]) for x in author)), 'title': title, 'name': name } # Copy the template to the directory and make substitutions with open(filename, 'w') as fo: with open(os.path.join(template_path, 'describe_servable.py.template')) as fi: for line in fi: newline = Template(line).substitute(subs).rstrip() print(newline, file=fo) format_output('...Saved settings file as {}'.format(filename)) # Unless skipped, run the new file using the same Python that is running this code if not skip_run: proc = Popen([sys.executable, filename], stderr=PIPE) errors = proc.stderr.readlines() if proc.wait() != 0: format_output('WARNING: Script failed to run. Error details:') for line in errors: format_output(line.decode().rstrip()) else: print('...Saved model description as dlhub.json') return
def servables_cmd(): """List the available servables. Returns: (dict) the list of available servables as (uuid, name) pairs. """ client = get_dlhub_client() res = client.list_servables() format_output("{0}".format(res)) return res
def ls_cmd(): """List servables that have been init'd in this directory. Returns: (None) none """ format_output("Servables:") # List the set of servables in the dlhub directory. servables = [serv_file for serv_file in os.listdir('.') if serv_file.endswith('dlhub.json')] for serv in servables: res = serv.split(".json")[0] format_output(res)
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))
def describe_cmd(name): """Use DLHub to get a description of the servable. Args: name (string): DLHub name of the servable of the form <user>/<servable_name> Returns: (dict) a set of information regarding the servable """ # Check if name is proper format if len(name.split("/")) < 2: raise click.BadArgumentUsage('Please enter name in the form <user>/<servable_name>') # Retrieve the metadata client = get_dlhub_client() res = client.describe_servable(name) # Clean up the metadata fields _preprocess_metadata(res) # Print it to screen format_output(yaml.dump(res)) return res