Skip to content

Commit

Permalink
test issue #33 #34
Browse files Browse the repository at this point in the history
  • Loading branch information
chfw committed May 13, 2017
1 parent d82eb39 commit 17ff82f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
11 changes: 3 additions & 8 deletions pyexcel_io/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
:license: New BSD License, see LICENSE for more details
"""
import pyexcel_io.manager as manager
from pyexcel_io._compact import PY2, PY26
from pyexcel_io._compact import OrderedDict, isstream, StringIO
from pyexcel_io._compact import PY2, OrderedDict, isstream, StringIO
from .constants import (
MESSAGE_ERROR_03,
MESSAGE_WRONG_IO_INSTANCE
Expand Down Expand Up @@ -88,12 +87,8 @@ def open_content(self, file_content, **keywords):
keywords are passed on to individual readers
"""
import mmap
if not PY26 and isinstance(file_content, mmap.mmap):
file_stream = file_content
else:
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):
Expand Down
21 changes: 20 additions & 1 deletion pyexcel_io/fileformat/_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,24 @@ def open_stream(self, file_stream, multiple_sheets=False, **keywords):
self.__multiple_sheets = multiple_sheets
self._native_book = self._load_from_stream()

def open_content(self, file_content, multiple_sheets=False, **keywords):
if not compact.PY26:
import mmap
if isinstance(file_content, mmap.mmap):
# load from mmap
self._file_stream = iter(
lambda: file_content.read().decode('utf-8'), '')
self._keywords = keywords
self._native_book = self._load_from_stream()
else:
BookReader.open_content(
self, file_content,
multiple_sheets=multiple_sheets, **keywords)
else:
BookReader.open_content(
self, file_content,
multiple_sheets=multiple_sheets, **keywords)

def read_sheet(self, native_sheet):
if self.__load_from_memory_flag:
reader = CSVinMemoryReader(native_sheet, **self._keywords)
Expand Down Expand Up @@ -286,7 +304,8 @@ def _load_from_stream(self):
named_contents.append(new_sheet)
return named_contents
else:
self._file_stream.seek(0)
if hasattr(self._file_stream, 'seek'):
self._file_stream.seek(0)
return [NamedContent(self._file_type, self._file_stream)]

def _load_from_file(self):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
from nose.tools import eq_
from pyexcel_io import get_data, save_data
from pyexcel_io._compact import PY26


def test_issue_8():
Expand Down Expand Up @@ -48,5 +49,19 @@ def test_issue_28():
eq_(str(e), expected % 'test')


def test_issue_33_34():
if PY26:
pass
else:
import mmap
test_file = get_fixture("issue20.csv")
with open(test_file, 'r+b') as f:
memory_mapped_file = mmap.mmap(
f.fileno(), 0, access=mmap.ACCESS_READ)
data = get_data(memory_mapped_file, file_type='csv')
expected = [[u'to', u'infinity', u'and', u'beyond']]
eq_(data['csv'], expected)


def get_fixture(file_name):
return os.path.join("tests", "fixtures", file_name)

0 comments on commit 17ff82f

Please sign in to comment.