# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

import sys

import click
import rhea

from polyaxon_cli.cli.getters.experiment import (
    get_experiment_job_or_local,
    get_project_experiment_or_local
)
from polyaxon_cli.cli.upload import upload
from polyaxon_cli.client import PolyaxonClient
from polyaxon_cli.client.exceptions import PolyaxonHTTPError, PolyaxonShouldExitError
from polyaxon_cli.logger import clean_outputs
from polyaxon_cli.managers.experiment import ExperimentManager
from polyaxon_cli.managers.experiment_job import ExperimentJobManager
from polyaxon_cli.utils import cache
from polyaxon_cli.utils.formatting import (
    Printer,
    dict_tabulate,
    get_meta_response,
    get_resources,
    list_dicts_to_tabulate
)
from polyaxon_cli.utils.log_handler import get_logs_handler
from polyaxon_cli.utils.validation import validate_tags
from polyaxon_client.exceptions import PolyaxonClientException


def get_experiment_details(experiment):  # pylint:disable=redefined-outer-name
    if experiment.description:
        Printer.print_header("Experiment description:")
        click.echo('{}\n'.format(experiment.description))

    if experiment.resources:
        get_resources(experiment.resources.to_dict(), header="Experiment resources:")

    if experiment.declarations:
        Printer.print_header("Experiment declarations:")
        dict_tabulate(experiment.declarations)

    if experiment.last_metric:
        Printer.print_header("Experiment last metrics:")
        dict_tabulate(experiment.last_metric)

    response = experiment.to_light_dict(
        humanize_values=True,
        exclude_attrs=[
            'uuid', 'config', 'project', 'experiments', 'description',
            'declarations', 'last_metric', 'resources', 'jobs', 'run_env'
        ])

    Printer.print_header("Experiment info:")
    dict_tabulate(Printer.add_status_color(response))


@click.group()
@click.option('--project', '-p', type=str, help="The project name, e.g. 'mnist' or 'adam/mnist'.")
@click.option('--experiment', '-xp', type=int, help="The experiment id number.")
@click.pass_context
@clean_outputs
def experiment(ctx, project, experiment):  # pylint:disable=redefined-outer-name
    """Commands for experiments."""
    ctx.obj = ctx.obj or {}
    ctx.obj['project'] = project
    ctx.obj['experiment'] = experiment


@experiment.command()
@click.option('--job', '-j', type=int, help="The job id.")
@click.pass_context
@clean_outputs
def get(ctx, job):
    """Get experiment or experiment job.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples for getting an experiment:

    \b
    ```bash
    $ polyaxon experiment get  # if experiment is cached
    ```

    \b
    ```bash
    $ polyaxon experiment --experiment=1 get
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 1 --project=cats-vs-dogs get
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 1 -p alain/cats-vs-dogs get
    ```

    Examples for getting an experiment job:

    \b
    ```bash
    $ polyaxon experiment get -j 1  # if experiment is cached
    ```

    \b
    ```bash
    $ polyaxon experiment --experiment=1 get --job=10
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 1 --project=cats-vs-dogs get -j 2
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 1 -p alain/cats-vs-dogs get -j 2
    ```
    """

    def get_experiment():
        try:
            response = PolyaxonClient().experiment.get_experiment(user, project_name, _experiment)
            cache.cache(config_manager=ExperimentManager, response=response)
        except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
            Printer.print_error('Could not load experiment `{}` info.'.format(_experiment))
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

        get_experiment_details(response)

    def get_experiment_job():
        try:
            response = PolyaxonClient().experiment_job.get_job(user,
                                                               project_name,
                                                               _experiment,
                                                               _job)
            cache.cache(config_manager=ExperimentJobManager, response=response)
        except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
            Printer.print_error('Could not get job `{}`.'.format(_job))
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

        if response.resources:
            get_resources(response.resources.to_dict(), header="Job resources:")

        response = Printer.add_status_color(response.to_light_dict(
            humanize_values=True,
            exclude_attrs=['uuid', 'definition', 'experiment', 'unique_name', 'resources']
        ))
        Printer.print_header("Job info:")
        dict_tabulate(response)

    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))

    if job:
        _job = get_experiment_job_or_local(job)
        get_experiment_job()
    else:
        get_experiment()


