# Path: syncano_cli/base/exceptions.py
# class BadCredentialsException(CLIBaseException):
#     default_message = u'Wrong login credentials provided.'
# 
# class InstanceNotFoundException(CLIBaseException):
#     default_message = u'Instance `{}` not found.'
# 
# Path: syncano_cli/config.py
# ACCOUNT_CONFIG = ConfigParser()
# 
# ACCOUNT_CONFIG_PATH = os.path.join(os.path.expanduser('~'), '.syncano')


import six
import syncano
from syncano.exceptions import SyncanoException
from syncano_cli.base.exceptions import BadCredentialsException, InstanceNotFoundException
from syncano_cli.config import ACCOUNT_CONFIG, ACCOUNT_CONFIG_PATH
    from ConfigParser import NoOptionError, NoSectionError
    from configparser import NoOptionError, NoSectionError

def create_connection(config, instance_name=None):

    config = config or ACCOUNT_CONFIG_PATH
    ACCOUNT_CONFIG.read(config)
    try:
        api_key = ACCOUNT_CONFIG.get('DEFAULT', 'key')
    except NoOptionError:
        api_key = ''  # initialize with empty one;

    connection_dict = {
        'api_key': api_key,
    }
    instance_name = get_instance_name(config, instance_name)
    if instance_name:
        connection_dict['instance_name'] = instance_name
    try:
        return syncano.connect(**connection_dict)
    except SyncanoException:
        raise BadCredentialsException()


def get_instance(config, instance_name, connection=None):
    config = config or ACCOUNT_CONFIG_PATH
    instance_name = get_instance_name(config, instance_name)

    connection = connection or create_connection(config, instance_name=instance_name)
    try:
        return connection.Instance.please.get(name=instance_name)
    except SyncanoException:

# Path: syncano_cli/base/exceptions.py
# class BadCredentialsException(CLIBaseException):
#     default_message = u'Wrong login credentials provided.'
# 
# class InstanceNotFoundException(CLIBaseException):
#     default_message = u'Instance `{}` not found.'
# 
# Path: syncano_cli/config.py
# ACCOUNT_CONFIG = ConfigParser()
# 
# ACCOUNT_CONFIG_PATH = os.path.join(os.path.expanduser('~'), '.syncano')


# Path: syncano_cli/base/connection.py
import six
import syncano
from syncano.exceptions import SyncanoException
from syncano_cli.base.exceptions import BadCredentialsException, InstanceNotFoundException
from syncano_cli.config import ACCOUNT_CONFIG, ACCOUNT_CONFIG_PATH
    from ConfigParser import NoOptionError, NoSectionError
    from configparser import NoOptionError, NoSectionError


def create_connection(config, instance_name=None):

    config = config or ACCOUNT_CONFIG_PATH
    ACCOUNT_CONFIG.read(config)
    try:
        api_key = ACCOUNT_CONFIG.get('DEFAULT', 'key')
    except NoOptionError:
        api_key = ''  # initialize with empty one;

    connection_dict = {
        'api_key': api_key,
    }
    instance_name = get_instance_name(config, instance_name)
    if instance_name:
        connection_dict['instance_name'] = instance_name
    try:
        return syncano.connect(**connection_dict)
    except SyncanoException:
        raise BadCredentialsException()


def get_instance(config, instance_name, connection=None):
    config = config or ACCOUNT_CONFIG_PATH
    instance_name = get_instance_name(config, instance_name)

    connection = connection or create_connection(config, instance_name=instance_name)
    try:
        return connection.Instance.please.get(name=instance_name)
    except SyncanoException:

        raise InstanceNotFoundException(format_args=[instance_name])