Example #1
0
 def test_can_write_fixed_data_without_line_delimiter(self):
     fixed_data_format = data.DataFormat(data.FORMAT_FIXED)
     fixed_data_format.set_property(data.KEY_LINE_DELIMITER, 'none')
     fixed_data_format.validate()
     with io.StringIO() as target:
         with rowio.FixedRowWriter(target, fixed_data_format, [('x', 1)]) as fixed_writer:
             fixed_writer.write_rows([['1'], ['2'], ['3']])
         data_written = target.getvalue()
     self.assertEqual(data_written, '123')
Example #2
0
 def test_can_write_fixed_data_to_string(self):
     fixed_data_format = data.DataFormat(data.FORMAT_FIXED)
     fixed_data_format.set_property(data.KEY_ENCODING, 'utf-8')
     fixed_data_format.validate()
     field_names_and_lengths = [('a', 1), ('b', 3)]
     with io.StringIO() as target:
         with rowio.FixedRowWriter(target, fixed_data_format, field_names_and_lengths) as fixed_writer:
             fixed_writer.write_row(['a', 'bcd'])
             fixed_writer.write_row([_EURO_SIGN, '   '])
         data_written = dev_test.unified_newlines(target.getvalue())
     self.assertEqual('%r' % data_written, '%r' % 'abcd\n\u20ac   \n')
Example #3
0
 def test_fails_on_unicode_error_during_fixed_write(self):
     fixed_data_format = data.DataFormat(data.FORMAT_FIXED)
     fixed_data_format.set_property(data.KEY_ENCODING, 'ascii')
     fixed_data_format.validate()
     fixed_path = dev_test.path_to_test_result('test_fails_on_unicode_error_during_fixed_write.txt')
     with rowio.FixedRowWriter(fixed_path, fixed_data_format, [('x', 1)]) as fixed_writer:
         fixed_writer.write_row(['a'])
         try:
             fixed_writer.write_row([_EURO_SIGN])
             self.fail()
         except errors.DataError as anticipated_error:
             anticipated_error_message = str(anticipated_error)
             dev_test.assert_fnmatches(
                 self, anticipated_error_message, "*.txt (R2C1): cannot write data row: *; row=*")
Example #4
0
    def __init__(self, cid_or_path, target):
        assert cid_or_path is not None
        assert target is not None

        super(Writer, self).__init__(cid_or_path)

        data_format = cid_or_path.data_format
        assert self.cid.data_format.is_valid
        self._header = data_format.header
        self._delegated_writer = None
        if data_format.format == data.FORMAT_DELIMITED:
            self._delegated_writer = rowio.DelimitedRowWriter(target, data_format)
        elif data_format.format == data.FORMAT_FIXED:
            self._field_names_and_lengths = interface.field_names_and_lengths(self.cid)
            self._delegated_writer = rowio.FixedRowWriter(target, data_format, self._field_names_and_lengths)
        else:
            raise NotImplementedError('data_format=%r' % data_format.format)