# Path: syncano_cli/base/command.py
# class BaseCommand(RegisterMixin):
#     """
#     Base command class. Provides utilities for register/loging (setup an account);
#     Has a predefined class for prompting and format output nicely in the console;
#     Stores also meta information about global config. Defines structures for command like config, eg.: hosting;
#     """
# 
#     def __init__(self, config_path):
#         self.config_path = config_path
#         self.connection = create_connection(config_path)
# 
#     formatter = Formatter()
#     prompter = Prompter()
# 
#     META_CONFIG = {
#         'default': [
#             {
#                 'name': 'key',
#                 'required': True,
#                 'info': ''
#             },
#             {
#                 'name': 'instance_name',
#                 'required': False,
#                 'info': 'You can setup it using following command: syncano instances default <instance_name>.'
#             },
#         ]
#     }
#     DEFAULT_SECTION = 'DEFAULT'
# 
#     COMMAND_CONFIG = None
#     COMMAND_SECTION = None
#     COMMAND_CONFIG_PATH = None
# 
#     def has_setup(self):
#         has_global = self.has_global_setup()
#         has_command = self.has_command_setup(self.COMMAND_CONFIG_PATH)
#         if has_global and has_command:
#             return True
# 
#         if not has_global:
#             self.formatter.write('Login or create an account in Syncano.', SpacedOpt())
#             email = self.prompter.prompt('email')
#             password = self.prompter.prompt('password', hide_input=True)
#             repeat_password = self.prompter.prompt('repeat password', hide_input=True)
#             password = self.validate_password(password, repeat_password)
#             self.do_login_or_register(email, password, self.config_path)
# 
#         if not has_command:
#             self.setup_command_config(self.config_path)
# 
#         return False
# 
#     def has_command_setup(self, config_path):
#         if not config_path:
#             return True
#         return False
# 
#     def setup_command_config(self, config_path):  # noqa;
#         # override this in the child class;
#         return True
# 
#     def has_global_setup(self):
#         if os.path.isfile(self.config_path):
#             ACCOUNT_CONFIG.read(self.config_path)
#             return self.check_section(ACCOUNT_CONFIG)
# 
#         return False
# 
#     @classmethod
#     def check_section(cls, config_parser, section=None):
#         section = section or cls.DEFAULT_SECTION
#         try:
#             config_parser.get(cls.DEFAULT_SECTION, 'key')
#         except NoOptionError:
#             return False
# 
#         config_vars = []
#         config_vars.extend(cls.META_CONFIG[cls.DEFAULT_SECTION.lower()])
#         if cls.COMMAND_CONFIG:
#             config_vars.extend(cls.COMMAND_CONFIG.get(cls.COMMAND_SECTION) or {})
#         for config_meta in config_vars:
#             var_name = config_meta['name']
#             required = config_meta['required']
#             if required and not config_parser.has_option(section, var_name):
#                 return False
#             elif not required and not config_parser.has_option(section, var_name):
#                 cls.formatter.write(
#                     'Missing "{}" in default config. {}'.format(var_name, config_meta['info']),
#                     WarningOpt(), SpacedOpt()
#                 )
# 
#         return True
# 
# Path: syncano_cli/base/options.py
# class BottomSpacedOpt(OptionsBase):
#     OPTIONS = ['space_bottom']
# 
#     def __init__(self, space_bottom=True, **kwargs):
#         super(BottomSpacedOpt, self).__init__(space_bottom=space_bottom, **kwargs)
# 
# class ColorSchema(object):
#     INFO = 'green'
#     PROMPT = 'cyan'
#     WARNING = 'yellow'
#     ERROR = 'red'
# 
# 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)
# 
# class TopSpacedOpt(OptionsBase):
#     OPTIONS = ['space_top']
# 
#     def __init__(self, space_top=True, **kwargs):
#         super(TopSpacedOpt, self).__init__(space_top=space_top, **kwargs)
# 
# class WarningOpt(OptionsBase):
#     OPTIONS = ['color']
# 
#     def __init__(self, color=ColorSchema.WARNING, **kwargs):
#         super(WarningOpt, self).__init__(color=color, **kwargs)
# 
# Path: syncano_cli/config.py
# ACCOUNT_CONFIG = ConfigParser()


import click
import six
from syncano_cli.base.command import BaseCommand
from syncano_cli.base.options import BottomSpacedOpt, ColorSchema, SpacedOpt, TopSpacedOpt, WarningOpt
from syncano_cli.config import ACCOUNT_CONFIG
    from ConfigParser import NoOptionError
    from configparser import NoOptionError
# -*- coding: utf-8 -*-

if six.PY2:
elif six.PY3:
else:
    raise ImportError()


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):

