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

Skip to content

Commit 17ae529

Browse files
author
Steve Canny
committed
refac: remove now dead code that was replaced
1 parent c8e0a4f commit 17ae529

File tree

3 files changed

+3
-289
lines changed

3 files changed

+3
-289
lines changed

docx/image/constants.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,6 @@ class PNG_CHUNK_TYPE(object):
113113
IEND = 'IEND'
114114

115115

116-
class TAG(object):
117-
"""
118-
Identifiers for image attribute tags.
119-
"""
120-
PX_WIDTH = 'px_width'
121-
PX_HEIGHT = 'px_height'
122-
HORZ_PX_PER_UNIT = 'horz_px_per_unit'
123-
VERT_PX_PER_UNIT = 'vert_px_per_unit'
124-
UNITS_SPECIFIER = 'units_specifier'
125-
126-
127116
class TIFF_FLD_TYPE(object):
128117
"""
129118
Tag codes for TIFF Image File Directory (IFD) entries.

docx/image/png.py

Lines changed: 1 addition & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from __future__ import absolute_import, division, print_function
44

5-
from .constants import MIME_TYPE, PNG_CHUNK_TYPE, TAG
5+
from .constants import MIME_TYPE, PNG_CHUNK_TYPE
66
from .exceptions import InvalidImageStreamError
77
from .helpers import BIG_ENDIAN, StreamReader
88
from .image import BaseImageHeader
@@ -40,97 +40,6 @@ def from_stream(cls, stream):
4040

4141
return cls(px_width, px_height, horz_dpi, vert_dpi)
4242

43-
@classmethod
44-
def _parse_png_headers(cls, stream):
45-
"""
46-
Return a dict of field, value pairs parsed from the PNG chunks in
47-
*stream*.
48-
"""
49-
chunk_offsets = cls._parse_chunk_offsets(stream)
50-
attrs = cls._parse_chunks(stream, chunk_offsets)
51-
return attrs
52-
53-
@classmethod
54-
def _parse_chunk_offsets(cls, stream):
55-
"""
56-
Return a dict of chunk_type, offset(s) parsed from the chunks in
57-
*stream*. The offsets for a chunk type that may appear more than once
58-
are returned as a list regardless of their actual number in *stream*.
59-
"""
60-
chunk_offsets = {}
61-
for chunk_type, offset in cls._iter_chunk_offsets(stream):
62-
# this would need to be more sophisticated if we needed any of
63-
# the chunks that can appear multiple times
64-
chunk_offsets[chunk_type] = offset
65-
return chunk_offsets
66-
67-
@staticmethod
68-
def _iter_chunk_offsets(stream):
69-
"""
70-
Generate a (chunk_type, chunk_offset) 2-tuple for each of the chunks
71-
in the PNG image stream. Iteration stops after the IEND chunk is
72-
returned.
73-
"""
74-
chunk_offset = 8
75-
while True:
76-
chunk_data_len = stream.read_long(chunk_offset)
77-
chunk_type = stream.read_str(4, chunk_offset, 4)
78-
data_offset = chunk_offset + 8
79-
yield chunk_type, data_offset
80-
if chunk_type == _CHUNK_TYPE_IEND:
81-
break
82-
# incr offset for chunk len long, chunk type, chunk data, and CRC
83-
chunk_offset += (4 + 4 + chunk_data_len + 4)
84-
85-
@classmethod
86-
def _parse_chunks(cls, stream, chunk_offsets):
87-
"""
88-
Return a dict of field, value pairs parsed from selected chunks in
89-
the PNG image in *stream*, using *chunk_offsets* to locate the
90-
desired chunks.
91-
"""
92-
attrs = {}
93-
94-
# IHDR chunk -------------------
95-
if _CHUNK_TYPE_IHDR not in chunk_offsets:
96-
# IHDR chunk is mandatory, invalid if not present
97-
raise InvalidImageStreamError('no IHDR chunk in PNG image')
98-
ihdr_offset = chunk_offsets[_CHUNK_TYPE_IHDR]
99-
ihdr_attrs = cls._parse_IHDR(stream, ihdr_offset)
100-
attrs.update(ihdr_attrs)
101-
102-
# pHYs chunk -------------------
103-
if _CHUNK_TYPE_pHYs in chunk_offsets:
104-
phys_offset = chunk_offsets[_CHUNK_TYPE_pHYs]
105-
phys_attrs = cls._parse_pHYs(stream, phys_offset)
106-
attrs.update(phys_attrs)
107-
108-
return attrs
109-
110-
@classmethod
111-
def _parse_IHDR(cls, stream, offset):
112-
"""
113-
Return a dict containing values for TAG.PX_WIDTH and TAG.PX_HEIGHT
114-
extracted from the IHDR chunk in *stream* at *offset*.
115-
"""
116-
return {
117-
TAG.PX_WIDTH: stream.read_long(offset),
118-
TAG.PX_HEIGHT: stream.read_long(offset, 4)
119-
}
120-
121-
@classmethod
122-
def _parse_pHYs(cls, stream, offset):
123-
"""
124-
Return a dict containing values for TAG.HORZ_PX_PER_UNIT,
125-
TAG.VERT_PX_PER_UNIT, and TAG.UNITS_SPECIFIER parsed from the pHYs
126-
chunk at *offset* in *stream*.
127-
"""
128-
return {
129-
TAG.HORZ_PX_PER_UNIT: stream.read_long(offset),
130-
TAG.VERT_PX_PER_UNIT: stream.read_long(offset, 4),
131-
TAG.UNITS_SPECIFIER: stream.read_byte(offset, 8)
132-
}
133-
13443

13544
class _PngParser(object):
13645
"""

