Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4edbb14

Browse files
author
Steve Canny
committed
img: add _PngParser.parse()
1 parent b437d72 commit 4edbb14

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

docx/image/png.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ def parse(cls, stream):
172172
Return a |_PngParser| instance containing the header properties
173173
parsed from the PNG image in *stream*.
174174
"""
175-
raise NotImplementedError
175+
chunks = _Chunks.from_stream(stream)
176+
return cls(chunks)
176177

177178
@property
178179
def px_width(self):
@@ -203,3 +204,15 @@ def vert_dpi(self):
203204
when not present in the file, as is often the case.
204205
"""
205206
raise NotImplementedError
207+
208+
209+
class _Chunks(object):
210+
"""
211+
Collection of the chunks parsed from a PNG image stream
212+
"""
213+
@classmethod
214+
def from_stream(cls, stream):
215+
"""
216+
Return a |_Chunks| instance containing the PNG chunks in *stream*.
217+
"""
218+
raise NotImplementedError

tests/image/test_png.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from docx.image.constants import MIME_TYPE, TAG
1313
from docx.image.exceptions import InvalidImageStreamError
1414
from docx.image.helpers import BIG_ENDIAN, StreamReader
15-
from docx.image.png import Png, _PngParser
15+
from docx.image.png import _Chunks, Png, _PngParser
1616

1717
from ..unitutil import (
1818
initializer_mock, class_mock, instance_mock, method_mock, test_file
@@ -249,3 +249,37 @@ def stream_(self, request):
249249
@pytest.fixture
250250
def stream_rdr_(self, request):
251251
return instance_mock(request, StreamReader)
252+
253+
254+
class DescribePngParser(object):
255+
256+
def it_can_parse_the_headers_of_a_PNG_stream(self, parse_fixture):
257+
stream_, _Chunks_, _PngParser__init_, chunks_ = parse_fixture
258+
png_parser = _PngParser.parse(stream_)
259+
_Chunks_.from_stream.assert_called_once_with(stream_)
260+
_PngParser__init_.assert_called_once_with(chunks_)
261+
assert isinstance(png_parser, _PngParser)
262+
263+
# fixtures -------------------------------------------------------
264+
265+
@pytest.fixture
266+
def _Chunks_(self, request, chunks_):
267+
_Chunks_ = class_mock(request, 'docx.image.png._Chunks')
268+
_Chunks_.from_stream.return_value = chunks_
269+
return _Chunks_
270+
271+
@pytest.fixture
272+
def chunks_(self, request):
273+
return instance_mock(request, _Chunks)
274+
275+
@pytest.fixture
276+
def parse_fixture(self, stream_, _Chunks_, _PngParser__init_, chunks_):
277+
return stream_, _Chunks_, _PngParser__init_, chunks_
278+
279+
@pytest.fixture
280+
def _PngParser__init_(self, request):
281+
return initializer_mock(request, _PngParser)
282+
283+
@pytest.fixture
284+
def stream_(self, request):
285+
return instance_mock(request, BytesIO)

0 commit comments

Comments
 (0)