@experiment.command()
@click.pass_context
@clean_outputs
def delete(ctx):
    """Delete experiment.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Example:

    \b
    ```bash
    $ polyaxon experiment delete
    ```
    """
    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))
    if not click.confirm("Are sure you want to delete experiment `{}`".format(_experiment)):
        click.echo('Existing without deleting experiment.')
        sys.exit(1)

    try:
        response = PolyaxonClient().experiment.delete_experiment(
            user, project_name, _experiment)
        # Purge caching
        ExperimentManager.purge()
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not delete experiment `{}`.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    if response.status_code == 204:
        Printer.print_success("Experiment `{}` was delete successfully".format(_experiment))


@experiment.command()
@click.option('--name', type=str,
              help='Name of the experiment, must be unique within the project, could be none.')
@click.option('--description', type=str, help='Description of the experiment.')
@click.option('--tags', type=str, help='Tags of the experiment, comma separated values.')
@click.pass_context
@clean_outputs
def update(ctx, name, description, tags):
    """Update experiment.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples:

    \b
    ```bash
    $ polyaxon experiment -xp 2 update --description="new description for my experiments"
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 2 update --tags="foo, bar" --name="unique-name"
    ```
    """
    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))
    update_dict = {}

    if name:
        update_dict['name'] = name

    if description:
        update_dict['description'] = description

    tags = validate_tags(tags)
    if tags:
        update_dict['tags'] = tags

    if not update_dict:
        Printer.print_warning('No argument was provided to update the experiment.')
        sys.exit(0)

    try:
        response = PolyaxonClient().experiment.update_experiment(
            user, project_name, _experiment, update_dict)
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not update experiment `{}`.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    Printer.print_success("Experiment updated.")
    get_experiment_details(response)


@experiment.command()
@click.option('--yes', '-y', is_flag=True, default=False,
              help="Automatic yes to prompts. "
                   "Assume \"yes\" as answer to all prompts and run non-interactively.")
@click.pass_context
@clean_outputs
def stop(ctx, yes):
    """Stop experiment.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples:

    \b
    ```bash
    $ polyaxon experiment stop
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 2 stop
    ```
    """
    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))
    if not yes and not click.confirm("Are sure you want to stop "
                                     "experiment `{}`".format(_experiment)):
        click.echo('Existing without stopping experiment.')
        sys.exit(0)

    try:
        PolyaxonClient().experiment.stop(user, project_name, _experiment)
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not stop experiment `{}`.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    Printer.print_success("Experiment is being stopped.")


@experiment.command()
@click.option('--copy', '-c', is_flag=True, default=False,
              help="To copy the experiment before restarting.")
@click.option('--file', '-f', multiple=True, type=click.Path(exists=True),
              help="The polyaxon files to update with.")
@click.option('-u', is_flag=True, default=False,
              help="To upload the repo before restarting.")
@click.pass_context
@clean_outputs
def restart(ctx, copy, file, u):  # pylint:disable=redefined-builtin
    """Restart experiment.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples:

    \b
    ```bash
    $ polyaxon experiment --experiment=1 restart
    ```
    """
    config = None
    update_code = None
    if file:
        config = rhea.read(file)

    # Check if we need to upload
    if u:
        ctx.invoke(upload, sync=False)
        update_code = True

    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))
    try:
        if copy:
            response = PolyaxonClient().experiment.copy(
                user, project_name, _experiment, config=config, update_code=update_code)
            Printer.print_success('Experiment was copied with id {}'.format(response.id))
        else:
            response = PolyaxonClient().experiment.restart(
                user, project_name, _experiment, config=config, update_code=update_code)
            Printer.print_success('Experiment was restarted with id {}'.format(response.id))
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not restart experiment `{}`.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)


@experiment.command()
@click.option('--file', '-f', multiple=True, type=click.Path(exists=True),
              help="The polyaxon files to update with.")
@click.option('-u', is_flag=True, default=False,
              help="To upload the repo before resuming.")
@click.pass_context
@clean_outputs
def resume(ctx, file, u):  # pylint:disable=redefined-builtin
    """Resume experiment.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples:

    \b
    ```bash
    $ polyaxon experiment --experiment=1 resume
    ```
    """
    config = None
    update_code = None
    if file:
        config = rhea.read(file)

    # Check if we need to upload
    if u:
        ctx.invoke(upload, sync=False)
        update_code = True

    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))
    try:
        response = PolyaxonClient().experiment.resume(
            user, project_name, _experiment, config=config, update_code=update_code)
        Printer.print_success('Experiment was resumed with id {}'.format(response.id))
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not resume experiment `{}`.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)