tests/image/test_png.py

Lines changed: 2 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from mock import call
1212

1313
from docx.compat import BytesIO
14-
from docx.image.constants import MIME_TYPE, PNG_CHUNK_TYPE, TAG
14+
from docx.image.constants import MIME_TYPE, PNG_CHUNK_TYPE
1515
from docx.image.exceptions import InvalidImageStreamError
1616
from docx.image.helpers import BIG_ENDIAN, StreamReader
1717
from docx.image.png import (
@@ -20,8 +20,7 @@
2020
)
2121

2222
from ..unitutil import (
23-
function_mock, class_mock, initializer_mock, instance_mock, method_mock,
24-
test_file
23+
function_mock, class_mock, initializer_mock, instance_mock, method_mock
2524
)
2625

2726

@@ -36,97 +35,12 @@ def it_can_construct_from_a_png_stream(self, from_stream_fixture):
3635
Png__init__.assert_called_once_with(cx, cy, horz_dpi, vert_dpi)
3736
assert isinstance(png, Png)
3837

39-
def it_parses_PNG_headers_to_access_attrs(self, parse_png_fixture):
40-
(stream_, _parse_chunk_offsets_, _parse_chunks_, chunk_offsets,
41-
attrs_) = parse_png_fixture
42-
attrs = Png._parse_png_headers(stream_)
43-
_parse_chunk_offsets_.assert_called_once_with(stream_)
44-
_parse_chunks_.assert_called_once_with(stream_, chunk_offsets)
45-
assert attrs == attrs_
46-
47-
def it_parses_chunk_offsets_to_help_chunk_parser(
48-
self, chunk_offset_fixture):
49-
stream, expected_chunk_offsets = chunk_offset_fixture
50-
chunk_offsets = Png._parse_chunk_offsets(stream)
51-
assert chunk_offsets == expected_chunk_offsets
52-
53-
def it_parses_chunks_to_extract_fields(self, parse_chunks_fixture):
54-
(stream_, chunk_offsets, _parse_IHDR_, ihdr_offset, _parse_pHYs_,
55-
phys_offset, expected_attrs) = parse_chunks_fixture
56-
attrs = Png._parse_chunks(stream_, chunk_offsets)
57-
_parse_IHDR_.assert_called_once_with(stream_, ihdr_offset)
58-
if phys_offset is not None:
59-
_parse_pHYs_.assert_called_once_with(stream_, phys_offset)
60-
assert attrs == expected_attrs
61-
62-
def it_raises_on_png_having_no_IHDR_chunk(self, no_IHDR_fixture):
63-
stream_, chunk_offsets = no_IHDR_fixture
64-
with pytest.raises(InvalidImageStreamError):
65-
Png._parse_chunks(stream_, chunk_offsets)
66-
67-
def it_can_parse_an_IHDR_chunk(self, parse_IHDR_fixture):
68-
stream, offset, expected_attrs = parse_IHDR_fixture
69-
attrs = Png._parse_IHDR(stream, offset)
70-
assert attrs == expected_attrs
71-
72-
def it_can_parse_an_pHYs_chunk(self, parse_pHYs_fixture):
73-
stream, offset, expected_attrs = parse_pHYs_fixture
74-
attrs = Png._parse_pHYs(stream, offset)
75-
assert attrs == expected_attrs
76-
7738
def it_knows_its_content_type(self):
7839
png = Png(None, None, None, None)
7940
assert png.content_type == MIME_TYPE.PNG
8041

