Skip to content

Commit 86ea283

Browse files
author
Steve Canny
committed
img: add _TiffParser._make_stream_reader()
1 parent 2b6497b commit 86ea283

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

docx/image/tiff.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import absolute_import, division, print_function
44

5+
from .helpers import BIG_ENDIAN, LITTLE_ENDIAN, StreamReader
56
from .image import Image
67

78

@@ -80,14 +81,25 @@ def vert_dpi(self):
8081
"""
8182
raise NotImplementedError
8283

84+
@classmethod
85+
def _detect_endian(cls, stream):
86+
"""
87+
Return either BIG_ENDIAN or LITTLE_ENDIAN depending on the endian
88+
indicator found in the TIFF *stream* header, either 'MM' or 'II'.
89+
"""
90+
stream.seek(0)
91+
endian_str = stream.read(2)
92+
return BIG_ENDIAN if endian_str == b'MM' else LITTLE_ENDIAN
93+
8394
@classmethod
8495
def _make_stream_reader(cls, stream):
8596
"""
8697
Return a |StreamReader| instance with wrapping *stream* and having
8798
"endian-ness" determined by the 'MM' or 'II' indicator in the TIFF
8899
stream header.
89100
"""
90-
raise NotImplementedError
101+
endian = cls._detect_endian(stream)
102+
return StreamReader(stream, endian)
91103

92104

93105
class _IfdEntries(object):

tests/image/test_tiff.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import pytest
1010

1111
from docx.compat import BytesIO
12-
from docx.image.helpers import StreamReader
12+
from docx.image.helpers import BIG_ENDIAN, LITTLE_ENDIAN, StreamReader
1313
from docx.image.tiff import _IfdEntries, Tiff, _TiffParser
1414

1515
from ..unitutil import (
@@ -87,6 +87,12 @@ def it_can_parse_the_properties_from_a_tiff_stream(
8787
_TiffParser__init_.assert_called_once_with(ifd_entries_)
8888
assert isinstance(tiff_parser, _TiffParser)
8989

90+
def it_makes_a_stream_reader_to_help_parse(self, mk_stream_rdr_fixture):
91+
stream, StreamReader_, endian, stream_rdr_ = mk_stream_rdr_fixture
92+
stream_rdr = _TiffParser._make_stream_reader(stream)
93+
StreamReader_.assert_called_once_with(stream, endian)
94+
assert stream_rdr is stream_rdr_
95+
9096
# fixtures -------------------------------------------------------
9197

9298
@pytest.fixture
@@ -119,10 +125,25 @@ def _make_stream_reader_(self, request, stream_rdr_):
119125
return_value=stream_rdr_
120126
)
121127

128+
@pytest.fixture(params=[
129+
(b'MM\x00*', BIG_ENDIAN),
130+
(b'II*\x00', LITTLE_ENDIAN),
131+
])
132+
def mk_stream_rdr_fixture(self, request, StreamReader_, stream_rdr_):
133+
bytes_, endian = request.param
134+
stream = BytesIO(bytes_)
135+
return stream, StreamReader_, endian, stream_rdr_
136+
122137
@pytest.fixture
123138
def stream_(self, request):
124139
return instance_mock(request, BytesIO)
125140

141+
@pytest.fixture
142+
def StreamReader_(self, request, stream_rdr_):
143+
return class_mock(
144+
request, 'docx.image.tiff.StreamReader', return_value=stream_rdr_
145+
)
146+
126147
@pytest.fixture
127148
def stream_rdr_(self, request, ifd0_offset_):
128149
stream_rdr_ = instance_mock(request, StreamReader)

0 commit comments

Comments
 (0)