@experiment.command()
@click.option('--page', type=int, help="To paginate through the list of jobs.")
@click.pass_context
@clean_outputs
def jobs(ctx, page):
    """List jobs for experiment.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples:

    \b
    ```bash
    $ polyaxon experiment --experiment=1 jobs
    ```
    """
    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))
    page = page or 1
    try:
        response = PolyaxonClient().experiment.list_jobs(
            user, project_name, _experiment, page=page)
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not get jobs for experiment `{}`.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    meta = get_meta_response(response)
    if meta:
        Printer.print_header('Jobs for experiment `{}`.'.format(_experiment))
        Printer.print_header('Navigation:')
        dict_tabulate(meta)
    else:
        Printer.print_header('No jobs found for experiment `{}`.'.format(_experiment))

    objects = [Printer.add_status_color(o.to_light_dict(humanize_values=True))
               for o in response['results']]
    objects = list_dicts_to_tabulate(objects)
    if objects:
        Printer.print_header("Jobs:")
        objects.pop('experiment', None)
        dict_tabulate(objects, is_list_dict=True)


@experiment.command()
@click.option('--job', '-j', type=int, help="The job id.")
@click.option('--page', type=int, help="To paginate through the list of statuses.")
@click.pass_context
@clean_outputs
def statuses(ctx, job, page):
    """Get experiment or experiment job statuses.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples getting experiment statuses:

    \b
    ```bash
    $ polyaxon experiment statuses
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 1 statuses
    ```

    Examples getting experiment job statuses:

    \b
    ```bash
    $ polyaxon experiment statuses -j 3
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 1 statuses --job 1
    ```
    """

    def get_experiment_statuses():
        try:
            response = PolyaxonClient().experiment.get_statuses(
                user, project_name, _experiment, page=page)
        except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
            Printer.print_error('Could get status for experiment `{}`.'.format(_experiment))
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

        meta = get_meta_response(response)
        if meta:
            Printer.print_header('Statuses for experiment `{}`.'.format(_experiment))
            Printer.print_header('Navigation:')
            dict_tabulate(meta)
        else:
            Printer.print_header('No statuses found for experiment `{}`.'.format(_experiment))

        objects = list_dicts_to_tabulate(
            [Printer.add_status_color(o.to_light_dict(humanize_values=True), status_key='status')
             for o in response['results']])
        if objects:
            Printer.print_header("Statuses:")
            objects.pop('experiment', None)
            dict_tabulate(objects, is_list_dict=True)

    def get_experiment_job_statuses():
        try:
            response = PolyaxonClient().experiment_job.get_statuses(user,
                                                                    project_name,
                                                                    _experiment,
                                                                    _job,
                                                                    page=page)
        except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
            Printer.print_error('Could not get status for job `{}`.'.format(job))
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

        meta = get_meta_response(response)
        if meta:
            Printer.print_header('Statuses for Job `{}`.'.format(_job))
            Printer.print_header('Navigation:')
            dict_tabulate(meta)
        else:
            Printer.print_header('No statuses found for job `{}`.'.format(_job))

        objects = list_dicts_to_tabulate(
            [Printer.add_status_color(o.to_light_dict(humanize_values=True), status_key='status')
             for o in response['results']])
        if objects:
            Printer.print_header("Statuses:")
            objects.pop('job', None)
            dict_tabulate(objects, is_list_dict=True)

    page = page or 1

    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))

    if job:
        _job = get_experiment_job_or_local(job)
        get_experiment_job_statuses()
    else:
        get_experiment_statuses()