81-
# def it_knows_its_dpi(self, dpi_fixture):
82-
# png, expected_dpi = dpi_fixture
83-
# assert png.horz_dpi == expected_dpi
84-
# assert png.vert_dpi == expected_dpi
85-
8642
# fixtures -------------------------------------------------------
8743

88-
@pytest.fixture
89-
def attrs(self):
90-
return dict()
91-
92-
@pytest.fixture
93-
def attrs_(self, request):
94-
return instance_mock(request, dict)
95-
96-
@pytest.fixture(params=[
97-
('150-dpi.png', {
98-
'IHDR': 16, 'pHYs': 41, 'iCCP': 62, 'cHRM': 2713, 'IDAT': 2757,
99-
'IEND': 146888}),
100-
('300-dpi.png', {
101-
'IHDR': 16, 'pHYs': 41, 'tEXt': 62, 'IDAT': 99, 'IEND': 39917}),
102-
])
103-
def chunk_offset_fixture(self, request):
104-
filename, expected_chunk_offsets = request.param
105-
path = test_file(filename)
106-
with open(path, 'rb') as f:
107-
blob = f.read()
108-
stream = BytesIO(blob)
109-
stream_rdr = StreamReader(stream, BIG_ENDIAN)
110-
return stream_rdr, expected_chunk_offsets
111-
112-
@pytest.fixture
113-
def chunk_offsets(self, request):
114-
return dict()
115-
116-
@pytest.fixture(params=[
117-
(5906, 1, 150), (11811, 1, 300), (5906, 0, 72), (None, 0, 72),
118-
(666, 0, 72), (2835, 1, 72)
119-
])
120-
def dpi_fixture(self, request):
121-
px_per_unit, units_specifier, expected_dpi = request.param
122-
attrs = {
123-
TAG.HORZ_PX_PER_UNIT: px_per_unit,
124-
TAG.VERT_PX_PER_UNIT: px_per_unit,
125-
TAG.UNITS_SPECIFIER: units_specifier
126-
}
127-
png = Png(None, None, None, attrs)
128-
return png, expected_dpi
129-
13044
@pytest.fixture
13145
def from_stream_fixture(
13246
self, stream_, _PngParser_, png_parser_, Png__init__):
@@ -140,98 +54,10 @@ def from_stream_fixture(
14054
horz_dpi, vert_dpi
14155
)
14256

