# Path: grafana_dashboards/errors.py
# class DashboardBuilderException(Exception):
# class DuplicateKeyError(DashboardBuilderException):
# class WrongComponentAttributeCountError(DashboardBuilderException):
# class UnregisteredComponentError(DashboardBuilderException):
# 
# Path: grafana_dashboards/components/base.py
# class ComponentRegistry(object):
#     def __init__(self):
#         super(ComponentRegistry, self).__init__()
#         self._types = {}
#         self._components = {}
#         for clazz in _get_subclasses(ComponentBase):
#             logger.info('Loading component type %s', clazz)
#             self._types[get_component_type(clazz)] = clazz
#             self._components[clazz] = {}
# 
#     def _class_for_type(self, component_type):
#         if isinstance(component_type, basestring):
#             component_type = self._types.get(component_type)
#         if self._components.get(component_type) is None:
#             raise errors.UnregisteredComponentError("No component of type '%s' found!" % component_type)
#         return component_type
# 
#     def add(self, component):
#         """
# 
#         :type component: dict
#         """
#         if len(component) > 2:
#             raise errors.WrongComponentAttributeCountError(
#                 'Component must have exactly 2 attributes - name and component type with data.'
#                 'This contains %s attributes' % len(component.keys()))
#         component_name = component.get('name')
#         if component_name is None:
#             logger.info("Component '%s' does not have 'name' attribute, skipping", component.keys())
#             return
#         component_type = None
#         for key in component.keys():
#             if key == 'name':
#                 continue
#             component_type = key
#             break
#         try:
#             clazz = self._class_for_type(component_type)
#         except errors.UnregisteredComponentError:
#             logger.info("Missing implementation class for component '%s', skipping", component_type)
#             return
#         logger.debug("Adding component '%s' with name '%s'", component_type, component_name)
#         components = self._get_component(clazz)
#         if component_name in components:
#             raise errors.DuplicateKeyError(
#                 "Key '%s' is already defined for component %s" % (component_name, component_type))
#         components[component_name] = self.create_component(clazz, component)
# 
#     def __getitem__(self, item):
#         return self._get_component(item).values()
# 
#     def _get_component(self, item):
#         component = self._components.get(item)
#         if component is None:
#             raise errors.UnregisteredComponentError("No component of type '%s' found!" % item)
#         return component
# 
#     def create_component(self, component_type, data):
#         return self._class_for_type(component_type)(data, self)
# 
#     def get_component(self, component_type, name):
#         component = self._get_component(component_type).get(name)
#         if component is None:
#             raise errors.UnregisteredComponentError("No component '%s' with name '%s' found!" % (component_type, name))
#         return component
# 
# class ComponentBase(object):
#     def __init__(self, data, registry):
#         """
# 
#         :type registry: ComponentRegistry
#         """
#         super(ComponentBase, self).__init__()
#         self.data = data[get_component_type(type(self))]
#         if self.data is None:
#             self.data = {}
#         self.name = data.get('name')
#         self.registry = registry


import pytest
from grafana_dashboards import errors
from grafana_dashboards.components.base import ComponentRegistry, ComponentBase
__author__ = 'Jakub Plichta <jakub.plichta@gmail.com>'


class TestBase(ComponentBase):
    pass


def test_registry_unregistered_component():
    registry = ComponentRegistry()

    registry.add({'name': 'name', 'non-existent': {}})
    with pytest.raises(errors.UnregisteredComponentError):
        registry.get_component('non-existent', 'name')


def test_registry_add_ignored():
    registry = ComponentRegistry()

    registry.add({})


def test_registry_add_component_with_too_few_fields():
    registry = ComponentRegistry()

    registry.add({'test-base': {}})
    with pytest.raises(errors.UnregisteredComponentError):
        registry.get_component('test-base', None)


def test_registry_add_component_with_too_many_fields():

