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

import os

from bioblend import galaxy

from biomaj2galaxy import config, pass_context
from biomaj2galaxy.io import info, warn

import click


CONFIG_TEMPLATE = """## BioMAJ2Galaxy: Global Configuration File.
# Each stanza should contain a single Galaxy server to interact with.
#
# You can set the key __default to the name of a default instance
__default: local

local:
    url: "%(url)s"
    apikey: "%(apikey)s"
"""

SUCCESS_MESSAGE = (
    "Ready to go! Type `biomaj2galaxy` to get a list of commands you can execute."
)


@click.command()
@pass_context
def init(ctx, url=None, api_key=None, admin=False, **kwds):
    """Help initialize global configuration (in home directory)
    """

    click.echo("""Welcome to BioMAJ2Galaxy""")
    if os.path.exists(config.global_config_path()):
        info("Your biomaj2galaxy configuration already exists. Please edit it instead: %s" % config.global_config_path())
        return 0

    while True:
        # Check environment
        url = click.prompt("url")
        apikey = click.prompt("apikey")

        info("Testing connection...")
        try:
            instance = galaxy.GalaxyInstance(url=url, key=apikey)
            instance.libraries.get_libraries()
            # We do a connection test during startup.
            info("Ok! Everything looks good.")
            break
        except Exception as e:
            warn("Error, we could not access the configuration data for your instance: %s", e)
            should_break = click.prompt("Continue despite inability to contact this instance? [y/n]")
            if should_break in ('Y', 'y'):
                break

    config_path = config.global_config_path()
    if os.path.exists(config_path):
        warn("File %s already exists, refusing to overwrite." % config_path)
        return -1

    with open(config_path, "w") as f:
        f.write(CONFIG_TEMPLATE % {
            'url': url,
            'apikey': apikey,
        })
        info(SUCCESS_MESSAGE)

[((32, 1, 32, 16), 'click.command', 'click.command', ({}, {}), '()', False, 'import click\n'), ((38, 4, 38, 46), 'click.echo', 'click.echo', ({(38, 15, 38, 45): '"""Welcome to BioMAJ2Galaxy"""'}, {}), "('Welcome to BioMAJ2Galaxy')", False, 'import click\n'), ((61, 18, 61, 45), 'biomaj2galaxy.config.global_config_path', 'config.global_config_path', ({}, {}), '()', False, 'from biomaj2galaxy import config, pass_context\n'), ((62, 7, 62, 34), 'os.path.exists', 'os.path.exists', ({(62, 22, 62, 33): 'config_path'}, {}), '(config_path)', False, 'import os\n'), ((39, 22, 39, 49), 'biomaj2galaxy.config.global_config_path', 'config.global_config_path', ({}, {}), '()', False, 'from biomaj2galaxy import config, pass_context\n'), ((45, 14, 45, 33), 'click.prompt', 'click.prompt', ({(45, 27, 45, 32): '"""url"""'}, {}), "('url')", False, 'import click\n'), ((46, 17, 46, 39), 'click.prompt', 'click.prompt', ({(46, 30, 46, 38): '"""apikey"""'}, {}), "('apikey')", False, 'import click\n'), ((48, 8, 48, 37), 'biomaj2galaxy.io.info', 'info', ({(48, 13, 48, 36): '"""Testing connection..."""'}, {}), "('Testing connection...')", False, 'from biomaj2galaxy.io import info, warn\n'), ((63, 8, 63, 76), 'biomaj2galaxy.io.warn', 'warn', ({(63, 13, 63, 75): "('File %s already exists, refusing to overwrite.' % config_path)"}, {}), "('File %s already exists, refusing to overwrite.' % config_path)", False, 'from biomaj2galaxy.io import info, warn\n'), ((71, 8, 71, 29), 'biomaj2galaxy.io.info', 'info', ({(71, 13, 71, 28): 'SUCCESS_MESSAGE'}, {}), '(SUCCESS_MESSAGE)', False, 'from biomaj2galaxy.io import info, warn\n'), ((50, 23, 50, 65), 'bioblend.galaxy.GalaxyInstance', 'galaxy.GalaxyInstance', (), '', False, 'from bioblend import galaxy\n'), ((53, 12, 53, 46), 'biomaj2galaxy.io.info', 'info', ({(53, 17, 53, 45): '"""Ok! Everything looks good."""'}, {}), "('Ok! Everything looks good.')", False, 'from biomaj2galaxy.io import info, warn\n'), ((40, 93, 40, 120), 'biomaj2galaxy.config.global_config_path', 'config.global_config_path', ({}, {}), '()', False, 'from biomaj2galaxy import config, pass_context\n'), ((56, 12, 56, 94), 'biomaj2galaxy.io.warn', 'warn', ({(56, 17, 56, 90): '"""Error, we could not access the configuration data for your instance: %s"""', (56, 92, 56, 93): 'e'}, {}), "('Error, we could not access the configuration data for your instance: %s',\n    e)", False, 'from biomaj2galaxy.io import info, warn\n'), ((57, 27, 57, 101), 'click.prompt', 'click.prompt', ({(57, 40, 57, 100): '"""Continue despite inability to contact this instance? [y/n]"""'}, {}), "('Continue despite inability to contact this instance? [y/n]')", False, 'import click\n')]