# Path: pytablereader/error.py
# class ValidationError(Exception):
#     """
#     Exception raised when data is not properly formatted.
#     """
# 
# Path: pytablereader/formatter.py
# class TableFormatter(LoaderAcceptor, TableFormatterInterface):
#     """
#     The abstract class of |TableData| formatter.
#     """
# 
#     def _validate_source_data(self):
#         if not self._source_data:
#             raise DataError("source data is empty")
# 
#     def __init__(self, source_data):
#         self._source_data = source_data
# 
#         self._validate_source_data()
# 
#     def _extract_type_hints(self, headers=None):
#         if self._loader.type_hints:
#             return self._loader.type_hints
# 
#         if not self._loader.type_hint_rules or not headers:
#             return []
# 
#         type_hints = []
#         for header in headers:
#             for regexp, type_hint in self._loader.type_hint_rules.items():
#                 if regexp.search(header):
#                     type_hints.append(type_hint)
#                     break
#             else:
#                 type_hints.append(None)
# 
#         logger.debug(
#             dedent(
#                 """\
#                 extracted type hints:
#                 {}
#                 """
#             ).format(
#                 json.dumps(
#                     OrderedDict(
#                         {header: str(type_hint) for header, type_hint in zip(headers, type_hints)}
#                     ),
#                     indent=4,
#                 )
#             )
#         )
# 
#         return type_hints
# 
# Path: pytablereader/json/formatter.py
# class SingleJsonTableConverterBase(JsonConverter):
#     def _make_table_name(self):
#         kv_mapping = self._loader._get_basic_tablename_keyvalue_mapping()
#         kv_mapping[tnt.KEY] = self._loader.get_format_key()
# 
#         if self._loader.source_type == SourceType.FILE:
#             kv_mapping[tnt.DEFAULT] = tnt.FILENAME
#         elif self._loader.source_type == SourceType.TEXT:
#             kv_mapping[tnt.DEFAULT] = tnt.KEY
# 
#         return self._loader._expand_table_name_format(kv_mapping)


import jsonschema
from tabledata import TableData
from ..error import ValidationError
from ..formatter import TableFormatter
from ..json.formatter import SingleJsonTableConverterBase
"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""




class FlatJsonTableConverter(SingleJsonTableConverterBase):
    """
    A concrete class of JSON table data formatter.
    """

    @property
    def _schema(self):
        return {"type": "object", "additionalProperties": self._VALUE_TYPE_SCHEMA}

    def _validate_source_data(self):
        for json_record in self._buffer:
            try:
                jsonschema.validate(json_record, self._schema)
            except jsonschema.ValidationError as e:

# Path: pytablereader/error.py
# class ValidationError(Exception):
#     """
#     Exception raised when data is not properly formatted.
#     """
# 
# Path: pytablereader/formatter.py
# class TableFormatter(LoaderAcceptor, TableFormatterInterface):
#     """
#     The abstract class of |TableData| formatter.
#     """
# 
#     def _validate_source_data(self):
#         if not self._source_data:
#             raise DataError("source data is empty")
# 
#     def __init__(self, source_data):
#         self._source_data = source_data
# 
#         self._validate_source_data()
# 
#     def _extract_type_hints(self, headers=None):
#         if self._loader.type_hints:
#             return self._loader.type_hints
# 
#         if not self._loader.type_hint_rules or not headers:
#             return []
# 
#         type_hints = []
#         for header in headers:
#             for regexp, type_hint in self._loader.type_hint_rules.items():
#                 if regexp.search(header):
#                     type_hints.append(type_hint)
#                     break
#             else:
#                 type_hints.append(None)
# 
#         logger.debug(
#             dedent(
#                 """\
#                 extracted type hints:
#                 {}
#                 """
#             ).format(
#                 json.dumps(
#                     OrderedDict(
#                         {header: str(type_hint) for header, type_hint in zip(headers, type_hints)}
#                     ),
#                     indent=4,
#                 )
#             )
#         )
# 
#         return type_hints
# 
# Path: pytablereader/json/formatter.py
# class SingleJsonTableConverterBase(JsonConverter):
#     def _make_table_name(self):
#         kv_mapping = self._loader._get_basic_tablename_keyvalue_mapping()
#         kv_mapping[tnt.KEY] = self._loader.get_format_key()
# 
#         if self._loader.source_type == SourceType.FILE:
#             kv_mapping[tnt.DEFAULT] = tnt.FILENAME
#         elif self._loader.source_type == SourceType.TEXT:
#             kv_mapping[tnt.DEFAULT] = tnt.KEY
# 
#         return self._loader._expand_table_name_format(kv_mapping)


# Path: pytablereader/jsonlines/formatter.py
import jsonschema
from tabledata import TableData
from ..error import ValidationError
from ..formatter import TableFormatter
from ..json.formatter import SingleJsonTableConverterBase

"""
.. codeauthor:: Tsuyoshi Hombashi <tsuyoshi.hombashi@gmail.com>
"""




class FlatJsonTableConverter(SingleJsonTableConverterBase):
    """
    A concrete class of JSON table data formatter.
    """

    @property
    def _schema(self):
        return {"type": "object", "additionalProperties": self._VALUE_TYPE_SCHEMA}

    def _validate_source_data(self):
        for json_record in self._buffer:
            try:
                jsonschema.validate(json_record, self._schema)
            except jsonschema.ValidationError as e:

                raise ValidationError(e)