class DummySource(WriteOnlySheetSource):
    """
    Write into json file
    """
    fields = [params.FILE_TYPE]
    targets = (params.BOOK, params.SHEET)
    actions = (params.WRITE_ACTION,)

    def __init__(self, file_type=None, file_stream=None, **keywords):
        if file_stream:
            self.content = file_stream
        else:
            self.content = StringIO()
        self.file_type = file_type
        self.keywords = keywords

    @classmethod
    def can_i_handle(cls, action, file_type):
        status = False
        if action == params.WRITE_ACTION and file_type == 'dummy':
            status = True
        return status

    def write_data(self, sheet):
        self.content.write(FIXTURE)
Exemplo n.º 2
0
 def __init__(self, file_type=None, file_stream=None, **keywords):
     if file_stream:
         self._content = file_stream
     else:
         self._content = StringIO()
     self.file_type = file_type
     self.keywords = keywords
class DummySource(with_metaclass(Plugin, AbstractSource, MemorySourceMixin)):
    """
    For a dynamically loaded plugin, you will need to create the following
    fields. and implement to class level functions: keywords and
    is_my_business.
    """
    plugin_name = 'source'
    fields = [FIXTURE]
    targets = (constants.BOOK, constants.SHEET)
    actions = (constants.WRITE_ACTION,)
    attributes = [FIXTURE]
    key = FIXTURE

    def __init__(self, file_type=None, file_stream=None, **keywords):
        if file_stream:
            self._content = file_stream
        else:
            self._content = StringIO()
        self.file_type = file_type
        self.keywords = keywords

    def write_data(self, sheet):
        self._content.write(FIXTURE)

    @classmethod
    def keywords(self):
        for target, action in product(self.targets, self.actions):
            yield "%s-%s" % (target, action)

    @classmethod
    def is_my_business(self, action, **keywords):
        statuses = [_has_field(field, keywords) for field in self.fields]
        results = [status for status in statuses if status is False]
        return len(results) == 0
Exemplo n.º 4
0
class DummySource(WriteOnlySheetSource):
    """
    Write into json file
    """
    fields = [params.FILE_TYPE]
    targets = (params.BOOK, params.SHEET)
    actions = (params.WRITE_ACTION,)

    def __init__(self, file_type=None, file_stream=None, **keywords):
        if file_stream:
            self.content = file_stream
        else:
            self.content = StringIO()
        self.file_type = file_type
        self.keywords = keywords

    @classmethod
    def can_i_handle(cls, action, file_type):
        status = False
        if action == params.WRITE_ACTION and file_type == 'dummy':
            status = True
        return status

    def write_data(self, sheet):
        self.content.write(FIXTURE)
Exemplo n.º 5
0
 def test_csv_output_stringio2(self):
     data = [[1, 2, 3], [4, 5, 6]]
     r = pe.Sheet(data)
     io = StringIO()
     r.save_to_memory("csv", io)
     r = pe.load_from_memory("csv", io.getvalue())
     result = ['1', '2', '3', '4', '5', '6']
     actual = pe.utils.to_array(r.enumerate())
     assert actual == result
Exemplo n.º 6
0
 def test_csv_output_stringio(self):
     data = [[1, 2, 3], [4, 5, 6]]
     io = StringIO()
     w = pe.Writer(("csv", io))
     w.write_rows(data)
     w.close()
     r = pe.Reader(("csv", io.getvalue()))
     result = ['1', '2', '3', '4', '5', '6']
     actual = pe.utils.to_array(r.enumerate())
     assert actual == result
Exemplo n.º 7
0
 def test_issue_06(self):
     import logging
     logger = logging.getLogger("test")
     logger.setLevel(logging.DEBUG)
     ch = logging.StreamHandler()
     ch.setLevel(logging.DEBUG)
     logger.addHandler(ch)
     output = StringIO()
     book = pe.Book({'hoja1': [['datos', 'de', 'prueba'], [1, 2, 3]], })
     book.save_to_memory('csv', output)
     logger.debug(output.getvalue())