@experiment.command()
@click.option('--job', '-j', type=int, help="The job id.")
@click.option('--gpu', '-g', is_flag=True, help="List experiment GPU resources.")
@click.pass_context
@clean_outputs
def resources(ctx, job, gpu):
    """Get experiment or experiment job resources.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples for getting experiment resources:

    \b
    ```bash
    $ polyaxon experiment -xp 19 resources
    ```

    For GPU resources

    \b
    ```bash
    $ polyaxon experiment -xp 19 resources --gpu
    ```

    Examples for getting experiment job resources:

    \b
    ```bash
    $ polyaxon experiment -xp 19 resources -j 1
    ```

    For GPU resources

    \b
    ```bash
    $ polyaxon experiment -xp 19 resources -j 1 --gpu
    ```
    """

    def get_experiment_resources():
        try:
            message_handler = Printer.gpu_resources if gpu else Printer.resources
            PolyaxonClient().experiment.resources(
                user, project_name, _experiment, message_handler=message_handler)
        except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
            Printer.print_error('Could not get resources for experiment `{}`.'.format(_experiment))
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

    def get_experiment_job_resources():
        try:
            message_handler = Printer.gpu_resources if gpu else Printer.resources
            PolyaxonClient().experiment_job.resources(user,
                                                      project_name,
                                                      _experiment,
                                                      _job,
                                                      message_handler=message_handler)
        except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
            Printer.print_error('Could not get resources for job `{}`.'.format(_job))
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))

    if job:
        _job = get_experiment_job_or_local(job)
        get_experiment_job_resources()
    else:
        get_experiment_resources()


@experiment.command()
@click.option('--job', '-j', type=int, help="The job id.")
@click.option('--past', '-p', is_flag=True, help="Show the past logs.")
@click.option('--follow', '-f', is_flag=True, default=False,
              help="Stream logs after showing past logs.")
@click.option('--hide_time', is_flag=True, default=False,
              help="Whether or not to hide timestamps from the log stream.")
@click.pass_context
@clean_outputs
def logs(ctx, job, past, follow, hide_time):
    """Get experiment or experiment job logs.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples for getting experiment logs:

    \b
    ```bash
    $ polyaxon experiment logs
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 10 -p mnist logs
    ```

    Examples for getting experiment job logs:

    \b
    ```bash
    $ polyaxon experiment -xp 1 -j 1 logs
    ```
    """

    def get_experiment_logs():
        if past:
            try:
                response = PolyaxonClient().experiment.logs(
                    user, project_name, _experiment, stream=False)
                get_logs_handler(handle_job_info=True,
                                 show_timestamp=not hide_time,
                                 stream=False)(response.content.decode().split('\n'))
                print()

                if not follow:
                    return
            except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
                if not follow:
                    Printer.print_error(
                        'Could not get logs for experiment `{}`.'.format(_experiment))
                    Printer.print_error(
                        'Error message `{}`.'.format(e))
                    sys.exit(1)

        try:
            PolyaxonClient().experiment.logs(
                user,
                project_name,
                _experiment,
                message_handler=get_logs_handler(handle_job_info=True,
                                                 show_timestamp=not hide_time))
        except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
            Printer.print_error('Could not get logs for experiment `{}`.'.format(_experiment))
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

    def get_experiment_job_logs():
        if past:
            try:
                response = PolyaxonClient().experiment_job.logs(
                    user,
                    project_name,
                    _experiment,
                    _job,
                    stream=False)
                get_logs_handler(handle_job_info=True,
                                 show_timestamp=not hide_time,
                                 stream=False)(response.content.decode().split('\n'))
                print()

                if not follow:
                    return
            except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
                if not follow:
                    Printer.print_error(
                        'Could not get logs for experiment `{}`.'.format(_experiment))
                    Printer.print_error(
                        'Error message `{}`.'.format(e))
                    sys.exit(1)

        try:
            PolyaxonClient().experiment_job.logs(
                user,
                project_name,
                _experiment,
                _job,
                message_handler=get_logs_handler(handle_job_info=True,
                                                 show_timestamp=not hide_time))
        except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
            Printer.print_error('Could not get logs for job `{}`.'.format(_job))
            Printer.print_error('Error message `{}`.'.format(e))
            sys.exit(1)

    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))

    if job:
        _job = get_experiment_job_or_local(job)
        get_experiment_job_logs()
    else:
        get_experiment_logs()


@experiment.command()
@click.pass_context
@clean_outputs
def outputs(ctx):
    """Download outputs for experiment.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples:

    \b
    ```bash
    $ polyaxon experiment -xp 1 outputs
    ```
    """
    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))
    try:
        PolyaxonClient().experiment.download_outputs(user, project_name, _experiment)
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not download outputs for experiment `{}`.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)
    Printer.print_success('Files downloaded.')


