Exemple #1
0
 def set_sheet_name(self, name):
     self.content = StringIO()
     if PY2:
         self.writer = UnicodeWriter(self.content,
                                     encoding=self._encoding,
                                     **self._keywords)
     else:
         import csv
         self.writer = csv.writer(self.content, **self._keywords)
Exemple #2
0
    def read_sheet(self, native_sheet):
        content = self.zipfile.read(native_sheet.payload)
        if PY2:
            sheet = StringIO(content)
        else:
            sheet = StringIO(content.decode("utf-8"))

        reader = CSVinMemoryReader(NamedContent(native_sheet.name, sheet),
                                   **self._keywords)
        return reader.to_array()
def test_utf16_memory_encoding():
    content = [[u'Äkkilähdöt', u'Matkakirjoituksia', u'Matkatoimistot']]
    io = StringIO()
    writer = CSVMemoryWriter(
        io, None, lineterminator="\n", single_sheet_in_book=True,
        encoding="utf-16")
    writer.write_array(content)
    actual = io.getvalue()
    if PY2:
        actual = actual.decode('utf-16')
    eq_(actual, u'Äkkilähdöt,Matkakirjoituksia,Matkatoimistot\n')
Exemple #4
0
def test_utf16_memory_encoding():
    content = [[u'Äkkilähdöt', u'Matkakirjoituksia', u'Matkatoimistot']]
    io = StringIO()
    writer = CSVMemoryWriter(io,
                             None,
                             lineterminator="\n",
                             single_sheet_in_book=True,
                             encoding="utf-16")
    writer.write_array(content)
    actual = io.getvalue()
    if PY2:
        actual = actual.decode('utf-16')
    eq_(actual, u'Äkkilähdöt,Matkakirjoituksia,Matkatoimistot\n')
Exemple #5
0
class CSVZipSheetWriter(CSVSheetWriter):
    def __init__(self, zipfile, sheetname, file_extension, **keywords):
        self.file_extension = file_extension
        keywords['single_sheet_in_book'] = False
        CSVSheetWriter.__init__(self, zipfile, sheetname, **keywords)

    def set_sheet_name(self, name):
        self.content = StringIO()
        self.writer = csv.writer(self.content, **self._keywords)

    def close(self):
        file_name = "%s.%s" % (self._native_sheet, self.file_extension)
        self.content.seek(0)
        self._native_book.writestr(file_name, self.content.read())
        self.content.close()
Exemple #6
0
def test_writer_csvz_data_from_memory():
    if not PY2:
        io = StringIO()
        writer = get_writer(io, file_type="csvz")
        writer.write({'adb': [[2, 3]]})
    else:
        raise NotImplementedError("pass it")
Exemple #7
0
    def read_sheet(self, index):
        name = self.content_array[index].name
        content = self.zipfile.read(self.content_array[index].payload)
        encoding_guess = chardet.detect(content)
        sheet = StringIO(content.decode(encoding_guess["encoding"]))

        return CSVinMemoryReader(NamedContent(name, sheet), **self.keywords)
Exemple #8
0
def test_writer_csvz_data_from_memory():
    if not PY2:
        io = StringIO()
        writer = get_writer(io, file_type="csvz")
        writer.write({"adb": [[2, 3]]})
    else:
        raise Exception("pass it")
Exemple #9
0
class CSVZipSheetWriter(CSVFileWriter):
    """ handle the zipfile interface """
    def __init__(self, zipfile, sheetname, file_extension, **keywords):
        self.file_extension = file_extension
        keywords["single_sheet_in_book"] = False
        self.content = StringIO()
        super().__init__(zipfile, sheetname, **keywords)

    def get_writer(self):
        return csv.writer(self.content, **self._keywords)

    def close(self):
        file_name = "%s.%s" % (self._sheet_name, self.file_extension)
        self.content.seek(0)
        self._native_book.writestr(file_name, self.content.read())
        self.content.close()
Exemple #10
0
    def set_sheet_name(self, name):
        self.content = StringIO()
        if PY2:
            self.writer = UnicodeWriter(
                self.content, encoding=self._encoding, **self._keywords
            )
        else:
            import csv

            self.writer = csv.writer(self.content, **self._keywords)
Exemple #11
0
class CSVZipSheetWriter(CSVSheetWriter):
    """ handle the zipfile interface """
    def __init__(self, zipfile, sheetname, file_extension, **keywords):
        self.file_extension = file_extension
        keywords['single_sheet_in_book'] = False
        CSVSheetWriter.__init__(self, zipfile, sheetname, **keywords)

    def set_sheet_name(self, name):
        self.content = StringIO()
        if PY2:
            self.writer = UnicodeWriter(self.content,
                                        encoding=self._encoding,
                                        **self._keywords)
        else:
            import csv
            self.writer = csv.writer(self.content, **self._keywords)

    def close(self):
        file_name = "%s.%s" % (self._native_sheet, self.file_extension)
        self.content.seek(0)
        self._native_book.writestr(file_name, self.content.read())
        self.content.close()
Exemple #12
0
class CSVZipSheetWriter(CSVSheetWriter):
    """ handle the zipfile interface """

    def __init__(self, zipfile, sheetname, file_extension, **keywords):
        self.file_extension = file_extension
        keywords["single_sheet_in_book"] = False
        CSVSheetWriter.__init__(self, zipfile, sheetname, **keywords)

    def set_sheet_name(self, name):
        self.content = StringIO()
        if PY2:
            self.writer = UnicodeWriter(
                self.content, encoding=self._encoding, **self._keywords
            )
        else:
            import csv

            self.writer = csv.writer(self.content, **self._keywords)

    def close(self):
        file_name = "%s.%s" % (self._native_sheet, self.file_extension)
        self.content.seek(0)
        self._native_book.writestr(file_name, self.content.read())
        self.content.close()
Exemple #13
0
def get_io(file_type):
    """A utility function to help you generate a correct io stream

    :param file_type: a supported file type
    :returns: a appropriate io stream, None otherwise
    """
    __file_type = None
    if file_type:
        __file_type = file_type.lower()

    if __file_type in TEXT_STREAM_TYPES:
        return StringIO()
    elif __file_type in BINARY_STREAM_TYPES:
        return BytesIO()
    else:
        return None
Exemple #14
0
def get_io(file_type):
    """A utility function to help you generate a correct io stream

    :param file_type: a supported file type
    :returns: a appropriate io stream, None otherwise
    """
    __file_type = None
    if file_type:
        __file_type = file_type.lower()

    if __file_type in text_stream_types:
        return StringIO()
    elif __file_type in binary_stream_types:
        return BytesIO()
    else:
        return None
Exemple #15
0
 def set_sheet_name(self, name):
     self.content = StringIO()
     self.writer = csv.writer(self.content, **self._keywords)
Exemple #16
0
def test_load_csvz_data_from_memory():
    if not PY2:
        io = StringIO()
        get_data(io, file_type="csvz")
    else:
        raise BadZipfile("pass it")
Exemple #17
0
 def __init__(self, zipfile, sheetname, file_extension, **keywords):
     self.file_extension = file_extension
     keywords["single_sheet_in_book"] = False
     self.content = StringIO()
     super().__init__(zipfile, sheetname, **keywords)