# Copyright 2013: Mirantis Inc.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import six

from rally.common.i18n import _
from rally.common import log as logging

LOG = logging.getLogger(__name__)


class RallyException(Exception):
    """Base Rally Exception

    To correctly use this class, inherit from it and define
    a "msg_fmt" property. That msg_fmt will get printf'd
    with the keyword arguments provided to the constructor.

    """
    msg_fmt = _("%(message)s")

    def __init__(self, message=None, **kwargs):
        self.kwargs = kwargs

        if "%(message)s" in self.msg_fmt:
            kwargs.update({"message": message})

        super(RallyException, self).__init__(self.msg_fmt % kwargs)

    def format_message(self):
        return six.text_type(self)


class ImmutableException(RallyException):
    msg_fmt = _("This object is immutable.")


class InvalidArgumentsException(RallyException):
    msg_fmt = _("Invalid arguments: '%(message)s'")


class InvalidConfigException(RallyException):
    msg_fmt = _("This config has invalid schema: `%(message)s`")


class InvalidRunnerResult(RallyException):
    msg_fmt = _("Type of result of `%(name)s` runner should be"
                " `base.ScenarioRunnerResult`. Got: `%(results_type)s`")


class InvalidTaskException(InvalidConfigException):
    msg_fmt = _("Task config is invalid: `%(message)s`")


class NotFoundScenarios(InvalidTaskException):
    msg_fmt = _("There are no benchmark scenarios with names: `%(names)s`.")


class InvalidBenchmarkConfig(InvalidTaskException):
    msg_fmt = _("Input task is invalid!\n\n"
                "Benchmark %(name)s[%(pos)s] has wrong configuration"
                "\nBenchmark configuration:\n%(config)s\n"
                "\nReason:\n %(reason)s")


class NotFoundException(RallyException):
    msg_fmt = _("The resource can not be found.")


class PluginNotFound(NotFoundException):
    msg_fmt = _("There is no plugin with name: %(name)s in "
                "%(namespace)s namespace.")


class PluginWithSuchNameExists(RallyException):
    msg_fmt = _("Plugin with such name: %(name)s already exists in "
                "%(namespace)s namespace")


class NoSuchConfigField(NotFoundException):
    msg_fmt = _("There is no field in the task config with name `%(name)s`.")