Skip to content

Commit 51e4880

Browse files
author
Steve Canny
committed
img: add Png._parse_png_headers()
* localize image-related exceptions within the image sub-package to ease migration when module is reused in python-pptx
1 parent 82ced1b commit 51e4880

7 files changed

Lines changed: 104 additions & 4 deletions

File tree

‎docx/image/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import Image as PIL_Image
1919

2020
from docx.compat import BytesIO, is_string
21-
from docx.exceptions import UnrecognizedImageError
2221
from docx.image.bmp import Bmp
22+
from docx.image.exceptions import UnrecognizedImageError
2323
from docx.image.gif import Gif
2424
from docx.image.jpeg import Exif, Jfif
2525
from docx.image.png import Png
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# encoding: utf-8
22

33

4+
class InvalidImageStreamError(Exception):
5+
"""
6+
The recognized image stream appears to be corrupted
7+
"""
8+
9+
410
class UnrecognizedImageError(Exception):
511
"""
612
The provided image stream could not be recognized.

‎docx/image/helpers.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,20 @@
33
from __future__ import absolute_import, division, print_function
44

55

6+
_BIG_ENDIAN = '>'
7+
_LITTLE_ENDIAN = '<'
8+
9+
610
class StreamReader(object):
711
"""
812
Wraps a file-like object to provide access to structured data from a
913
binary file. Byte-order is configurable. *base_offset* is added to any
1014
base value provided to calculate actual location for reads.
1115
"""
16+
def __init__(self, stream, byte_order, base_offset=0):
17+
super(StreamReader, self).__init__()
18+
self._stream = stream
19+
self._byte_order = (
20+
_LITTLE_ENDIAN if byte_order == _LITTLE_ENDIAN else _BIG_ENDIAN
21+
)
22+
self._base_offset = base_offset

‎docx/image/png.py‎

Lines changed: 23 additions & 0 deletions
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 .exceptions import InvalidImageStreamError
56
from .helpers import StreamReader
67
from .image import Image
78

@@ -27,4 +28,26 @@ def _parse_png_headers(cls, stream):
2728
Return a dict of field, value pairs parsed from the PNG chunks in
2829
*stream*.
2930
"""
31+
chunk_offsets = cls._parse_chunk_offsets(stream)
32+
# IHDR chunk is mandatory, invalid if not present
33+
if 'IHDR' not in chunk_offsets:
34+
raise InvalidImageStreamError('no IHDR chunk in PNG image')
35+
attrs = cls._parse_chunks(stream, chunk_offsets)
36+
return attrs
37+
38+
@classmethod
39+
def _parse_chunk_offsets(cls, stream):
40+
"""
41+
Return a dict of chunk_type, offset(s) parsed from the chunks in
42+
*stream*. The offsets for a chunk type that may appear more than once
43+
are returned as a list regardless of their actual number in *stream*.
44+
"""
45+
raise NotImplementedError
46+
47+
@classmethod
48+
def _parse_chunks(cls, stream, chunk_offsets):
49+
"""
50+
Return a dict of field, value pairs parsed from the chunks in
51+
*stream* having offsets in *chunk_offsets*.
52+
"""
3053
raise NotImplementedError

‎tests/image/test_helpers.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# encoding: utf-8
2+
3+
"""
4+
Test suite for docx.image.helpers module
5+
"""
6+
7+
from __future__ import absolute_import, print_function
8+
9+
10+
class DescribeStreamReader(object):
11+
pass

‎tests/image/test_image.py‎

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

1111
from docx.compat import BytesIO
12-
from docx.exceptions import UnrecognizedImageError
1312
from docx.image import image_cls_that_can_parse, Image_OLD
1413
from docx.image.bmp import Bmp
14+
from docx.image.exceptions import UnrecognizedImageError
1515
from docx.image.gif import Gif
1616
from docx.image.image import Image
1717
from docx.image.jpeg import Exif, Jfif

‎tests/image/test_png.py‎

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pytest
1010

1111
from docx.compat import BytesIO
12+
from docx.image.exceptions import InvalidImageStreamError
1213
from docx.image.helpers import StreamReader
1314
from docx.image.png import Png
1415

@@ -31,16 +32,37 @@ def it_can_construct_from_a_png_stream(self, from_stream_fixture):
3132
Png__init__.assert_called_once_with(blob_, filename_, cx, cy, attrs)
3233
assert isinstance(png, Png)
3334

35+
def it_parses_PNG_headers_to_access_attrs(self, parse_png_fixture):
36+
(stream_, _parse_chunk_offsets_, _parse_chunks_, chunk_offsets_,
37+
attrs_) = parse_png_fixture
38+
attrs = Png._parse_png_headers(stream_)
39+
_parse_chunk_offsets_.assert_called_once_with(stream_)
40+
_parse_chunks_.assert_called_once_with(stream_, chunk_offsets_)
41+
assert attrs == attrs_
42+
43+
def it_raises_on_png_having_no_IHDR_chunk(self, no_IHDR_fixture):
44+
stream_ = no_IHDR_fixture
45+
with pytest.raises(InvalidImageStreamError):
46+
Png._parse_png_headers(stream_)
47+
3448
# fixtures -------------------------------------------------------
3549

3650
@pytest.fixture
3751
def attrs(self):
3852
return dict()
3953

54+
@pytest.fixture
55+
def attrs_(self, request):
56+
return instance_mock(request, dict)
57+
4058
@pytest.fixture
4159
def blob_(self, request):
4260
return instance_mock(request, bytes)
4361

62+
@pytest.fixture
63+
def chunk_offsets_(self, request):
64+
return dict()
65+
4466
@pytest.fixture
4567
def filename_(self, request):
4668
return instance_mock(request, str)
@@ -57,15 +79,42 @@ def from_stream_fixture(
5779
)
5880

5981
@pytest.fixture
60-
def Png__init__(self, request):
61-
return initializer_mock(request, Png)
82+
def no_IHDR_fixture(
83+
self, stream_, _parse_chunk_offsets_, _parse_chunks_):
84+
return stream_
85+
86+
@pytest.fixture
87+
def parse_png_fixture(
88+
self, stream_rdr_, _parse_chunk_offsets_, _parse_chunks_,
89+
chunk_offsets_, attrs_):
90+
chunk_offsets_['IHDR'] = 666
91+
return (
92+
stream_rdr_, _parse_chunk_offsets_, _parse_chunks_,
93+
chunk_offsets_, attrs_
94+
)
95+
96+
@pytest.fixture
97+
def _parse_chunk_offsets_(self, request, chunk_offsets_):
98+
return method_mock(
99+
request, Png, '_parse_chunk_offsets', return_value=chunk_offsets_
100+
)
101+
102+
@pytest.fixture
103+
def _parse_chunks_(self, request, attrs_):
104+
return method_mock(
105+
request, Png, '_parse_chunks', return_value=attrs_
106+
)
62107

63108
@pytest.fixture
64109
def _parse_png_headers_(self, request, attrs):
65110
return method_mock(
66111
request, Png, '_parse_png_headers', return_value=attrs
67112
)
68113

114+
@pytest.fixture
115+
def Png__init__(self, request):
116+
return initializer_mock(request, Png)
117+
69118
@pytest.fixture
70119
def png_(self, request):
71120
return instance_mock(request, Png)

0 commit comments

Comments
 (0)