# Path: syncano_cli/base/connection.py
# def get_instance_name(config, instance_name):
#     ACCOUNT_CONFIG.read(config)
#     try:
#         instance_name = instance_name or ACCOUNT_CONFIG.get('DEFAULT', 'instance_name')
#     except (NoOptionError, NoSectionError):
#         return None
#     return instance_name
# 
# Path: syncano_cli/base/options.py
# class ErrorOpt(OptionsBase):
#     OPTIONS = ['color']
# 
#     def __init__(self, color=ColorSchema.ERROR, **kwargs):
#         super(ErrorOpt, self).__init__(color=color, **kwargs)
# 
# class SpacedOpt(OptionsBase):
#     OPTIONS = ['space_top', 'space_bottom']
# 
#     def __init__(self, space_top=True, space_bottom=True, **kwargs):
#         super(SpacedOpt, self).__init__(space_top=space_top, space_bottom=space_bottom, **kwargs)
# 
# Path: syncano_cli/config.py
# ACCOUNT_CONFIG_PATH = os.path.join(os.path.expanduser('~'), '.syncano')
# 
# Path: syncano_cli/instance/command.py
# class InstanceCommands(BaseCommand):
# 
#     def list(self):
#         return self.api_list()
# 
#     def api_list(self):
#         return self.connection.Instance.please.all()
# 
#     def details(self, instance_name):
#         return self.display_details(self.connection.Instance.please.get(name=instance_name))
# 
#     def delete(self, instance_name):
#         self.connection.Instance.please.delete(name=instance_name)
#         self.formatter.write('Instance `{}` deleted.'.format(instance_name), WarningOpt(), SpacedOpt())
# 
#     @classmethod
#     def set_default(cls, instance_name, config_path):
#         ACCOUNT_CONFIG.set('DEFAULT', 'instance_name', instance_name)
#         with open(config_path, 'wt') as fp:
#             ACCOUNT_CONFIG.write(fp)
# 
#     def create(self, instance_name, description=None):
#         kwargs = {
#             'name': instance_name
#         }
#         if description:
#             kwargs.update({'description': description})
#         instance = self.connection.Instance.please.create(**kwargs)
#         self.formatter.write('Instance `{}` created.'.format(instance.name), TopSpacedOpt())
#         self.formatter.write('To set this instance as default use: `syncano instances default {}`'.format(
#             instance.name
#         ), BottomSpacedOpt())
#         return instance
# 
#     def display_details(self, instance):
#         self.formatter.write('Details for Instance `{}`.'.format(instance.name), SpacedOpt())
# 
#         details_template = """Name: {instance.name}
# Description: {description}
# Owner: {instance.owner.email}
# Metadata: {instance.metadata}
# 
# """
# 
#         self.formatter.write_lines(
#             details_template.format(instance=instance, description=instance.description or u'N/A').splitlines())
# 
#     def display_list(self, instances):
#         try:
#             default_instance_name = ACCOUNT_CONFIG.get('DEFAULT', 'instance_name')
#         except NoOptionError:
#             default_instance_name = None
# 
#         self.formatter.write("Available Instances:", SpacedOpt())
# 
#         def get_name_label(name, default_instance_name):
#             if instance.name != default_instance_name:
#                 return u"{}".format(name)
#             return click.style(u"{} (default)".format(name), fg=ColorSchema.WARNING)
# 
#         for instance in instances:
#             self.formatter.write(u"* {name}: {description}".format(
#                 description=click.style(instance.description or u'no description', fg=ColorSchema.INFO),
#                 name=get_name_label(instance.name, default_instance_name)
#             ))
#         self.formatter.empty_line()
# 
# Path: syncano_cli/instance/exceptions.py
# class InstanceNameMismatchException(CLIBaseException):
#     default_message = u'Provided instance name do not match.'
# 
# class InstancesNotFoundException(CLIBaseException):
#     default_message = u'No instances found.'


import sys
import click
from syncano_cli.base.connection import get_instance_name
from syncano_cli.base.options import ErrorOpt, SpacedOpt
from syncano_cli.config import ACCOUNT_CONFIG_PATH
from syncano_cli.instance.command import InstanceCommands
from syncano_cli.instance.exceptions import InstanceNameMismatchException, InstancesNotFoundException
@click.pass_context
@click.option('--config', help=u'Account configuration file.', default=ACCOUNT_CONFIG_PATH)
def instances(ctx, config):
    """
    Manage your Instances. Instance is an equivalent of a project or a set of data.
    It contains information about Sockets, Data Classes, Data Objects and more.
    You can own and/or belong to multiple Instances.
    """
    instance_commands = InstanceCommands(config)
    instance_commands.has_setup()
    ctx.obj['instance_commands'] = instance_commands
    ctx.obj['config'] = config or ACCOUNT_CONFIG_PATH


@instances.command()
@click.pass_context
def list(ctx):
    """Display a list with defined Instances in Syncano. List displays only name and description."""
    instance_commands = ctx.obj['instance_commands']
    syncano_instances = instance_commands.list()
    if not syncano_instances:
        raise InstancesNotFoundException()
    instance_commands.display_list(syncano_instances)


@instances.command()
@click.pass_context
@click.argument('instance_name', required=False)
def details(ctx, instance_name):
    """Display Syncano Instance details."""

