Skip to content

Commit 98b4ab2

Browse files
author
Steve Canny
committed
img: add _ChunkParser.from_stream()
1 parent 279fb9f commit 98b4ab2

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

docx/image/png.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from .constants import MIME_TYPE, TAG
66
from .exceptions import InvalidImageStreamError
7+
from .helpers import BIG_ENDIAN, StreamReader
78
from .image import BaseImageHeader
89

910

@@ -224,13 +225,18 @@ class _ChunkParser(object):
224225
"""
225226
Extracts chunks from a PNG image stream
226227
"""
228+
def __init__(self, stream_rdr):
229+
super(_ChunkParser, self).__init__()
230+
self._stream_rdr = stream_rdr
231+
227232
@classmethod
228233
def from_stream(cls, stream):
229234
"""
230235
Return a |_ChunkParser| instance that can extract the chunks from the
231236
PNG image in *stream*.
232237
"""
233-
raise NotImplementedError
238+
stream_rdr = StreamReader(stream, BIG_ENDIAN)
239+
return cls(stream_rdr)
234240

235241
def iter_chunks(self):
236242
"""

tests/image/test_png.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,3 +325,40 @@ def _Chunks__init_(self, request):
325325
@pytest.fixture
326326
def stream_(self, request):
327327
return instance_mock(request, BytesIO)
328+
329+
330+
class Describe_ChunkParser(object):
331+
332+
def it_can_construct_from_a_stream(self, from_stream_fixture):
333+
stream_, StreamReader_, stream_rdr_, _ChunkParser__init_ = (
334+
from_stream_fixture
335+
)
336+
chunk_parser = _ChunkParser.from_stream(stream_)
337+
StreamReader_.assert_called_once_with(stream_, BIG_ENDIAN)
338+
_ChunkParser__init_.assert_called_once_with(stream_rdr_)
339+
assert isinstance(chunk_parser, _ChunkParser)
340+
341+
# fixtures -------------------------------------------------------
342+
343+
@pytest.fixture
344+
def from_stream_fixture(
345+
self, stream_, StreamReader_, stream_rdr_, _ChunkParser__init_):
346+
return stream_, StreamReader_, stream_rdr_, _ChunkParser__init_
347+
348+
@pytest.fixture
349+
def _ChunkParser__init_(self, request):
350+
return initializer_mock(request, _ChunkParser)
351+
352+
@pytest.fixture
353+
def StreamReader_(self, request, stream_rdr_):
354+
return class_mock(
355+
request, 'docx.image.png.StreamReader', return_value=stream_rdr_
356+
)
357+
358+
@pytest.fixture
359+
def stream_(self, request):
360+
return instance_mock(request, BytesIO)
361+
362+
@pytest.fixture
363+
def stream_rdr_(self, request):
364+
return instance_mock(request, StreamReader)

0 commit comments

Comments
 (0)