Exemplo n.º 8
0
def test_issue_06():
    import logging
    logger = logging.getLogger("test")
    logger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    logger.addHandler(ch)
    output = StringIO()
    book = pe.Book({'hoja1': [['datos', 'de', 'prueba'], [1, 2, 3]], })
    book.save_to_memory('csv', output)
    logger.debug(output.getvalue())
Exemplo n.º 9
0
 def test_csv_output_stringio2(self):
     data = [
         [1, 2, 3],
         [4, 5, 6]
     ]
     r = pe.Sheet(data)
     io = StringIO()
     r.save_to_memory("csv", io)
     r = pe.load_from_memory("csv", io.getvalue())
     result=['1', '2', '3', '4', '5', '6']
     actual = pe.utils.to_array(r.enumerate())
     assert actual == result
Exemplo n.º 10
0
def test_issue_06():
    import logging

    logger = logging.getLogger("test")
    logger.setLevel(logging.DEBUG)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    logger.addHandler(ch)
    output = StringIO()
    book = p.Book({"hoja1": [["datos", "de", "prueba"], [1, 2, 3]]})
    book.save_to_memory("csv", output)
    logger.debug(output.getvalue())
Exemplo n.º 11
0
 def test_csv_output_stringio(self):
     data = [
         [1, 2, 3],
         [4, 5, 6]
     ]
     io = StringIO()
     w = pe.Writer(("csv",io))
     w.write_rows(data)
     w.close()
     r = pe.Reader(("csv", io.getvalue()))
     result=['1', '2', '3', '4', '5', '6']
     actual = pe.utils.to_array(r.enumerate())
     assert actual == result
 def __init__(self, file_type=None, file_stream=None, **keywords):
     if file_stream:
         self.content = file_stream
     else:
         self.content = StringIO()
     self.file_type = file_type
     self.keywords = keywords
Exemplo n.º 13
0
class DummySource(AbstractSource, MemorySourceMixin):
    """
    For a dynamically loaded plugin, you will need to create the following
    fields. and implement to class level functions: keywords and
    is_my_business.
    """
    def __init__(self, file_type=None, file_stream=None, **keywords):
        if file_stream:
            self._content = file_stream
        else:
            self._content = StringIO()
        self.file_type = file_type
        self.keywords = keywords

    def write_data(self, sheet):
        self._content.write(FIXTURE)
Exemplo n.º 14
0
class DummySource(AbstractSource, MemorySourceMixin):
    """
    Write into json file
    """
    fields = [FIXTURE]
    targets = (constants.BOOK, constants.SHEET)
    actions = (constants.WRITE_ACTION,)
    attributes = [FIXTURE]
    key = FIXTURE

    def __init__(self, file_type=None, file_stream=None, **keywords):
        if file_stream:
            self._content = file_stream
        else:
            self._content = StringIO()
        self.file_type = file_type
        self.keywords = keywords

    def write_data(self, sheet):
        self._content.write(FIXTURE)
Exemplo n.º 15
0
 def setUp(self):
     self.data = {
         "1": [1, 2, 3, 4, 5, 6, 7, 8],
         "2": ["1", "2", "3", "4", "5", "6", "7", "8"],
         "3": [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8],
         "4": ["1.1", "2.2", "3.3", "4.4", "5.5", "6.6", "7,7", "8.8"],
         "5": [2, 3, 4, 5, 6, 7, 8, 9],
         "6": ["2", "3", "4", "5", "6", "7", "8", "9"]
     }
     self.io = StringIO()
     w = pe.Writer(("csv", self.io))
     w.write_dict(self.data)
     #w.close()
     self.test_tuple = ("csv", self.io.getvalue())
Exemplo n.º 16
0
class DummySource(Source):
    """
    Write into json file
    """
    fields = [FIXTURE]
    targets = (params.BOOK, params.SHEET)
    actions = (params.WRITE_ACTION, )
    attributes = [FIXTURE]
    key = FIXTURE

    def __init__(self, file_type=None, file_stream=None, **keywords):
        if file_stream:
            self.content = file_stream
        else:
            self.content = StringIO()
        self.file_type = file_type
        self.keywords = keywords

    def write_data(self, sheet):
        self.content.write(FIXTURE)

    def get_internal_stream(self):
        return self.content