# Path: syncano_cli/base/connection.py
# def get_instance_name(config, instance_name):
#     ACCOUNT_CONFIG.read(config)
#     try:
#         instance_name = instance_name or ACCOUNT_CONFIG.get('DEFAULT', 'instance_name')
#     except (NoOptionError, NoSectionError):
#         return None
#     return instance_name
# 
# Path: syncano_cli/base/options.py
# class ErrorOpt(OptionsBase):
#     OPTIONS = ['color']
# 
#     def __init__(self, color=ColorSchema.ERROR, **kwargs):
#         super(ErrorOpt, self).__init__(color=color, **kwargs)
# 
# class SpacedOpt(OptionsBase):
#     OPTIONS = ['space_top', 'space_bottom']
# 
#     def __init__(self, space_top=True, space_bottom=True, **kwargs):
#         super(SpacedOpt, self).__init__(space_top=space_top, space_bottom=space_bottom, **kwargs)
# 
# Path: syncano_cli/config.py
# ACCOUNT_CONFIG_PATH = os.path.join(os.path.expanduser('~'), '.syncano')
# 
# Path: syncano_cli/instance/command.py
# class InstanceCommands(BaseCommand):
# 
#     def list(self):
#         return self.api_list()
# 
#     def api_list(self):
#         return self.connection.Instance.please.all()
# 
#     def details(self, instance_name):
#         return self.display_details(self.connection.Instance.please.get(name=instance_name))
# 
#     def delete(self, instance_name):
#         self.connection.Instance.please.delete(name=instance_name)
#         self.formatter.write('Instance `{}` deleted.'.format(instance_name), WarningOpt(), SpacedOpt())
# 
#     @classmethod
#     def set_default(cls, instance_name, config_path):
#         ACCOUNT_CONFIG.set('DEFAULT', 'instance_name', instance_name)
#         with open(config_path, 'wt') as fp:
#             ACCOUNT_CONFIG.write(fp)
# 
#     def create(self, instance_name, description=None):
#         kwargs = {
#             'name': instance_name
#         }
#         if description:
#             kwargs.update({'description': description})
#         instance = self.connection.Instance.please.create(**kwargs)
#         self.formatter.write('Instance `{}` created.'.format(instance.name), TopSpacedOpt())
#         self.formatter.write('To set this instance as default use: `syncano instances default {}`'.format(
#             instance.name
#         ), BottomSpacedOpt())
#         return instance
# 
#     def display_details(self, instance):
#         self.formatter.write('Details for Instance `{}`.'.format(instance.name), SpacedOpt())
# 
#         details_template = """Name: {instance.name}
# Description: {description}
# Owner: {instance.owner.email}
# Metadata: {instance.metadata}
# 
# """
# 
#         self.formatter.write_lines(
#             details_template.format(instance=instance, description=instance.description or u'N/A').splitlines())
# 
#     def display_list(self, instances):
#         try:
#             default_instance_name = ACCOUNT_CONFIG.get('DEFAULT', 'instance_name')
#         except NoOptionError:
#             default_instance_name = None
# 
#         self.formatter.write("Available Instances:", SpacedOpt())
# 
#         def get_name_label(name, default_instance_name):
#             if instance.name != default_instance_name:
#                 return u"{}".format(name)
#             return click.style(u"{} (default)".format(name), fg=ColorSchema.WARNING)
# 
#         for instance in instances:
#             self.formatter.write(u"* {name}: {description}".format(
#                 description=click.style(instance.description or u'no description', fg=ColorSchema.INFO),
#                 name=get_name_label(instance.name, default_instance_name)
#             ))
#         self.formatter.empty_line()
# 
# Path: syncano_cli/instance/exceptions.py
# class InstanceNameMismatchException(CLIBaseException):
#     default_message = u'Provided instance name do not match.'
# 
# class InstancesNotFoundException(CLIBaseException):
#     default_message = u'No instances found.'


# Path: syncano_cli/instance/commands.py
import sys
import click
from syncano_cli.base.connection import get_instance_name
from syncano_cli.base.options import ErrorOpt, SpacedOpt
from syncano_cli.config import ACCOUNT_CONFIG_PATH
from syncano_cli.instance.command import InstanceCommands
from syncano_cli.instance.exceptions import InstanceNameMismatchException, InstancesNotFoundException

@click.pass_context
@click.option('--config', help=u'Account configuration file.', default=ACCOUNT_CONFIG_PATH)
def instances(ctx, config):
    """
    Manage your Instances. Instance is an equivalent of a project or a set of data.
    It contains information about Sockets, Data Classes, Data Objects and more.
    You can own and/or belong to multiple Instances.
    """
    instance_commands = InstanceCommands(config)
    instance_commands.has_setup()
    ctx.obj['instance_commands'] = instance_commands
    ctx.obj['config'] = config or ACCOUNT_CONFIG_PATH


@instances.command()
@click.pass_context
def list(ctx):
    """Display a list with defined Instances in Syncano. List displays only name and description."""
    instance_commands = ctx.obj['instance_commands']
    syncano_instances = instance_commands.list()
    if not syncano_instances:
        raise InstancesNotFoundException()
    instance_commands.display_list(syncano_instances)


@instances.command()
@click.pass_context
@click.argument('instance_name', required=False)
def details(ctx, instance_name):
    """Display Syncano Instance details."""

    instance_name = get_instance_name(ctx.obj['config'], instance_name)  # default one if no provided;