Skip to content

[ENG-8488] | Fix inconsistent Excel rendering by enforcing binary mode … #401

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
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
11 changes: 9 additions & 2 deletions mfr/extensions/tabular/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

logger = logging.getLogger(__name__)

BINARY_EXCEL_EXTS = {'.xls', '.xlsx'}

class TabularRenderer(extension.BaseRenderer):

Expand All @@ -30,8 +31,14 @@ def render(self):
extension=self.metadata.ext,
)

with open(self.file_path, errors='replace') as fp:
sheets, size, nbr_rows, nbr_cols = self._render_grid(fp, self.metadata.ext)
ext = (self.metadata.ext or '').lower()
if ext in BINARY_EXCEL_EXTS:
open_kwargs = {'mode': 'rb'}
else:
open_kwargs = {'errors': 'replace'}

with open(self.file_path, **open_kwargs) as fp:
sheets, size, nbr_rows, nbr_cols = self._render_grid(fp, ext)

# Force GC
gc.collect()
Expand Down
39 changes: 13 additions & 26 deletions mfr/extensions/tabular/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,41 +90,28 @@ def sav_to_csv(fp):

def to_bytes(fp):
"""
Return *exactly* the original bytes of the Excel file and rewind *fp*.
Handles both binary and text wrappers that WaterButler may give us.
Return exactly the original bytes and rewind fp.
Requires a binary file-like object or a bytes object.
"""
try:
fp.seek(0)
except Exception:
pass

raw = fp.read()
if isinstance(raw, bytes):
try:
fp.seek(0)
except Exception:
pass
return raw
if isinstance(fp, (bytes, bytearray, memoryview)):
return bytes(fp)

if hasattr(fp, "buffer"):
buf = fp.buffer
if hasattr(fp, "read"):
try:
buf.seek(0)
if hasattr(fp, "seek"):
fp.seek(0)
except Exception:
pass
data = buf.read()
raw = fp.read()
try:
buf.seek(0)
if hasattr(fp, "seek"):
fp.seek(0)
except Exception:
pass
else:
data = raw.encode("utf-8", "surrogateescape")
if isinstance(raw, (bytes, bytearray, memoryview)):
return bytes(raw)

try:
fp.seek(0)
except Exception:
pass
return data
raise TypeError("Expected binary file-like object; got text/str")


def _extract_rows(fields, raw_rows):
Expand Down