@experiment.command()
@click.pass_context
@clean_outputs
def bookmark(ctx):
    """Bookmark experiment.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples:

    \b
    ```bash
    $ polyaxon experiment bookmark
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 2 bookmark
    ```
    """
    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))
    try:
        PolyaxonClient().experiment.bookmark(user, project_name, _experiment)
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not bookmark experiment `{}`.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    Printer.print_success("Experiment is bookmarked.")


@experiment.command()
@click.pass_context
@clean_outputs
def unbookmark(ctx):
    """Unbookmark experiment.

    Uses [Caching](/references/polyaxon-cli/#caching)

    Examples:

    \b
    ```bash
    $ polyaxon experiment unbookmark
    ```

    \b
    ```bash
    $ polyaxon experiment -xp 2 unbookmark
    ```
    """
    user, project_name, _experiment = get_project_experiment_or_local(ctx.obj.get('project'),
                                                                      ctx.obj.get('experiment'))
    try:
        PolyaxonClient().experiment.unbookmark(user, project_name, _experiment)
    except (PolyaxonHTTPError, PolyaxonShouldExitError, PolyaxonClientException) as e:
        Printer.print_error('Could not unbookmark experiment `{}`.'.format(_experiment))
        Printer.print_error('Error message `{}`.'.format(e))
        sys.exit(1)

    Printer.print_success("Experiment is unbookmarked.")

