# Path: grafana_dashboards/common.py
# def get_component_type(clazz):
#     """
# 
#     :type clazz: type
#     """
#     return _all_cap_re.sub(r'\1-\2', _first_cap_re.sub(r'\1-\2', clazz.__name__)).lower()
# 
# Path: grafana_dashboards/components/base.py
# class JsonListGenerator(JsonGenerator):
#     def __init__(self, data, registry, item_base_class):
#         super(JsonListGenerator, self).__init__(data, registry)
#         self.component_item_types = [get_component_type(clazz) for clazz in _get_subclasses(item_base_class)]
# 
#     def gen_json_from_data(self, data, context):
#         super(JsonListGenerator, self).gen_json_from_data(data, context)
#         result_list = []
#         for items in data:
#             self.gen_item_json(items, result_list)
#         return result_list
# 
#     def gen_item_json(self, items, result_list):
#         if isinstance(items, basestring):
#             # this is component without context
#             result_list += self.registry.get_component(type(self), items).gen_json()
#         else:
#             self._gen_item_json_with_context(items, result_list)
# 
#     def _gen_item_json_with_context(self, items, result_list):
#         # TODO add check for dictionary
#         for (item_type, item_data) in items.items():
#             if item_type not in self.component_item_types:
#                 # this is named component with context
#                 for context in Context.create_context(item_data, get_placeholders(item_type)):
#                     result_list += self.registry.get_component(type(self), item_type).gen_json(context)
#             else:
#                 # this is inplace defined component
#                 item = self.registry.create_component(item_type, {item_type: item_data}).gen_json()
#                 if isinstance(item, list):
#                     result_list += item
#                 else:
#                     result_list.append(item)
# 
# class JsonGenerator(ComponentBase):
# 
#     _copy_fields = set()
# 
#     def gen_json(self, context=Context()):
#         return self.gen_json_from_data(context.expand_placeholders(self.data), context)
# 
#     def gen_json_from_data(self, data, context):
#         component_type = get_component_type(type(self))
#         if self.name:
#             logger.debug("Processing component '%s' with name '%s' from template '%s'",
#                          component_type, context.expand_placeholders(self.name), self.name)
#         else:
#             logger.debug("Processing anonymous component '%s'", component_type)
#         json = {}
#         for field in self._copy_fields:
#             if field in data:
#                 json[field] = data[field]
#         return json
# 
# Path: grafana_dashboards/components/panels.py
# class Panels(JsonListGenerator):
#     def __init__(self, data, registry):
#         super(Panels, self).__init__(data, registry, PanelsItemBase)


from grafana_dashboards.common import get_component_type
from grafana_dashboards.components.base import JsonListGenerator, JsonGenerator
from grafana_dashboards.components.panels import Panels
# limitations under the License.
from __future__ import unicode_literals


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


class Rows(JsonListGenerator):
    def __init__(self, data, registry):
        super(Rows, self).__init__(data, registry, RowsItemBase)


class RowsItemBase(JsonGenerator):
    pass


class Row(RowsItemBase):

    _copy_fields = {'repeat'}

    def gen_json_from_data(self, data, context):
        row_json = super(Row, self).gen_json_from_data(data, context)
        row_json.update({
            'title': data.get('title', ''),
            'height': data.get('height', '250px'),
            'showTitle': data.get('showTitle', False),
            'collapse': data.get('collapse', False),
            'panels': []
        })
        if get_component_type(Panels) in data:

# Path: grafana_dashboards/common.py
# def get_component_type(clazz):
#     """
# 
#     :type clazz: type
#     """
#     return _all_cap_re.sub(r'\1-\2', _first_cap_re.sub(r'\1-\2', clazz.__name__)).lower()
# 
# Path: grafana_dashboards/components/base.py
# class JsonListGenerator(JsonGenerator):
#     def __init__(self, data, registry, item_base_class):
#         super(JsonListGenerator, self).__init__(data, registry)
#         self.component_item_types = [get_component_type(clazz) for clazz in _get_subclasses(item_base_class)]
# 
#     def gen_json_from_data(self, data, context):
#         super(JsonListGenerator, self).gen_json_from_data(data, context)
#         result_list = []
#         for items in data:
#             self.gen_item_json(items, result_list)
#         return result_list
# 
#     def gen_item_json(self, items, result_list):
#         if isinstance(items, basestring):
#             # this is component without context
#             result_list += self.registry.get_component(type(self), items).gen_json()
#         else:
#             self._gen_item_json_with_context(items, result_list)
# 
#     def _gen_item_json_with_context(self, items, result_list):
#         # TODO add check for dictionary
#         for (item_type, item_data) in items.items():
#             if item_type not in self.component_item_types:
#                 # this is named component with context
#                 for context in Context.create_context(item_data, get_placeholders(item_type)):
#                     result_list += self.registry.get_component(type(self), item_type).gen_json(context)
#             else:
#                 # this is inplace defined component
#                 item = self.registry.create_component(item_type, {item_type: item_data}).gen_json()
#                 if isinstance(item, list):
#                     result_list += item
#                 else:
#                     result_list.append(item)
# 
# class JsonGenerator(ComponentBase):
# 
#     _copy_fields = set()
# 
#     def gen_json(self, context=Context()):
#         return self.gen_json_from_data(context.expand_placeholders(self.data), context)
# 
#     def gen_json_from_data(self, data, context):
#         component_type = get_component_type(type(self))
#         if self.name:
#             logger.debug("Processing component '%s' with name '%s' from template '%s'",
#                          component_type, context.expand_placeholders(self.name), self.name)
#         else:
#             logger.debug("Processing anonymous component '%s'", component_type)
#         json = {}
#         for field in self._copy_fields:
#             if field in data:
#                 json[field] = data[field]
#         return json
# 
# Path: grafana_dashboards/components/panels.py
# class Panels(JsonListGenerator):
#     def __init__(self, data, registry):
#         super(Panels, self).__init__(data, registry, PanelsItemBase)


# Path: grafana_dashboards/components/rows.py
from grafana_dashboards.common import get_component_type
from grafana_dashboards.components.base import JsonListGenerator, JsonGenerator
from grafana_dashboards.components.panels import Panels

# limitations under the License.
from __future__ import unicode_literals


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


class Rows(JsonListGenerator):
    def __init__(self, data, registry):
        super(Rows, self).__init__(data, registry, RowsItemBase)


class RowsItemBase(JsonGenerator):
    pass


class Row(RowsItemBase):

    _copy_fields = {'repeat'}

    def gen_json_from_data(self, data, context):
        row_json = super(Row, self).gen_json_from_data(data, context)
        row_json.update({
            'title': data.get('title', ''),
            'height': data.get('height', '250px'),
            'showTitle': data.get('showTitle', False),
            'collapse': data.get('collapse', False),
            'panels': []
        })
        if get_component_type(Panels) in data:

            row_json['panels'] = self.registry.create_component(Panels, data).gen_json()