New style writer (#76)
* 🔨 code up the new writer interface * 🐴 prototype * 🚜 new style csv writer for file, content and stream * This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst * 💚 add test dependency * This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst * 💚 update moban update yaml * 💄 update coding style with latest black, isort * ✨ tsv writers in new style * 🐴 new style csvz writer * 🐴 new style tsvz writer * 🔥 remove old style csvz and tsvz writers * 🔥 remove old style csv and tsv writer * 📚 update changelog * 🚜 code refactoring and update 📚 * This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst * 🚜 rename sheet writer files * 🔥 remove .moban.hashes * 🔥 remove useless set_type function call * 💄 update coding style Co-authored-by: chfw <chfw@users.noreply.github.com>
This commit is contained in:
parent
7a3957c36f
commit
4bc78d671c
|
@ -5,23 +5,25 @@ jobs:
|
|||
runs-on: ubuntu-latest
|
||||
name: synchronize templates via moban
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
ref: ${{ github.head_ref }}
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: '3.7'
|
||||
- name: check changes
|
||||
run: |
|
||||
pip install moban gitfs2 pypifs
|
||||
moban
|
||||
git status
|
||||
git diff --exit-code
|
||||
- name: Auto-commit
|
||||
if: failure()
|
||||
uses: docker://cdssnc/auto-commit-github-action
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
args: This is an auto-commit, updating project meta data, such as changelog.rst, contributors.rst
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
ref: ${{ github.head_ref }}
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v1
|
||||
with:
|
||||
python-version: '3.7'
|
||||
- name: check changes
|
||||
run: |
|
||||
pip install moban gitfs2 pypifs moban-jinja2-github moban-ansible
|
||||
moban
|
||||
git status
|
||||
git diff --exit-code
|
||||
- name: Auto-commit
|
||||
if: failure()
|
||||
uses: docker://cdssnc/auto-commit-github-action
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
args: >-
|
||||
This is an auto-commit, updating project meta data,
|
||||
such as changelog.rst, contributors.rst
|
||||
|
|
|
@ -13,6 +13,11 @@ Change log
|
|||
|
||||
#. python 3.6 lower versions are no longer supported
|
||||
|
||||
**updated**
|
||||
|
||||
#. pyexcel-io plugin interface has been rewritten. PyInstaller user will be
|
||||
impacted.
|
||||
|
||||
0.5.20 - 17.7.2019
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
|
|
@ -8,6 +8,9 @@ releases:
|
|||
- action: removed
|
||||
details:
|
||||
- 'python 3.6 lower versions are no longer supported'
|
||||
- action: updated
|
||||
details:
|
||||
- 'pyexcel-io plugin interface has been rewritten. PyInstaller user will be impacted.'
|
||||
version: 0.6.0
|
||||
date: tbd
|
||||
- changes:
|
||||
|
|
|
@ -1,6 +1,30 @@
|
|||
Packaging with PyInstaller
|
||||
================================================================================
|
||||
|
||||
With pyexcel-io v0.6.0, the way to package it has been changed because
|
||||
plugin interface update.
|
||||
|
||||
Built-in plugins for pyexcel-io
|
||||
---------------------------------
|
||||
|
||||
In order to package every built-in plugins of pyexcel-io, you need to specify::
|
||||
|
||||
--hidden-import pyexcel_io.readers.csvr
|
||||
--hidden-import pyexcel_io.readers.csvz
|
||||
--hidden-import pyexcel_io.readers.tsv
|
||||
--hidden-import pyexcel_io.readers.tsvz
|
||||
--hidden-import pyexcel_io.writers.csv_file_writer
|
||||
--hidden-import pyexcel_io.writers.csv_memory_writer
|
||||
--hidden-import pyexcel_io.writers.tsv_file_writer
|
||||
--hidden-import pyexcel_io.writers.tsv_memory_writer
|
||||
--hidden-import pyexcel_io.writers.csvz_writer
|
||||
--hidden-import pyexcel_io.writers.tsvz_writer
|
||||
--hidden-import pyexcel_io.database.importers.django
|
||||
--hidden-import pyexcel_io.database.importers.sqlalchemy
|
||||
--hidden-import pyexcel_io.database.exporters.django
|
||||
--hidden-import pyexcel_io.database.exporters.sqlalchemy
|
||||
|
||||
|
||||
With pyexcel-io v0.4.0, the way to package it has been changed because it
|
||||
uses lml for all plugins.
|
||||
|
||||
|
@ -14,9 +38,9 @@ In order to package every built-in plugins of pyexcel-io, you need to specify::
|
|||
--hidden-import pyexcel_io.readers.tsv
|
||||
--hidden-import pyexcel_io.readers.tsvz
|
||||
--hidden-import pyexcel_io.writers.csvw
|
||||
--hidden-import pyexcel_io.readers.csvz
|
||||
--hidden-import pyexcel_io.readers.tsv
|
||||
--hidden-import pyexcel_io.readers.tsvz
|
||||
--hidden-import pyexcel_io.writers.csvz
|
||||
--hidden-import pyexcel_io.writers.tsv
|
||||
--hidden-import pyexcel_io.writers.tsvz
|
||||
--hidden-import pyexcel_io.database.importers.django
|
||||
--hidden-import pyexcel_io.database.importers.sqlalchemy
|
||||
--hidden-import pyexcel_io.database.exporters.django
|
||||
|
|
|
@ -15,6 +15,7 @@ test_dependencies:
|
|||
- pyexcel
|
||||
- pyexcel-xls
|
||||
- SQLAlchemy
|
||||
- pyexcel-xlsxw
|
||||
extra_dependencies:
|
||||
- xls:
|
||||
- pyexcel-xls>=0.5.0
|
||||
|
|
|
@ -14,7 +14,7 @@ from types import GeneratorType
|
|||
from pyexcel_io import constants
|
||||
from pyexcel_io.reader import Reader
|
||||
from pyexcel_io.writer import Writer
|
||||
from pyexcel_io.plugins import OLD_READERS
|
||||
from pyexcel_io.plugins import OLD_READERS, OLD_WRITERS
|
||||
from pyexcel_io._compact import isstream
|
||||
from pyexcel_io.exceptions import (
|
||||
NoSupportingPluginFound,
|
||||
|
@ -244,7 +244,11 @@ def get_writer(
|
|||
|
||||
file_type_given = False
|
||||
|
||||
writer = Writer(file_type, library)
|
||||
try:
|
||||
writer = OLD_WRITERS.get_a_plugin(file_type, library)
|
||||
except (NoSupportingPluginFound, SupportingPluginAvailableButNotInstalled):
|
||||
writer = Writer(file_type, library)
|
||||
|
||||
if file_name:
|
||||
if file_type_given:
|
||||
writer.open_content(file_name, **keywords)
|
||||
|
|
|
@ -20,6 +20,7 @@ plugin compactibility table."
|
|||
READER_PLUGIN = "pyexcel-io reader"
|
||||
NEW_READER_PLUGIN = "pyexcel-io new reader"
|
||||
WRITER_PLUGIN = "pyexcel-io writer"
|
||||
NEW_WRITER_PLUGIN = "pyexcel-io new writer"
|
||||
|
||||
|
||||
class IOPluginInfo(PluginInfo):
|
||||
|
@ -83,6 +84,26 @@ class NewIOPluginInfoChain(PluginInfoChain):
|
|||
)
|
||||
return self.add_a_plugin_instance(a_plugin_info)
|
||||
|
||||
def add_a_writer(
|
||||
self,
|
||||
relative_plugin_class_path=None,
|
||||
locations=(),
|
||||
file_types=(),
|
||||
stream_type=None,
|
||||
):
|
||||
""" add pyexcle-io writer plugin info """
|
||||
a_plugin_info = IOPluginInfo(
|
||||
NEW_WRITER_PLUGIN,
|
||||
self._get_abs_path(relative_plugin_class_path),
|
||||
file_types=[
|
||||
f"{location}-{file_type}"
|
||||
for file_type in file_types
|
||||
for location in locations
|
||||
],
|
||||
stream_type=stream_type,
|
||||
)
|
||||
return self.add_a_plugin_instance(a_plugin_info)
|
||||
|
||||
|
||||
class IOManager(PluginManager):
|
||||
"""Manage pyexcel-io plugins"""
|
||||
|
@ -143,12 +164,12 @@ class IOManager(PluginManager):
|
|||
class NewIOManager(IOManager):
|
||||
def load_me_later(self, plugin_info):
|
||||
PluginManager.load_me_later(self, plugin_info)
|
||||
_do_additional_registration(plugin_info)
|
||||
_do_additional_registration_for_new_plugins(plugin_info)
|
||||
|
||||
def register_a_plugin(self, cls, plugin_info):
|
||||
""" for dynamically loaded plugin """
|
||||
PluginManager.register_a_plugin(self, cls, plugin_info)
|
||||
_do_additional_registration(plugin_info)
|
||||
_do_additional_registration_for_new_plugins(plugin_info)
|
||||
|
||||
def get_a_plugin(
|
||||
self, file_type=None, location=None, library=None, **keywords
|
||||
|
@ -156,7 +177,6 @@ class NewIOManager(IOManager):
|
|||
__file_type = file_type.lower()
|
||||
plugin = self.load_me_now(f"{location}-{__file_type}", library=library)
|
||||
handler = plugin()
|
||||
handler.set_type(__file_type)
|
||||
return handler
|
||||
|
||||
def raise_exception(self, file_type):
|
||||
|
@ -194,6 +214,16 @@ def _do_additional_registration(plugin_info):
|
|||
manager.register_a_file_type(file_type, plugin_info.stream_type, None)
|
||||
|
||||
|
||||
def _do_additional_registration_for_new_plugins(plugin_info):
|
||||
for file_type in plugin_info.tags():
|
||||
manager.register_stream_type(
|
||||
file_type.split("-")[1], plugin_info.stream_type
|
||||
)
|
||||
manager.register_a_file_type(
|
||||
file_type.split("-")[1], plugin_info.stream_type, None
|
||||
)
|
||||
|
||||
|
||||
class FakeReaders:
|
||||
def get_all_formats(self):
|
||||
return OLD_READERS.get_all_formats().union(
|
||||
|
@ -201,10 +231,19 @@ class FakeReaders:
|
|||
)
|
||||
|
||||
|
||||
class FakeWriters:
|
||||
def get_all_formats(self):
|
||||
return OLD_WRITERS.get_all_formats().union(
|
||||
NEW_WRITERS.get_all_formats()
|
||||
)
|
||||
|
||||
|
||||
OLD_READERS = IOManager(READER_PLUGIN, ioutils.AVAILABLE_READERS)
|
||||
WRITERS = IOManager(WRITER_PLUGIN, ioutils.AVAILABLE_WRITERS)
|
||||
OLD_WRITERS = IOManager(WRITER_PLUGIN, ioutils.AVAILABLE_WRITERS)
|
||||
NEW_WRITERS = NewIOManager(NEW_WRITER_PLUGIN, ioutils.AVAILABLE_WRITERS)
|
||||
NEW_READERS = NewIOManager(NEW_READER_PLUGIN, ioutils.AVAILABLE_READERS)
|
||||
READERS = FakeReaders()
|
||||
WRITERS = FakeWriters()
|
||||
|
||||
|
||||
def load_plugins(plugin_name_patterns, path, black_list, white_list):
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
:copyright: (c) 2014-2020 by Onni Software Ltd.
|
||||
:license: New BSD License, see LICENSE for more details
|
||||
"""
|
||||
from pyexcel_io.plugins import IOPluginInfoChain, NewIOPluginInfoChain
|
||||
from pyexcel_io.plugins import NewIOPluginInfoChain
|
||||
|
||||
NewIOPluginInfoChain(__name__).add_a_reader(
|
||||
relative_plugin_class_path="csv_file_reader.FileReader",
|
||||
|
|
|
@ -1,24 +1,43 @@
|
|||
from pyexcel_io.plugins import WRITERS
|
||||
from pyexcel_io.plugins import NEW_WRITERS
|
||||
from pyexcel_io._compact import isstream
|
||||
|
||||
from .constants import MESSAGE_ERROR_03
|
||||
|
||||
|
||||
class Writer(object):
|
||||
def __init__(self, file_type, library):
|
||||
self.writer = WRITERS.get_a_plugin(file_type, library)
|
||||
def __init__(self, file_type, library=None):
|
||||
self.file_type = file_type
|
||||
self.library = library
|
||||
self.keyboards = None
|
||||
|
||||
def open(self, file_name, **keywords):
|
||||
self.writer = NEW_WRITERS.get_a_plugin(
|
||||
self.file_type, library=self.library, location="file"
|
||||
)
|
||||
self.writer.open(file_name, **keywords)
|
||||
|
||||
def open_stream(self, file_stream, **keywords):
|
||||
self.writer.open_stream(file_stream, **keywords)
|
||||
|
||||
def open_content(self, file_stream, **keywords):
|
||||
self.writer.open_content(file_stream, **keywords)
|
||||
if not isstream(file_stream):
|
||||
raise IOError(MESSAGE_ERROR_03)
|
||||
self.writer = NEW_WRITERS.get_a_plugin(
|
||||
self.file_type, library=self.library, location="content"
|
||||
)
|
||||
self.writer.open(file_stream, **keywords)
|
||||
|
||||
def open_stream(self, file_stream, **keywords):
|
||||
self.writer = NEW_WRITERS.get_a_plugin(
|
||||
self.file_type, library=self.library, location="memory"
|
||||
)
|
||||
self.writer.open(file_stream, **keywords)
|
||||
|
||||
def write(self, incoming_dict):
|
||||
self.writer.write(incoming_dict)
|
||||
|
||||
def create_sheet(self, sheet_name):
|
||||
return self.writer.create_sheet(sheet_name)
|
||||
for sheet_name in incoming_dict:
|
||||
sheet_writer = self.writer.create_sheet(sheet_name)
|
||||
if sheet_writer:
|
||||
sheet_writer.write_array(incoming_dict[sheet_name])
|
||||
sheet_writer.close()
|
||||
else:
|
||||
raise Exception("Cannot create a sheet writer!")
|
||||
|
||||
def close(self):
|
||||
self.writer.close()
|
||||
|
|
|
@ -7,22 +7,36 @@
|
|||
:copyright: (c) 2014-2020 by Onni Software Ltd.
|
||||
:license: New BSD License, see LICENSE for more details
|
||||
"""
|
||||
from pyexcel_io.plugins import IOPluginInfoChain
|
||||
from pyexcel_io.plugins import NewIOPluginInfoChain
|
||||
|
||||
IOPluginInfoChain(__name__).add_a_writer(
|
||||
relative_plugin_class_path="csvw.CSVBookWriter",
|
||||
NewIOPluginInfoChain(__name__).add_a_writer(
|
||||
relative_plugin_class_path="csv_in_file.CsvFileWriter",
|
||||
locations=["file", "content"],
|
||||
file_types=["csv"],
|
||||
stream_type="text",
|
||||
).add_a_writer(
|
||||
relative_plugin_class_path="tsv.TSVBookWriter",
|
||||
relative_plugin_class_path="csv_in_memory.CsvMemoryWriter",
|
||||
locations=["memory"],
|
||||
file_types=["csv"],
|
||||
stream_type="text",
|
||||
).add_a_writer(
|
||||
relative_plugin_class_path="tsv_in_file.TsvFileWriter",
|
||||
locations=["file", "content"],
|
||||
file_types=["tsv"],
|
||||
stream_type="text",
|
||||
).add_a_writer(
|
||||
relative_plugin_class_path="csvz.CSVZipBookWriter",
|
||||
relative_plugin_class_path="tsv_in_memory.TsvMemoryWriter",
|
||||
locations=["memory"],
|
||||
file_types=["tsv"],
|
||||
stream_type="text",
|
||||
).add_a_writer(
|
||||
relative_plugin_class_path="csvz_writer.CsvZipWriter",
|
||||
locations=["memory", "file", "content"],
|
||||
file_types=["csvz"],
|
||||
stream_type="binary",
|
||||
).add_a_writer(
|
||||
relative_plugin_class_path="tsvz.TSVZipBookWriter",
|
||||
relative_plugin_class_path="tsvz_writer.TsvZipWriter",
|
||||
locations=["memory", "file", "content"],
|
||||
file_types=["tsvz"],
|
||||
stream_type="binary",
|
||||
)
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
from pyexcel_io.writers.csv_sheet import CSVFileWriter
|
||||
|
||||
|
||||
class CsvFileWriter:
|
||||
def __init__(self):
|
||||
self.__index = 0
|
||||
self.writer = None
|
||||
|
||||
def open(self, file_alike_object, **keywords):
|
||||
self._file_alike_object = file_alike_object
|
||||
self._keywords = keywords
|
||||
|
||||
def create_sheet(self, name):
|
||||
self.writer = CSVFileWriter(
|
||||
self._file_alike_object,
|
||||
name,
|
||||
sheet_index=self.__index,
|
||||
**self._keywords
|
||||
)
|
||||
self.__index = self.__index + 1
|
||||
return self.writer
|
||||
|
||||
def close(self):
|
||||
if self.writer:
|
||||
self.writer.close()
|
|
@ -0,0 +1,24 @@
|
|||
from pyexcel_io.writers.csv_sheet import CSVMemoryWriter
|
||||
|
||||
|
||||
class CsvMemoryWriter:
|
||||
def __init__(self):
|
||||
self.__index = 0
|
||||
|
||||
def open(self, file_alike_object, **keywords):
|
||||
self._file_alike_object = file_alike_object
|
||||
self._keywords = keywords
|
||||
|
||||
def create_sheet(self, name):
|
||||
writer_class = CSVMemoryWriter
|
||||
writer = writer_class(
|
||||
self._file_alike_object,
|
||||
name,
|
||||
sheet_index=self.__index,
|
||||
**self._keywords
|
||||
)
|
||||
self.__index = self.__index + 1
|
||||
return writer
|
||||
|
||||
def close(self):
|
||||
pass
|
|
@ -9,9 +9,7 @@
|
|||
"""
|
||||
import csv
|
||||
|
||||
import pyexcel_io._compact as compact
|
||||
import pyexcel_io.constants as constants
|
||||
from pyexcel_io.book import BookWriter
|
||||
from pyexcel_io.sheet import SheetWriter
|
||||
|
||||
|
||||
|
@ -119,27 +117,3 @@ class CSVMemoryWriter(CSVSheetWriter):
|
|||
pass
|
||||
else:
|
||||
self.writer.writerow([constants.SEPARATOR_FORMATTER % ""])
|
||||
|
||||
|
||||
class CSVBookWriter(BookWriter):
|
||||
""" write csv with unicode support """
|
||||
|
||||
def __init__(self):
|
||||
BookWriter.__init__(self)
|
||||
self._file_type = constants.FILE_FORMAT_CSV
|
||||
self.__index = 0
|
||||
|
||||
def create_sheet(self, name):
|
||||
writer_class = None
|
||||
if compact.is_string(type(self._file_alike_object)):
|
||||
writer_class = CSVFileWriter
|
||||
else:
|
||||
writer_class = CSVMemoryWriter
|
||||
writer = writer_class(
|
||||
self._file_alike_object,
|
||||
name,
|
||||
sheet_index=self.__index,
|
||||
**self._keywords
|
||||
)
|
||||
self.__index = self.__index + 1
|
||||
return writer
|
|
@ -1,68 +0,0 @@
|
|||
"""
|
||||
pyexcel_io.fileformat.csvz
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The lower level csvz file format handler.
|
||||
|
||||
:copyright: (c) 2014-2020 by Onni Software Ltd.
|
||||
:license: New BSD License, see LICENSE for more details
|
||||
"""
|
||||
import zipfile
|
||||
|
||||
from pyexcel_io.book import BookWriter
|
||||
from pyexcel_io._compact import StringIO
|
||||
from pyexcel_io.constants import FILE_FORMAT_CSVZ, DEFAULT_SHEET_NAME
|
||||
|
||||
from .csvw import CSVSheetWriter
|
||||
|
||||
|
||||
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()
|
||||
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()
|
||||
|
||||
|
||||
class CSVZipBookWriter(BookWriter):
|
||||
"""
|
||||
csvz writer
|
||||
|
||||
It is better to store csv files as a csvz as it saves your disk space.
|
||||
Pyexcel-io had the facility to unzip it for you or you could use
|
||||
any other unzip software.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
BookWriter.__init__(self)
|
||||
self._file_type = FILE_FORMAT_CSVZ
|
||||
self.zipfile = None
|
||||
|
||||
def open(self, file_name, **keywords):
|
||||
BookWriter.open(self, file_name, **keywords)
|
||||
self.zipfile = zipfile.ZipFile(file_name, "w", zipfile.ZIP_DEFLATED)
|
||||
|
||||
def create_sheet(self, name):
|
||||
given_name = name
|
||||
if given_name is None:
|
||||
given_name = DEFAULT_SHEET_NAME
|
||||
writer = CSVZipSheetWriter(
|
||||
self.zipfile, given_name, self._file_type[:3], **self._keywords
|
||||
)
|
||||
return writer
|
||||
|
||||
def close(self):
|
||||
self.zipfile.close()
|
|
@ -0,0 +1,32 @@
|
|||
"""
|
||||
pyexcel_io.fileformat.csvz
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The lower level csvz file format handler.
|
||||
|
||||
:copyright: (c) 2014-2020 by Onni Software Ltd.
|
||||
:license: New BSD License, see LICENSE for more details
|
||||
"""
|
||||
from pyexcel_io._compact import StringIO
|
||||
from pyexcel_io.writers.csv_sheet import CSVSheetWriter
|
||||
|
||||
|
||||
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()
|
||||
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()
|
|
@ -0,0 +1,36 @@
|
|||
import zipfile
|
||||
|
||||
from pyexcel_io.constants import FILE_FORMAT_CSVZ, DEFAULT_SHEET_NAME
|
||||
from pyexcel_io.writers.csvz_sheet import CSVZipSheetWriter
|
||||
|
||||
|
||||
class CsvZipWriter(object):
|
||||
"""
|
||||
csvz writer
|
||||
|
||||
It is better to store csv files as a csvz as it saves your disk space.
|
||||
Pyexcel-io had the facility to unzip it for you or you could use
|
||||
any other unzip software.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.zipfile = None
|
||||
self._keywords = None
|
||||
self._file_type = FILE_FORMAT_CSVZ
|
||||
|
||||
def open(self, file_name, **keywords):
|
||||
self.zipfile = zipfile.ZipFile(file_name, "w", zipfile.ZIP_DEFLATED)
|
||||
self._keywords = keywords
|
||||
|
||||
def create_sheet(self, name):
|
||||
given_name = name
|
||||
if given_name is None:
|
||||
given_name = DEFAULT_SHEET_NAME
|
||||
writer = CSVZipSheetWriter(
|
||||
self.zipfile, given_name, self._file_type[:3], **self._keywords
|
||||
)
|
||||
return writer
|
||||
|
||||
def close(self):
|
||||
if self.zipfile:
|
||||
self.zipfile.close()
|
|
@ -1,24 +0,0 @@
|
|||
"""
|
||||
pyexcel_io.fileformat.tsv
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The lower level tsv file format handler.
|
||||
|
||||
:copyright: (c) 2014-2020 by Onni Software Ltd.
|
||||
:license: New BSD License, see LICENSE for more details
|
||||
"""
|
||||
import pyexcel_io.constants as constants
|
||||
|
||||
from .csvw import CSVBookWriter
|
||||
|
||||
|
||||
class TSVBookWriter(CSVBookWriter):
|
||||
""" write tsv """
|
||||
|
||||
def __init__(self):
|
||||
CSVBookWriter.__init__(self)
|
||||
self._file_type = constants.FILE_FORMAT_TSV
|
||||
|
||||
def open(self, file_name, **keywords):
|
||||
keywords["dialect"] = constants.KEYWORD_TSV_DIALECT
|
||||
CSVBookWriter.open(self, file_name, **keywords)
|
|
@ -0,0 +1,9 @@
|
|||
from pyexcel_io.constants import KEYWORD_TSV_DIALECT
|
||||
from pyexcel_io.writers.csv_in_file import CsvFileWriter
|
||||
|
||||
|
||||
class TsvFileWriter(CsvFileWriter):
|
||||
def open(self, file_alike_object, **keywords):
|
||||
super().open(
|
||||
file_alike_object, dialect=KEYWORD_TSV_DIALECT, **keywords
|
||||
)
|
|
@ -0,0 +1,9 @@
|
|||
from pyexcel_io.constants import KEYWORD_TSV_DIALECT
|
||||
from pyexcel_io.writers.csv_in_memory import CsvMemoryWriter
|
||||
|
||||
|
||||
class TsvMemoryWriter(CsvMemoryWriter):
|
||||
def open(self, file_alike_object, **keywords):
|
||||
super().open(
|
||||
file_alike_object, dialect=KEYWORD_TSV_DIALECT, **keywords
|
||||
)
|
|
@ -1,27 +0,0 @@
|
|||
"""
|
||||
pyexcel_io.fileformat.tsvz
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The lower level tsvz file format handler.
|
||||
|
||||
:copyright: (c) 2014-2020 by Onni Software Ltd.
|
||||
:license: New BSD License, see LICENSE for more details
|
||||
"""
|
||||
from pyexcel_io.constants import FILE_FORMAT_TSVZ, KEYWORD_TSV_DIALECT
|
||||
|
||||
from .csvz import CSVZipBookWriter
|
||||
|
||||
|
||||
class TSVZipBookWriter(CSVZipBookWriter):
|
||||
"""write zipped tsv file
|
||||
|
||||
It is similiar to CSVZipBookWriter, but support tab separated values
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
CSVZipBookWriter.__init__(self)
|
||||
self._file_type = FILE_FORMAT_TSVZ
|
||||
|
||||
def open(self, file_name, **keywords):
|
||||
keywords["dialect"] = KEYWORD_TSV_DIALECT
|
||||
CSVZipBookWriter.open(self, file_name, **keywords)
|
|
@ -0,0 +1,11 @@
|
|||
from pyexcel_io.constants import FILE_FORMAT_TSVZ, KEYWORD_TSV_DIALECT
|
||||
from pyexcel_io.writers.csvz_writer import CsvZipWriter
|
||||
|
||||
|
||||
class TsvZipWriter(CsvZipWriter):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self._file_type = FILE_FORMAT_TSVZ
|
||||
|
||||
def open(self, file_name, **keywords):
|
||||
super().open(file_name, dialect=KEYWORD_TSV_DIALECT, **keywords)
|
|
@ -12,3 +12,4 @@ moban_jinja2_github
|
|||
pyexcel
|
||||
pyexcel-xls
|
||||
SQLAlchemy
|
||||
pyexcel-xlsxw
|
||||
|
|
|
@ -1,6 +1,3 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
from textwrap import dedent
|
||||
from unittest import TestCase
|
||||
|
@ -14,7 +11,7 @@ from pyexcel_io.readers.csvr import (
|
|||
CSVSheetReader,
|
||||
CSVinMemoryReader,
|
||||
)
|
||||
from pyexcel_io.writers.csvw import CSVFileWriter, CSVMemoryWriter
|
||||
from pyexcel_io.writers.csv_sheet import CSVFileWriter, CSVMemoryWriter
|
||||
|
||||
from nose.tools import eq_, raises
|
||||
|
||||
|
|
|
@ -4,9 +4,8 @@ from unittest import TestCase
|
|||
|
||||
import pyexcel_io.manager as manager
|
||||
from pyexcel_io.reader import Reader
|
||||
from pyexcel_io.writer import Writer
|
||||
from pyexcel_io._compact import OrderedDict
|
||||
from pyexcel_io.writers.tsv import TSVBookWriter
|
||||
from pyexcel_io.writers.csvw import CSVBookWriter
|
||||
|
||||
from nose.tools import raises
|
||||
|
||||
|
@ -162,7 +161,7 @@ class TestTSVBookReaders(TestReadMultipleSheets):
|
|||
|
||||
class TestWriteMultipleSheets(TestCase):
|
||||
file_type = "csv"
|
||||
writer_class = CSVBookWriter
|
||||
|
||||
result1 = dedent(
|
||||
"""
|
||||
1,2,3
|
||||
|
@ -204,6 +203,9 @@ class TestWriteMultipleSheets(TestCase):
|
|||
"""
|
||||
)
|
||||
|
||||
def writer_class(self):
|
||||
return Writer(self.file_type)
|
||||
|
||||
def reader_class(self):
|
||||
return Reader(self.file_type)
|
||||
|
||||
|
@ -246,7 +248,7 @@ class TestWriteMultipleSheets(TestCase):
|
|||
def test_multiple_sheet_into_memory(self):
|
||||
io = manager.get_io(self.file_type)
|
||||
w = self.writer_class()
|
||||
w.open(io, lineterminator="\n")
|
||||
w.open_stream(io, lineterminator="\n")
|
||||
w.write(self.sheets)
|
||||
w.close()
|
||||
content = io.getvalue()
|
||||
|
@ -256,7 +258,7 @@ class TestWriteMultipleSheets(TestCase):
|
|||
"""Write csv book into a single stream"""
|
||||
io = manager.get_io(self.file_type)
|
||||
w = self.writer_class()
|
||||
w.open(io, lineterminator="\n")
|
||||
w.open_stream(io, lineterminator="\n")
|
||||
w.write(self.sheets)
|
||||
w.close()
|
||||
reader = self.reader_class()
|
||||
|
@ -276,7 +278,7 @@ class TestWriteMultipleSheets(TestCase):
|
|||
|
||||
class TestTSVWriteMultipleSheets(TestWriteMultipleSheets):
|
||||
file_type = "tsv"
|
||||
writer_class = TSVBookWriter
|
||||
|
||||
result1 = dedent(
|
||||
"""
|
||||
1\t2\t3
|
||||
|
@ -321,7 +323,7 @@ class TestTSVWriteMultipleSheets(TestWriteMultipleSheets):
|
|||
|
||||
class TestWriter(TestCase):
|
||||
file_type = "csv"
|
||||
writer_class = CSVBookWriter
|
||||
|
||||
result = dedent(
|
||||
"""
|
||||
1,2,3
|
||||
|
@ -330,6 +332,9 @@ class TestWriter(TestCase):
|
|||
"""
|
||||
).strip("\n")
|
||||
|
||||
def writer_class(self):
|
||||
return Writer(self.file_type)
|
||||
|
||||
def setUp(self):
|
||||
self.test_file = "csv_book." + self.file_type
|
||||
self.data = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]]
|
||||
|
@ -349,7 +354,7 @@ class TestWriter(TestCase):
|
|||
|
||||
class TestTSVWriters(TestWriter):
|
||||
file_type = "tsv"
|
||||
writer_class = TSVBookWriter
|
||||
|
||||
result = dedent(
|
||||
"""
|
||||
1\t2\t3
|
||||
|
@ -361,7 +366,7 @@ class TestTSVWriters(TestWriter):
|
|||
|
||||
class TestMemoryWriter(TestCase):
|
||||
file_type = "csv"
|
||||
writer_class = CSVBookWriter
|
||||
|
||||
result = dedent(
|
||||
"""
|
||||
1,2,3
|
||||
|
@ -370,6 +375,9 @@ class TestMemoryWriter(TestCase):
|
|||
"""
|
||||
).strip("\n")
|
||||
|
||||
def writer_class(self):
|
||||
return Writer(self.file_type)
|
||||
|
||||
def setUp(self):
|
||||
self.test_file = "csv_book." + self.file_type
|
||||
self.data = [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "9"]]
|
||||
|
@ -377,7 +385,7 @@ class TestMemoryWriter(TestCase):
|
|||
def test_book_writer_to_memroy(self):
|
||||
io = manager.get_io(self.file_type)
|
||||
w = self.writer_class()
|
||||
w.open(io, single_sheet_in_book=True)
|
||||
w.open_stream(io, single_sheet_in_book=True)
|
||||
w.write({self.file_type: self.data})
|
||||
w.close()
|
||||
content = io.getvalue().replace("\r", "")
|
||||
|
@ -386,7 +394,7 @@ class TestMemoryWriter(TestCase):
|
|||
|
||||
class TestTSVMemoryWriter(TestMemoryWriter):
|
||||
file_type = "tsv"
|
||||
writer_class = TSVBookWriter
|
||||
|
||||
result = dedent(
|
||||
"""
|
||||
1\t2\t3
|
||||
|
|
|
@ -7,9 +7,8 @@ from unittest import TestCase
|
|||
import pyexcel_io.manager as manager
|
||||
from pyexcel_io import save_data
|
||||
from pyexcel_io.reader import Reader
|
||||
from pyexcel_io.writer import Writer
|
||||
from pyexcel_io._compact import OrderedDict
|
||||
from pyexcel_io.writers.csvz import CSVZipBookWriter
|
||||
from pyexcel_io.writers.tsvz import TSVZipBookWriter
|
||||
|
||||
from nose.tools import raises
|
||||
|
||||
|
@ -18,9 +17,11 @@ PY2 = sys.version_info[0] == 2
|
|||
|
||||
class TestCSVZ(TestCase):
|
||||
file_type = "csvz"
|
||||
writer_class = CSVZipBookWriter
|
||||
result = u"中,文,1,2,3"
|
||||
|
||||
def writer_class(self):
|
||||
return Writer(self.file_type)
|
||||
|
||||
def reader_class(self):
|
||||
return Reader(self.file_type)
|
||||
|
||||
|
@ -69,14 +70,13 @@ class TestCSVZ(TestCase):
|
|||
|
||||
class TestTSVZ(TestCSVZ):
|
||||
file_type = "tsvz"
|
||||
writer_class = TSVZipBookWriter
|
||||
result = u"中\t文\t1\t2\t3"
|
||||
|
||||
|
||||
def test_reading_from_memory():
|
||||
data = [[1, 2, 3]]
|
||||
io = manager.get_io("csvz")
|
||||
zipbook = CSVZipBookWriter()
|
||||
zipbook = Writer("csvz")
|
||||
zipbook.open_stream(io)
|
||||
zipbook.write({None: data})
|
||||
zipbook.close()
|
||||
|
@ -89,7 +89,7 @@ def test_reading_from_memory():
|
|||
def test_reading_from_memory_tsvz():
|
||||
data = [[1, 2, 3]]
|
||||
io = manager.get_io("tsvz")
|
||||
zipbook = TSVZipBookWriter()
|
||||
zipbook = Writer("tsvz")
|
||||
zipbook.open_stream(io)
|
||||
zipbook.write({None: data})
|
||||
zipbook.close()
|
||||
|
|
Loading…
Reference in New Issue