# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright 2016 Continuum Analytics, Inc.
#
# May be copied and distributed freely only as part of an Anaconda or
# Miniconda installation.
# -----------------------------------------------------------------------------
import sys

import click

try:
    from ruamel.yaml import YAML
except ImportError:
    from ruamel_yaml import YAML

yaml = YAML(typ='safe')
yaml.default_flow_style = False


class Unauthorized(click.ClickException):
    """User is not authorized"""


class JSONAPIError(click.ClickException):
    """A JSON API error container"""
    def __init__(self, errors):
        self.status = errors[0]['status']
        message = ', '.join(error['detail'] for error in errors)
        super(JSONAPIError, self).__init__(message)


class ServerError(click.ClickException):
    """Server error"""


class ClientError(click.ClickException):
    """Client error"""


def stdin_is_interactive():
    """True if stdin is a tty."""
    return sys.stdin.isatty()