# Path: syncano_cli/base/command.py
# class BaseCommand(RegisterMixin):
#     """
#     Base command class. Provides utilities for register/loging (setup an account);
#     Has a predefined class for prompting and format output nicely in the console;
#     Stores also meta information about global config. Defines structures for command like config, eg.: hosting;
#     """
# 
#     def __init__(self, config_path):
#         self.config_path = config_path
#         self.connection = create_connection(config_path)
# 
#     formatter = Formatter()
#     prompter = Prompter()
# 
#     META_CONFIG = {
#         'default': [
#             {
#                 'name': 'key',
#                 'required': True,
#                 'info': ''
#             },
#             {
#                 'name': 'instance_name',
#                 'required': False,
#                 'info': 'You can setup it using following command: syncano instances default <instance_name>.'
#             },
#         ]
#     }
#     DEFAULT_SECTION = 'DEFAULT'
# 
#     COMMAND_CONFIG = None
#     COMMAND_SECTION = None
#     COMMAND_CONFIG_PATH = None
# 
#     def has_setup(self):
#         has_global = self.has_global_setup()
#         has_command = self.has_command_setup(self.COMMAND_CONFIG_PATH)
#         if has_global and has_command:
#             return True
# 
#         if not has_global:
#             self.formatter.write('Login or create an account in Syncano.', SpacedOpt())
#             email = self.prompter.prompt('email')
#             password = self.prompter.prompt('password', hide_input=True)
#             repeat_password = self.prompter.prompt('repeat password', hide_input=True)
#             password = self.validate_password(password, repeat_password)
#             self.do_login_or_register(email, password, self.config_path)
# 
#         if not has_command:
#             self.setup_command_config(self.config_path)
# 
#         return False
# 
#     def has_command_setup(self, config_path):
#         if not config_path:
#             return True
#         return False
# 
#     def setup_command_config(self, config_path):  # noqa;
#         # override this in the child class;
#         return True
# 
#     def has_global_setup(self):
#         if os.path.isfile(self.config_path):
#             ACCOUNT_CONFIG.read(self.config_path)
#             return self.check_section(ACCOUNT_CONFIG)
# 
#         return False
# 
#     @classmethod
#     def check_section(cls, config_parser, section=None):
#         section = section or cls.DEFAULT_SECTION
#         try:
#             config_parser.get(cls.DEFAULT_SECTION, 'key')
#         except NoOptionError:
#             return False
# 
#         config_vars = []
#         config_vars.extend(cls.META_CONFIG[cls.DEFAULT_SECTION.lower()])
#         if cls.COMMAND_CONFIG:
#             config_vars.extend(cls.COMMAND_CONFIG.get(cls.COMMAND_SECTION) or {})
#         for config_meta in config_vars:
#             var_name = config_meta['name']
#             required = config_meta['required']
#             if required and not config_parser.has_option(section, var_name):
#                 return False
#             elif not required and not config_parser.has_option(section, var_name):
#                 cls.formatter.write(
#                     'Missing "{}" in default config. {}'.format(var_name, config_meta['info']),
#                     WarningOpt(), SpacedOpt()
#                 )
# 
#         return True
# 
# Path: syncano_cli/base/options.py
# class BottomSpacedOpt(OptionsBase):
#     OPTIONS = ['space_bottom']
# 
#     def __init__(self, space_bottom=True, **kwargs):
#         super(BottomSpacedOpt, self).__init__(space_bottom=space_bottom, **kwargs)
# 
# class ColorSchema(object):
#     INFO = 'green'
#     PROMPT = 'cyan'
#     WARNING = 'yellow'
#     ERROR = 'red'
# 
# 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)
# 
# class TopSpacedOpt(OptionsBase):
#     OPTIONS = ['space_top']
# 
#     def __init__(self, space_top=True, **kwargs):
#         super(TopSpacedOpt, self).__init__(space_top=space_top, **kwargs)
# 
# class WarningOpt(OptionsBase):
#     OPTIONS = ['color']
# 
#     def __init__(self, color=ColorSchema.WARNING, **kwargs):
#         super(WarningOpt, self).__init__(color=color, **kwargs)
# 
# Path: syncano_cli/config.py
# ACCOUNT_CONFIG = ConfigParser()


# Path: syncano_cli/instance/command.py
import click
import six
from syncano_cli.base.command import BaseCommand
from syncano_cli.base.options import BottomSpacedOpt, ColorSchema, SpacedOpt, TopSpacedOpt, WarningOpt
from syncano_cli.config import ACCOUNT_CONFIG
    from ConfigParser import NoOptionError
    from configparser import NoOptionError

# -*- coding: utf-8 -*-

if six.PY2:
elif six.PY3:
else:
    raise ImportError()


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)