Exemplo n.º 17
0
def test_issue_76():
    from pyexcel._compact import StringIO
    tsv_stream = StringIO()
    tsv_stream.write('1\t2\t3\t4\n')
    tsv_stream.write('1\t2\t3\t4\n')
    tsv_stream.seek(0)
    sheet = pe.get_sheet(file_stream=tsv_stream,
                         file_type='csv',
                         delimiter='\t')
    data = [[1, 2, 3, 4], [1, 2, 3, 4]]
    eq_(sheet.array, data)
Exemplo n.º 18
0
def test_issue_76():
    from pyexcel._compact import StringIO

    tsv_stream = StringIO()
    tsv_stream.write("1\t2\t3\t4\n")
    tsv_stream.write("1\t2\t3\t4\n")
    tsv_stream.seek(0)
    sheet = p.get_sheet(
        file_stream=tsv_stream, file_type="csv", delimiter="\t"
    )
    data = [[1, 2, 3, 4], [1, 2, 3, 4]]
    eq_(sheet.array, data)
Exemplo n.º 19
0
class TestColumnFormatter:
    def setUp(self):
        self.data = {
            "1": [1, 2, 3, 4, 5, 6, 7, 8],
            "2": ["1", "2", "3", "4", "5", "6", "7", "8"],
            "3": [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8],
            "4": ["1.1", "2.2", "3.3", "4.4", "5.5", "6.6", "7,7", "8.8"],
            "5": [2, 3, 4, 5, 6, 7, 8, 9],
            "6": ["2", "3", "4", "5", "6", "7", "8", "9"]
        }
        self.io = StringIO()
        w = pe.Writer(("csv", self.io))
        w.write_dict(self.data)
        #w.close()
        self.test_tuple = ("csv", self.io.getvalue())

    def test_general_usage(self):
        r = pe.Reader(self.test_tuple)
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            str))
        c1 = r.column_at(0)[1:]
        c2 = self.data["2"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    def test_one_formatter_for_two_columns(self):
        r = pe.Reader(self.test_tuple)
        r.add_formatter(pe.formatters.ColumnFormatter(
            [0,5],
            str))
        c1 = r.column_at(0)[1:]
        c2 = self.data["2"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]
        c1 = r.column_at(5)[1:]
        c2 = self.data["6"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    @raises(NotImplementedError)
    def test_invalid_input(self):
        pe.formatters.ColumnFormatter("world", str)

    @raises(IndexError)
    def test_invalid_input2(self):
        """Empty list"""
        pe.formatters.ColumnFormatter([], str)

    @raises(IndexError)
    def test_float_in_list(self):
        pe.formatters.ColumnFormatter([1, 1.1], str)

    def test_two_formatters(self):
        r = pe.Reader(self.test_tuple)
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            str))
        c1 = r.column_at(0)[1:]
        c2 = self.data["2"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            int))
        c1 = r.column_at(0)[1:]
        c2 = self.data["1"]
        for i in range(0, len(c1)):
            assert type(c1[i]) == int
            assert c1[i] == c2[i]

    def test_custom_func(self):
        r = pe.Reader(self.test_tuple)
        f = lambda x: int(x) + 1
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            int,
            f))
        c1 = r.column_at(0)[1:]
        c2 = self.data["5"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]

    def test_custom_func_with_a_general_converter(self):
        r = pe.Reader(self.test_tuple)
        f = lambda x: int(x) + 1
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            int,
            f))
        c1 = r.column_at(0)[1:]
        c2 = self.data["5"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]
        r.add_formatter(pe.formatters.ColumnFormatter(
            0,
            str))
        c1 = r.column_at(0)[1:]
        c2 = self.data["6"]
        for i in range(0, len(c1)):
            assert c1[i] == c2[i]