Skip to content

Refactor django and sqlalchemy writers #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pyexcel_io/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
FILE_FORMAT_XLS = "xls"
FILE_FORMAT_XLSX = "xlsx"
FILE_FORMAT_XLSM = "xlsm"
FILE_FORMAT_XLSB = "xlsb"
FILE_FORMAT_HTML = "html"
FILE_FORMAT_PDF = "pdf"
DB_SQL = "sql"
DB_DJANGO = "django"
DB_QUERYSET = "queryset"
Expand Down
12 changes: 2 additions & 10 deletions pyexcel_io/database/importers/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import pyexcel_io.constants as constants
from pyexcel_io.sheet import SheetWriter
from pyexcel_io.utils import is_empty_array, swap_empty_string_for_none
from pyexcel_io.plugin_api.abstract_writer import IWriter

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -57,7 +58,7 @@ def close(self):
an_object.save()


class DjangoBookWriter(object):
class DjangoBookWriter(IWriter):
""" write data into django models """

def __init__(self, exporter, _, **keywords):
Expand All @@ -82,14 +83,5 @@ def create_sheet(self, sheet_name):

return sheet_writer

def write(self, incoming_dict):
for sheet_name in incoming_dict:
sheet_writer = self.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):
pass
12 changes: 2 additions & 10 deletions pyexcel_io/database/importers/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pyexcel_io.constants as constants
from pyexcel_io.sheet import SheetWriter
from pyexcel_io.utils import is_empty_array, swap_empty_string_for_none
from pyexcel_io.plugin_api.abstract_writer import IWriter


class PyexcelSQLSkipRowException(Exception):
Expand Down Expand Up @@ -71,7 +72,7 @@ def close(self):
self._native_book.session.commit()


class SQLBookWriter(object):
class SQLBookWriter(IWriter):
""" write data into database tables via sqlalchemy """

def __init__(self, file_content, _, auto_commit=True, **keywords):
Expand All @@ -93,14 +94,5 @@ def create_sheet(self, sheet_name):

return sheet_writer

def write(self, incoming_dict):
for sheet_name in incoming_dict:
sheet_writer = self.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):
pass
13 changes: 11 additions & 2 deletions pyexcel_io/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
XLSX_PLUGIN = "pyexcel-xlsx"
ODS_PLUGIN = "pyexcel-ods"
ODS3_PLUGIN = "pyexcel-ods3"
ODSR_PLUGIN = "pyexcel-odsr"
ODSW_PLUGIN = "pyexcel-odsw"
XLSXR_PLUGIN = "pyexcel-xlsxr"
XLSXW_PLUGIN = "pyexcel-xlsxw"
XLSBR_PLUGIN = "pyexcel-xlsbr"
HTMLR_PLUGIN = "pyexcel-htmlr"
PDFR_PLUGIN = "pyexcel-pdfr"
IO_ITSELF = "pyexcel-io"

AVAILABLE_NEW_READERS = {}
Expand All @@ -23,17 +29,20 @@
constants.FILE_FORMAT_XLS: [XLS_PLUGIN],
constants.FILE_FORMAT_XLSX: [XLS_PLUGIN, XLSX_PLUGIN],
constants.FILE_FORMAT_XLSM: [XLS_PLUGIN, XLSX_PLUGIN],
constants.FILE_FORMAT_ODS: [ODS_PLUGIN, ODS3_PLUGIN],
constants.FILE_FORMAT_ODS: [ODS_PLUGIN, ODS3_PLUGIN, ODSR_PLUGIN],
constants.FILE_FORMAT_TSV: [IO_ITSELF],
constants.FILE_FORMAT_CSVZ: [IO_ITSELF],
constants.FILE_FORMAT_TSVZ: [IO_ITSELF],
constants.FILE_FORMAT_XLSB: [XLSBR_PLUGIN],
constants.FILE_FORMAT_HTML: [HTMLR_PLUGIN],
constants.FILE_FORMAT_PDF: [PDFR_PLUGIN],
}

AVAILABLE_WRITERS = {
constants.FILE_FORMAT_XLS: [XLS_PLUGIN],
constants.FILE_FORMAT_XLSX: [XLSX_PLUGIN, XLSXW_PLUGIN],
constants.FILE_FORMAT_XLSM: [XLSX_PLUGIN],
constants.FILE_FORMAT_ODS: [ODS_PLUGIN, ODS3_PLUGIN],
constants.FILE_FORMAT_ODS: [ODS_PLUGIN, ODS3_PLUGIN, ODSW_PLUGIN],
constants.FILE_FORMAT_CSV: [IO_ITSELF],
constants.FILE_FORMAT_TSV: [IO_ITSELF],
constants.FILE_FORMAT_CSVZ: [IO_ITSELF],
Expand Down
4 changes: 2 additions & 2 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_wrong_parameter_to_get_writer():

def test_load_ods_data():
msg = "Please install one of these plugins for read data in 'ods': "
msg += "pyexcel-ods,pyexcel-ods3"
msg += "pyexcel-ods,pyexcel-ods3,pyexcel-odsr"
try:
get_data("test.ods")
except exceptions.SupportingPluginAvailableButNotInstalled as e:
Expand All @@ -85,7 +85,7 @@ def test_load_ods_data():
def test_load_ods_data_from_memory():
io = BytesIO()
msg = "Please install one of these plugins for read data in 'ods': "
msg += "pyexcel-ods,pyexcel-ods3"
msg += "pyexcel-ods,pyexcel-ods3,pyexcel-odsr"
try:
get_data(io, file_type="ods")
except exceptions.SupportingPluginAvailableButNotInstalled as e:
Expand Down