1"""
2    pyexcel.plugins.sources.pydata.recordssource
3    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5    Representation of records source
6
7    :copyright: (c) 2015-2020 by Onni Software Ltd.
8    :license: New BSD License
9"""
10from pyexcel.source import AbstractSource, MemorySourceMixin
11from pyexcel.constants import DEFAULT_SHEET_NAME
12from pyexcel.plugins.sources import params
13
14from .common import RecordsReader, _FakeIO
15
16
17class RecordsSource(AbstractSource, MemorySourceMixin):
18    """
19    A list of dictionaries as data source
20
21    The dictionaries should have identical fields.
22    """
23
24    def __init__(self, records, sheet_name=DEFAULT_SHEET_NAME, **keywords):
25        self.__records = records
26        self._content = _FakeIO()
27        self.__sheet_name = sheet_name
28        AbstractSource.__init__(self, **keywords)
29
30    def get_data(self):
31        records_reader = RecordsReader(self.__records, **self._keywords)
32        return {self.__sheet_name: records_reader.to_array()}
33
34    def get_source_info(self):
35        return params.RECORDS, None
36
37    def write_data(self, sheet):
38        self._content.setvalue(sheet.to_records())
39
40    def get_content(self):
41        return self._content
42