[(59, 'click.group', 'click.group', ([], {}), False, 'import click\n'), (60, 'click.option', 'click.option', (['"""--project"""', '"""-p"""'], {'type': 'str', 'help': '"""The project name, e.g. \'mnist\' or \'adam/mnist\'."""'}), False, 'import click\n'), (61, 'click.option', 'click.option', (['"""--experiment"""', '"""-xp"""'], {'type': 'int', 'help': '"""The experiment id number."""'}), False, 'import click\n'), (72, 'click.option', 'click.option', (['"""--job"""', '"""-j"""'], {'type': 'int', 'help': '"""The job id."""'}), False, 'import click\n'), (204, 'click.option', 'click.option', (['"""--name"""'], {'type': 'str', 'help': '"""Name of the experiment, must be unique within the project, could be none."""'}), False, 'import click\n'), (206, 'click.option', 'click.option', (['"""--description"""'], {'type': 'str', 'help': '"""Description of the experiment."""'}), False, 'import click\n'), (207, 'click.option', 'click.option', (['"""--tags"""'], {'type': 'str', 'help': '"""Tags of the experiment, comma separated values."""'}), False, 'import click\n'), (258, 'click.option', 'click.option', (['"""--yes"""', '"""-y"""'], {'is_flag': '(True)', 'default': '(False)', 'help': '"""Automatic yes to prompts. Assume "yes" as answer to all prompts and run non-interactively."""'}), False, 'import click\n'), (298, 'click.option', 'click.option', (['"""--copy"""', '"""-c"""'], {'is_flag': '(True)', 'default': '(False)', 'help': '"""To copy the experiment before restarting."""'}), False, 'import click\n'), (302, 'click.option', 'click.option', (['"""-u"""'], {'is_flag': '(True)', 'default': '(False)', 'help': '"""To upload the repo before restarting."""'}), False, 'import click\n'), (348, 'click.option', 'click.option', (['"""-u"""'], {'is_flag': '(True)', 'default': '(False)', 'help': '"""To upload the repo before resuming."""'}), False, 'import click\n'), (387, 'click.option', 'click.option', (['"""--page"""'], {'type': 'int', 'help': '"""To paginate through the list of jobs."""'}), False, 'import click\n'), (431, 'click.option', 'click.option', (['"""--job"""', '"""-j"""'], {'type': 'int', 'help': '"""The job id."""'}), False, 'import click\n'), (432, 'click.option', 'click.option', (['"""--page"""'], {'type': 'int', 'help': '"""To paginate through the list of statuses."""'}), False, 'import click\n'), (531, 'click.option', 'click.option', (['"""--job"""', '"""-j"""'], {'type': 'int', 'help': '"""The job id."""'}), False, 'import click\n'), (532, 'click.option', 'click.option', (['"""--gpu"""', '"""-g"""'], {'is_flag': '(True)', 'help': '"""List experiment GPU resources."""'}), False, 'import click\n'), (603, 'click.option', 'click.option', (['"""--job"""', '"""-j"""'], {'type': 'int', 'help': '"""The job id."""'}), False, 'import click\n'), (604, 'click.option', 'click.option', (['"""--past"""', '"""-p"""'], {'is_flag': '(True)', 'help': '"""Show the past logs."""'}), False, 'import click\n'), (605, 'click.option', 'click.option', (['"""--follow"""', '"""-f"""'], {'is_flag': '(True)', 'default': '(False)', 'help': '"""Stream logs after showing past logs."""'}), False, 'import click\n'), (607, 'click.option', 'click.option', (['"""--hide_time"""'], {'is_flag': '(True)', 'default': '(False)', 'help': '"""Whether or not to hide timestamps from the log stream."""'}), False, 'import click\n'), (55, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Experiment info:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (237, 'polyaxon_cli.utils.validation.validate_tags', 'validate_tags', (['tags'], {}), False, 'from polyaxon_cli.utils.validation import validate_tags\n'), (253, 'polyaxon_cli.utils.formatting.Printer.print_success', 'Printer.print_success', (['"""Experiment updated."""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (294, 'polyaxon_cli.utils.formatting.Printer.print_success', 'Printer.print_success', (['"""Experiment is being stopped."""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (413, 'polyaxon_cli.utils.formatting.get_meta_response', 'get_meta_response', (['response'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (423, 'polyaxon_cli.utils.formatting.list_dicts_to_tabulate', 'list_dicts_to_tabulate', (['objects'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (738, 'polyaxon_cli.utils.formatting.Printer.print_success', 'Printer.print_success', (['"""Files downloaded."""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (770, 'polyaxon_cli.utils.formatting.Printer.print_success', 'Printer.print_success', (['"""Experiment is bookmarked."""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (802, 'polyaxon_cli.utils.formatting.Printer.print_success', 'Printer.print_success', (['"""Experiment is unbookmarked."""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (34, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Experiment description:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (41, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Experiment declarations:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (42, 'polyaxon_cli.utils.formatting.dict_tabulate', 'dict_tabulate', (['experiment.declarations'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (45, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Experiment last metrics:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (46, 'polyaxon_cli.utils.formatting.dict_tabulate', 'dict_tabulate', (['experiment.last_metric'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (56, 'polyaxon_cli.utils.formatting.Printer.add_status_color', 'Printer.add_status_color', (['response'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (155, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Job info:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (156, 'polyaxon_cli.utils.formatting.dict_tabulate', 'dict_tabulate', (['response'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (162, 'polyaxon_cli.cli.getters.experiment.get_experiment_job_or_local', 'get_experiment_job_or_local', (['job'], {}), False, 'from polyaxon_cli.cli.getters.experiment import get_experiment_job_or_local, get_project_experiment_or_local\n'), (186, 'click.echo', 'click.echo', (['"""Existing without deleting experiment."""'], {}), False, 'import click\n'), (187, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (193, 'polyaxon_cli.managers.experiment.ExperimentManager.purge', 'ExperimentManager.purge', ([], {}), False, 'from polyaxon_cli.managers.experiment import ExperimentManager\n'), (242, 'polyaxon_cli.utils.formatting.Printer.print_warning', 'Printer.print_warning', (['"""No argument was provided to update the experiment."""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (243, 'sys.exit', 'sys.exit', (['(0)'], {}), False, 'import sys\n'), (284, 'click.echo', 'click.echo', (['"""Existing without stopping experiment."""'], {}), False, 'import click\n'), (285, 'sys.exit', 'sys.exit', (['(0)'], {}), False, 'import sys\n'), (321, 'rhea.read', 'rhea.read', (['file'], {}), False, 'import rhea\n'), (300, 'click.Path', 'click.Path', ([], {'exists': '(True)'}), False, 'import click\n'), (367, 'rhea.read', 'rhea.read', (['file'], {}), False, 'import rhea\n'), (346, 'click.Path', 'click.Path', ([], {'exists': '(True)'}), False, 'import click\n'), (416, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Navigation:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (417, 'polyaxon_cli.utils.formatting.dict_tabulate', 'dict_tabulate', (['meta'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (425, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Jobs:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (427, 'polyaxon_cli.utils.formatting.dict_tabulate', 'dict_tabulate', (['objects'], {'is_list_dict': '(True)'}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (474, 'polyaxon_cli.utils.formatting.get_meta_response', 'get_meta_response', (['response'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (502, 'polyaxon_cli.utils.formatting.get_meta_response', 'get_meta_response', (['response'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (524, 'polyaxon_cli.cli.getters.experiment.get_experiment_job_or_local', 'get_experiment_job_or_local', (['job'], {}), False, 'from polyaxon_cli.cli.getters.experiment import get_experiment_job_or_local, get_project_experiment_or_local\n'), (596, 'polyaxon_cli.cli.getters.experiment.get_experiment_job_or_local', 'get_experiment_job_or_local', (['job'], {}), False, 'from polyaxon_cli.cli.getters.experiment import get_experiment_job_or_local, get_project_experiment_or_local\n'), (709, 'polyaxon_cli.cli.getters.experiment.get_experiment_job_or_local', 'get_experiment_job_or_local', (['job'], {}), False, 'from polyaxon_cli.cli.getters.experiment import get_experiment_job_or_local, get_project_experiment_or_local\n'), (128, 'polyaxon_cli.utils.cache.cache', 'cache.cache', ([], {'config_manager': 'ExperimentManager', 'response': 'response'}), False, 'from polyaxon_cli.utils import cache\n'), (142, 'polyaxon_cli.utils.cache.cache', 'cache.cache', ([], {'config_manager': 'ExperimentJobManager', 'response': 'response'}), False, 'from polyaxon_cli.utils import cache\n'), (197, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (251, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (292, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (342, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (383, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (411, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (477, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Navigation:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (478, 'polyaxon_cli.utils.formatting.dict_tabulate', 'dict_tabulate', (['meta'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (486, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Statuses:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (488, 'polyaxon_cli.utils.formatting.dict_tabulate', 'dict_tabulate', (['objects'], {'is_list_dict': '(True)'}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (505, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Navigation:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (506, 'polyaxon_cli.utils.formatting.dict_tabulate', 'dict_tabulate', (['meta'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (514, 'polyaxon_cli.utils.formatting.Printer.print_header', 'Printer.print_header', (['"""Statuses:"""'], {}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (516, 'polyaxon_cli.utils.formatting.dict_tabulate', 'dict_tabulate', (['objects'], {'is_list_dict': '(True)'}), False, 'from polyaxon_cli.utils.formatting import Printer, dict_tabulate, get_meta_response, get_resources, list_dicts_to_tabulate\n'), (737, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (768, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (800, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (132, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (146, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (472, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (500, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (577, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (590, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (666, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (703, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (190, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (246, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (288, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (377, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (406, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (641, 'polyaxon_cli.utils.log_handler.get_logs_handler', 'get_logs_handler', ([], {'handle_job_info': '(True)', 'show_timestamp': '(not hide_time)', 'stream': '(False)'}), False, 'from polyaxon_cli.utils.log_handler import get_logs_handler\n'), (661, 'polyaxon_cli.utils.log_handler.get_logs_handler', 'get_logs_handler', ([], {'handle_job_info': '(True)', 'show_timestamp': '(not hide_time)'}), False, 'from polyaxon_cli.utils.log_handler import get_logs_handler\n'), (677, 'polyaxon_cli.utils.log_handler.get_logs_handler', 'get_logs_handler', ([], {'handle_job_info': '(True)', 'show_timestamp': '(not hide_time)', 'stream': '(False)'}), False, 'from polyaxon_cli.utils.log_handler import get_logs_handler\n'), (698, 'polyaxon_cli.utils.log_handler.get_logs_handler', 'get_logs_handler', ([], {'handle_job_info': '(True)', 'show_timestamp': '(not hide_time)'}), False, 'from polyaxon_cli.utils.log_handler import get_logs_handler\n'), (733, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (764, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (796, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (127, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (138, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (332, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (336, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (467, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (492, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (572, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (582, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (654, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (657, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (690, 'sys.exit', 'sys.exit', (['(1)'], {}), False, 'import sys\n'), (693, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (639, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n'), (671, 'polyaxon_cli.client.PolyaxonClient', 'PolyaxonClient', ([], {}), False, 'from polyaxon_cli.client import PolyaxonClient\n')]