Skip to content

Commit

Permalink
👕 bleach the code using white
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed Apr 15, 2018
1 parent 983bb7e commit 47915d1
Show file tree
Hide file tree
Showing 27 changed files with 485 additions and 342 deletions.
10 changes: 5 additions & 5 deletions pyexcel_io/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
"""
import logging
from ._compact import NullHandler

logging.getLogger(__name__).addHandler(NullHandler()) # noqa

from .io import get_data, iget_data, save_data # noqa
import pyexcel_io.plugins as plugins


BLACK_LIST = [__name__, 'pyexcel_webio', 'pyexcel_text']
BLACK_LIST = [__name__, "pyexcel_webio", "pyexcel_text"]
WHITE_LIST = [
'pyexcel_io.readers',
'pyexcel_io.writers',
'pyexcel_io.database']
PREFIX = 'pyexcel_'
"pyexcel_io.readers", "pyexcel_io.writers", "pyexcel_io.database"
]
PREFIX = "pyexcel_"

plugins.load_plugins(PREFIX, __path__, BLACK_LIST, WHITE_LIST)
12 changes: 11 additions & 1 deletion pyexcel_io/_compact.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,29 @@
try:
from logging import NullHandler
except ImportError:

class NullHandler(logging.Handler):

def emit(self, record):
pass


if PY2:
from cStringIO import StringIO
from cStringIO import StringIO as BytesIO

text_type = unicode
irange = xrange

class Iterator(object):

def next(self):
return type(self).__next__(self)


else:
from io import StringIO, BytesIO

text_type = str
Iterator = object
irange = range
Expand All @@ -56,19 +63,22 @@ def isstream(instance):
""" check if a instance is a stream """
try:
import mmap

i_am_not_mmap_obj = not isinstance(instance, mmap.mmap)
except ImportError:
# Python 2.6 or Google App Engine
i_am_not_mmap_obj = True

return hasattr(instance, 'read') and i_am_not_mmap_obj
return hasattr(instance, "read") and i_am_not_mmap_obj


def is_string(atype):
"""find out if a type is str or not"""
if atype == str:
return True

elif PY2:
if atype == unicode:
return True

return False
29 changes: 18 additions & 11 deletions pyexcel_io/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
"""
import pyexcel_io.manager as manager
from pyexcel_io._compact import OrderedDict, isstream, PY2
from .constants import (
MESSAGE_ERROR_03,
MESSAGE_WRONG_IO_INSTANCE
)
from .constants import MESSAGE_ERROR_03, MESSAGE_WRONG_IO_INSTANCE


class RWInterface(object):
Expand Down Expand Up @@ -51,6 +48,7 @@ def close(self):
pass

# implement context manager

def __enter__(self):
return self

Expand All @@ -62,6 +60,7 @@ class BookReader(RWInterface):
"""
Standard book reader
"""

def __init__(self):
super(BookReader, self).__init__()
self._file_name = None
Expand All @@ -86,14 +85,15 @@ def open_stream(self, file_stream, **keywords):
"""
if isstream(file_stream):
if PY2:
if hasattr(file_stream, 'seek'):
if hasattr(file_stream, "seek"):
file_stream.seek(0)
else:
# python 2
# Hei zipfile in odfpy would do a seek
# but stream from urlib cannot do seek
file_stream = _convert_content_to_stream(
file_stream.read(), self._file_type)
file_stream.read(), self._file_type
)
else:
from io import UnsupportedOperation

Expand All @@ -102,7 +102,8 @@ def open_stream(self, file_stream, **keywords):
except UnsupportedOperation:
# python 3
file_stream = _convert_content_to_stream(
file_stream.read(), self._file_type)
file_stream.read(), self._file_type
)

self._file_stream = file_stream
self._keywords = keywords
Expand All @@ -116,18 +117,21 @@ def open_content(self, file_content, **keywords):
keywords are passed on to individual readers
"""
file_stream = _convert_content_to_stream(
file_content, self._file_type)
file_stream = _convert_content_to_stream(file_content, self._file_type)
self.open_stream(file_stream, **keywords)

def read_sheet_by_name(self, sheet_name):
"""
read a named sheet from a excel data book
"""
named_contents = [content for content in self._native_book
if content.name == sheet_name]
named_contents = [
content
for content in self._native_book
if content.name == sheet_name
]
if len(named_contents) == 1:
return {named_contents[0].name: self.read_sheet(named_contents[0])}

