Пример #1
0
def choose_command(directory: str) -> str:
    scripts = sorted(find_scripts(directory))
    while True:
        if scripts:
            click.echo(
                'We found these script files in this directory.\n'
                'If any of them is the script file you\'d like to use for Valohai, type its number.\n'
                'Otherwise, you can just type the command to run.'
            )
            command = prompt_from_list(
                [
                    {'name': f'{interpreter} {script}'}
                    for (interpreter, script)
                    in scripts
                ],
                'Choose a number or enter a command.',
                nonlist_validator=lambda s: s.strip()
            )
            if isinstance(command, dict):
                command = command['name']
        else:  # pragma: no cover
            command = click.prompt(
                'We couldn\'t find script files in this directory.\n'
                'Please enter the command you\'d like to run in the Valohai platform.\n'
            )
        if not command:  # pragma: no cover
            error('Please try again.')
            continue
        if click.confirm(f'Is {click.style(command, bold=True)} correct?'):
            break
    success(f'Got it! Using {command} as the command.')
    return str(command)
Пример #2
0
def choose_image() -> str:
    image_suggestions = get_image_suggestions()
    click.echo(
        'Now let\'s pick a Docker image to use with your code.\n' +
        (
            'Here are some recommended choices, but feel free to type in one of '
            'your own from the ones available at https://hub.docker.com/'
            if image_suggestions
            else ''
        )
    )
    while True:
        image = prompt_from_list(
            image_suggestions,
            (
                'Choose a number or enter a Docker image name.'
                if image_suggestions else
                'Enter a Docker image name.'
            ),
            nonlist_validator=lambda s: s.strip()
        )
        if isinstance(image, dict):
            image_name = str(image['name'])
        else:
            image_name = str(image)
        if click.confirm(f'Is {click.style(image_name, bold=True)} correct?'):
            break
    success(f'Great! Using {image_name}.')
    return image_name
Пример #3
0
def choose_project(dir: str, spec: Optional[str] = None) -> Optional[dict]:
    """
    Choose a project, possibly interactively.

    :param dir: Directory (only used for prompts)
    :param spec: An optional search string
    :return: project object or None
    """
    projects: List[dict] = request('get',
                                   '/api/v0/projects/',
                                   params={
                                       'limit': '1000'
                                   }).json()['results']
    if not projects:
        if click.confirm('You don\'t have any projects. Create one instead?'):
            raise NewProjectInstead()
        return None

    if spec:
        projects = filter_projects(projects, spec)
        if not projects:
            warn(f'No projects match {spec}')
            return None
        if len(projects) == 1:
            return projects[0]

    def nonlist_validator(answer: str) -> Any:
        if answer.startswith('n'):
            raise NewProjectInstead()

    prompt = 'Which project would you like to link with {dir}?\nEnter [n] to create a new project.'.format(
        dir=click.style(dir, bold=True), )
    has_multiple_owners = (len(
        {p.get('owner', {}).get('id')
         for p in projects}) > 1)

    def project_name_formatter(project: dict) -> str:
        name: str = project['name']
        try:
            if has_multiple_owners:
                dim_owner = click.style(project['owner']['username'] + '/',
                                        dim=True)
                return f'{dim_owner}{name}'
        except Exception:
            pass
        return name

    projects.sort(key=lambda project: project_name_formatter(project).lower())
    return prompt_from_list(projects,
                            prompt,
                            nonlist_validator,
                            name_formatter=project_name_formatter)