# Path: grafana_dashboards/errors.py
# class DashboardBuilderException(Exception):
# class DuplicateKeyError(DashboardBuilderException):
# class WrongComponentAttributeCountError(DashboardBuilderException):
# class UnregisteredComponentError(DashboardBuilderException):
# 
# Path: grafana_dashboards/components/base.py
# class ComponentRegistry(object):
#     def __init__(self):
#         super(ComponentRegistry, self).__init__()
#         self._types = {}
#         self._components = {}
#         for clazz in _get_subclasses(ComponentBase):
#             logger.info('Loading component type %s', clazz)
#             self._types[get_component_type(clazz)] = clazz
#             self._components[clazz] = {}
# 
#     def _class_for_type(self, component_type):
#         if isinstance(component_type, basestring):
#             component_type = self._types.get(component_type)
#         if self._components.get(component_type) is None:
#             raise errors.UnregisteredComponentError("No component of type '%s' found!" % component_type)
#         return component_type
# 
#     def add(self, component):
#         """
# 
#         :type component: dict
#         """
#         if len(component) > 2:
#             raise errors.WrongComponentAttributeCountError(
#                 'Component must have exactly 2 attributes - name and component type with data.'
#                 'This contains %s attributes' % len(component.keys()))
#         component_name = component.get('name')
#         if component_name is None:
#             logger.info("Component '%s' does not have 'name' attribute, skipping", component.keys())
#             return
#         component_type = None
#         for key in component.keys():
#             if key == 'name':
#                 continue
#             component_type = key
#             break
#         try:
#             clazz = self._class_for_type(component_type)
#         except errors.UnregisteredComponentError:
#             logger.info("Missing implementation class for component '%s', skipping", component_type)
#             return
#         logger.debug("Adding component '%s' with name '%s'", component_type, component_name)
#         components = self._get_component(clazz)
#         if component_name in components:
#             raise errors.DuplicateKeyError(
#                 "Key '%s' is already defined for component %s" % (component_name, component_type))
#         components[component_name] = self.create_component(clazz, component)
# 
#     def __getitem__(self, item):
#         return self._get_component(item).values()
# 
#     def _get_component(self, item):
#         component = self._components.get(item)
#         if component is None:
#             raise errors.UnregisteredComponentError("No component of type '%s' found!" % item)
#         return component
# 
#     def create_component(self, component_type, data):
#         return self._class_for_type(component_type)(data, self)
# 
#     def get_component(self, component_type, name):
#         component = self._get_component(component_type).get(name)
#         if component is None:
#             raise errors.UnregisteredComponentError("No component '%s' with name '%s' found!" % (component_type, name))
#         return component
# 
# class ComponentBase(object):
#     def __init__(self, data, registry):
#         """
# 
#         :type registry: ComponentRegistry
#         """
#         super(ComponentBase, self).__init__()
#         self.data = data[get_component_type(type(self))]
#         if self.data is None:
#             self.data = {}
#         self.name = data.get('name')
#         self.registry = registry


# Path: tests/grafana_dashboards/components/test_base.py
import pytest
from grafana_dashboards import errors
from grafana_dashboards.components.base import ComponentRegistry, ComponentBase

__author__ = 'Jakub Plichta <jakub.plichta@gmail.com>'


class TestBase(ComponentBase):
    pass


def test_registry_unregistered_component():
    registry = ComponentRegistry()

    registry.add({'name': 'name', 'non-existent': {}})
    with pytest.raises(errors.UnregisteredComponentError):
        registry.get_component('non-existent', 'name')


def test_registry_add_ignored():
    registry = ComponentRegistry()

    registry.add({})


def test_registry_add_component_with_too_few_fields():
    registry = ComponentRegistry()

    registry.add({'test-base': {}})
    with pytest.raises(errors.UnregisteredComponentError):
        registry.get_component('test-base', None)


def test_registry_add_component_with_too_many_fields():

    registry = ComponentRegistry()