else:
raise ValueError("Cannot find sheet %s" % sheet_name)

Expand All @@ -138,6 +142,7 @@ def read_sheet_by_index(self, sheet_index):
try:
sheet = self._native_book[sheet_index]
return {sheet.name: self.read_sheet(sheet)}

except IndexError:
self.close()
raise
Expand Down Expand Up @@ -174,6 +179,7 @@ class BookWriter(RWInterface):
"""
Standard book writer
"""

def __init__(self):
super(BookWriter, self).__init__()
self._file_alike_object = None
Expand All @@ -196,6 +202,7 @@ def open_stream(self, file_stream, **keywords):
"""
if not isstream(file_stream):
raise IOError(MESSAGE_ERROR_03)

self.open(file_stream, **keywords)

def open_content(self, file_stream, **keywords):
Expand Down
39 changes: 19 additions & 20 deletions pyexcel_io/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
:license: New BSD License
"""
# flake8: noqa
DEFAULT_NAME = 'pyexcel'
DEFAULT_SHEET_NAME = '%s_sheet1' % DEFAULT_NAME
DEFAULT_PLUGIN_NAME = '__%s_io_plugins__' % DEFAULT_NAME
DEFAULT_NAME = "pyexcel"
DEFAULT_SHEET_NAME = "%s_sheet1" % DEFAULT_NAME
DEFAULT_PLUGIN_NAME = "__%s_io_plugins__" % DEFAULT_NAME

MESSAGE_INVALID_PARAMETERS = "Invalid parameters"
MESSAGE_ERROR_02 = "No content, file name. Nothing is given"
Expand All @@ -26,27 +26,26 @@
MESSAGE_DB_EXCEPTION = """
Warning: Bulk insertion got below exception. Trying to do it one by one slowly."""

FILE_FORMAT_CSV = 'csv'
FILE_FORMAT_TSV = 'tsv'
FILE_FORMAT_CSVZ = 'csvz'
FILE_FORMAT_TSVZ = 'tsvz'
FILE_FORMAT_ODS = 'ods'
FILE_FORMAT_XLS = 'xls'
FILE_FORMAT_XLSX = 'xlsx'
FILE_FORMAT_XLSM = 'xlsm'
DB_SQL = 'sql'
DB_DJANGO = 'django'
KEYWORD_TSV_DIALECT = 'excel-tab'
KEYWORD_LINE_TERMINATOR = 'lineterminator'
FILE_FORMAT_CSV = "csv"
FILE_FORMAT_TSV = "tsv"
FILE_FORMAT_CSVZ = "csvz"
FILE_FORMAT_TSVZ = "tsvz"
FILE_FORMAT_ODS = "ods"
FILE_FORMAT_XLS = "xls"
FILE_FORMAT_XLSX = "xlsx"
FILE_FORMAT_XLSM = "xlsm"
DB_SQL = "sql"
DB_DJANGO = "django"
KEYWORD_TSV_DIALECT = "excel-tab"
KEYWORD_LINE_TERMINATOR = "lineterminator"

SKIP_DATA = -1
TAKE_DATA = 0
STOP_ITERATION = 1


DEFAULT_MULTI_CSV_SEPARATOR = '__'
SEPARATOR_FORMATTER = '---%s---' % DEFAULT_NAME + "%s"
DEFAULT_MULTI_CSV_SEPARATOR = "__"
SEPARATOR_FORMATTER = "---%s---" % DEFAULT_NAME + "%s"
SEPARATOR_MATCHER = "---%s:(.*)---" % DEFAULT_NAME
DEFAULT_CSV_STREAM_FILE_FORMATTER = (
"---%s:" % DEFAULT_NAME + "%s---%s")
DEFAULT_CSV_NEWLINE = '\r\n'
DEFAULT_CSV_STREAM_FILE_FORMATTER = ("---%s:" % DEFAULT_NAME + "%s---%s")
DEFAULT_CSV_NEWLINE = "\r\n"
12 changes: 6 additions & 6 deletions pyexcel_io/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@