143-
@pytest.fixture
144-
def no_IHDR_fixture(self, stream_, chunk_offsets):
145-
return stream_, chunk_offsets
146-
147-
@pytest.fixture(params=[(42, 24), (42, None)])
148-
def parse_chunks_fixture(
149-
self, request, stream_rdr_, _parse_IHDR_, _parse_pHYs_):
150-
ihdr_offset, phys_offset = request.param
151-
chunk_offsets = {'IHDR': ihdr_offset}
152-
expected_attrs = dict(_parse_IHDR_.return_value)
153-
if phys_offset is not None:
154-
chunk_offsets['pHYs'] = phys_offset
155-
expected_attrs.update(_parse_pHYs_.return_value)
156-
return (
157-
stream_rdr_, chunk_offsets, _parse_IHDR_, ihdr_offset,
158-
_parse_pHYs_, phys_offset, expected_attrs
159-
)
160-
161-
@pytest.fixture
162-
def parse_IHDR_fixture(self):
163-
bytes_ = b'\x00\x00\x00\x2A\x00\x00\x00\x18'
164-
stream = BytesIO(bytes_)
165-
stream_rdr = StreamReader(stream, BIG_ENDIAN)
166-
offset = 0
167-
expected_attrs = {TAG.PX_WIDTH: 42, TAG.PX_HEIGHT: 24}
168-
return stream_rdr, offset, expected_attrs
169-
170-
@pytest.fixture
171-
def parse_pHYs_fixture(self):
172-
bytes_ = b'\x00\x00\x17\x12\x00\x00\x1E\xC2\x01'
173-
stream = BytesIO(bytes_)
174-
stream_rdr = StreamReader(stream, BIG_ENDIAN)
175-
offset = 0
176-
expected_attrs = {
177-
TAG.HORZ_PX_PER_UNIT: 5906, TAG.VERT_PX_PER_UNIT: 7874,
178-
TAG.UNITS_SPECIFIER: 1
179-
}
180-
return stream_rdr, offset, expected_attrs
181-
182-
@pytest.fixture
183-
def parse_png_fixture(
184-
self, stream_rdr_, _parse_chunk_offsets_, _parse_chunks_,
185-
chunk_offsets, attrs_):
186-
chunk_offsets['IHDR'] = 666
187-
return (
188-
stream_rdr_, _parse_chunk_offsets_, _parse_chunks_,
189-
chunk_offsets, attrs_
190-
)
191-
192-
@pytest.fixture
193-
def _parse_chunk_offsets_(self, request, chunk_offsets):
194-
return method_mock(
195-
request, Png, '_parse_chunk_offsets', return_value=chunk_offsets
196-
)
197-
198-
@pytest.fixture
199-
def _parse_chunks_(self, request, attrs_):
200-
return method_mock(
201-
request, Png, '_parse_chunks', return_value=attrs_
202-
)
203-
204-
@pytest.fixture
205-
def _parse_IHDR_(self, request):
206-
return method_mock(
207-
request, Png, '_parse_IHDR', return_value={
208-
TAG.PX_WIDTH: 12, TAG.PX_HEIGHT: 34
209-
}
210-
)
211-
212-
@pytest.fixture
213-
def _parse_pHYs_(self, request):
214-
return method_mock(
215-
request, Png, '_parse_pHYs', return_value={
216-
TAG.HORZ_PX_PER_UNIT: 56, TAG.VERT_PX_PER_UNIT: 78,
217-
TAG.UNITS_SPECIFIER: 1
218-
}
219-
)
220-
221-
@pytest.fixture
222-
def _parse_png_headers_(self, request, attrs):
223-
return method_mock(
224-
request, Png, '_parse_png_headers', return_value=attrs
225-
)
226-
22757
@pytest.fixture
22858
def Png__init__(self, request):
22959
return initializer_mock(request, Png)
23060

231-
@pytest.fixture
232-
def png_(self, request):
233-
return instance_mock(request, Png)
234-
23561
@pytest.fixture
23662
def _PngParser_(self, request, png_parser_):
23763
_PngParser_ = class_mock(request, 'docx.image.png._PngParser')
@@ -242,20 +68,10 @@ def _PngParser_(self, request, png_parser_):
24268
def png_parser_(self, request):
24369
return instance_mock(request, _PngParser)
24470

245-
@pytest.fixture
246-
def StreamReader_(self, request, stream_rdr_):
247-
return class_mock(
248-
request, 'docx.image.png.StreamReader', return_value=stream_rdr_
249-
)
250-
25171
@pytest.fixture
25272
def stream_(self, request):
25373
return instance_mock(request, BytesIO)
25474

255-
@pytest.fixture
256-
def stream_rdr_(self, request):
257-
return instance_mock(request, StreamReader)
258-
25975

26076
class Describe_PngParser(object):
26177

0 commit comments

Comments
 (0)