IOPluginInfoChain(__name__).add_a_reader(
relative_plugin_class_path='exporters.django.DjangoBookReader',
file_types=[DB_DJANGO]
relative_plugin_class_path="exporters.django.DjangoBookReader",
file_types=[DB_DJANGO],
).add_a_reader(
relative_plugin_class_path='exporters.sqlalchemy.SQLBookReader',
relative_plugin_class_path="exporters.sqlalchemy.SQLBookReader",
file_types=[DB_SQL],
).add_a_writer(
relative_plugin_class_path='importers.django.DjangoBookWriter',
relative_plugin_class_path="importers.django.DjangoBookWriter",
file_types=[DB_DJANGO],
).add_a_writer(
relative_plugin_class_path='importers.sqlalchemy.SQLBookWriter',
file_types=[DB_SQL]
relative_plugin_class_path="importers.sqlalchemy.SQLBookWriter",
file_types=[DB_SQL],
)
17 changes: 14 additions & 3 deletions pyexcel_io/database/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

class DbExporter(BookReader):
""" Transcode the book reader interface to db interface """

def open(self, file_name, **keywords):
self.export_tables(self, file_name, **keywords)

Expand All @@ -28,6 +29,7 @@ def export_tables(self, exporter, **keywords):

class DjangoModelExportAdapter(object):
""" django export parameter holder """

def __init__(self, model, export_columns=None):
self.model = model
self.export_columns = export_columns
Expand All @@ -44,8 +46,10 @@ def get_name(self):

class DjangoModelImportAdapter(DjangoModelExportAdapter):
""" parameter holder for django data import """

class InOutParameter(object):
""" local class to manipulate variable io """

def __init__(self):
self.output = None
self.input = None
Expand Down Expand Up @@ -102,14 +106,16 @@ def _process_parameters(self):
if self.__column_names.input:
self.__column_names.output = [
self.__column_name_mapping_dict.input[name]
for name in self.__column_names.input]
for name in self.__column_names.input
]
self.__column_name_mapping_dict.output = None
if self.__column_names.output is None:
self.__column_names.output = self.__column_names.input


class DjangoModelExporter(object):
""" public interface for django model export """

def __init__(self):
self.adapters = []

Expand All @@ -120,6 +126,7 @@ def append(self, import_adapter):

class DjangoModelImporter(object):
""" public interface for django model import """

def __init__(self):
self.__adapters = {}

Expand All @@ -134,33 +141,37 @@ def get(self, name):

class SQLTableExportAdapter(DjangoModelExportAdapter):
""" parameter holder for sql table data export """

def __init__(self, model, export_columns=None):
DjangoModelExportAdapter.__init__(self, model, export_columns)
self.table = model

def get_name(self):
return getattr(self.table, '__tablename__', None)
return getattr(self.table, "__tablename__", None)


class SQLTableImportAdapter(DjangoModelImportAdapter):
""" parameter holder for sqlalchemy table import """

def __init__(self, model):
DjangoModelImportAdapter.__init__(self, model)
self.table = model

def get_name(self):
return getattr(self.table, '__tablename__', None)
return getattr(self.table, "__tablename__", None)


class SQLTableExporter(DjangoModelExporter):
""" public interface for sql table export """

def __init__(self, session):
DjangoModelExporter.__init__(self)
self.session = session


class SQLTableImporter(DjangoModelImporter):
""" public interface to do data import via sqlalchemy """

def __init__(self, session):
DjangoModelImporter.__init__(self)
self.session = session
17 changes: 10 additions & 7 deletions pyexcel_io/database/exporters/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@
class DjangoModelReader(QuerysetsReader):
"""Read from django model
"""

def __init__(self, model, export_columns=None, **keywords):
self.__model = model
if export_columns:
column_names = export_columns
else:
column_names = sorted(
[field.attname
for field in self.__model._meta.concrete_fields])
QuerysetsReader.__init__(self, self.__model.objects.all(),
column_names,
**keywords)
[field.attname for field in self.__model._meta.concrete_fields]
)
QuerysetsReader.__init__(
self, self.__model.objects.all(), column_names, **keywords
)


class DjangoBookReader(DbExporter):
""" read django models """

def __init__(self):
DbExporter.__init__(self)
self.exporter = None
Expand All @@ -38,8 +40,9 @@ def export_tables(self, file_content, **keywords):
self._load_from_django_models()

def read_sheet(self, native_sheet):
reader = DjangoModelReader(native_sheet.model,
native_sheet.export_columns)
reader = DjangoModelReader(
native_sheet.model, native_sheet.export_columns
)
return reader.to_array()

def _load_from_django_models(self):
Expand Down

0 comments on commit 47915d1

Please sign in to comment.