Skip to content

Commit 9d7fc7e

Browse files
author
Steve Canny
committed
img: remove now-dead ImageCollection
* remove PartCollection, as this was the last remaining subclass. * Remove tests and test file. * remove now-dead Package.after_unmarshal()
1 parent 1682b2b commit 9d7fc7e

File tree

7 files changed

+2
-180
lines changed

7 files changed

+2
-180
lines changed

pptx/package.py

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from pptx.opc.constants import RELATIONSHIP_TYPE as RT
1313
from pptx.opc.package import OpcPackage
1414
from pptx.parts.coreprops import CoreProperties
15-
from pptx.parts.image import Image, ImageCollection, ImagePart
15+
from pptx.parts.image import Image, ImagePart
1616
from pptx.util import lazyproperty
1717

1818

@@ -31,14 +31,6 @@ class Package(OpcPackage):
3131
os.path.split(__file__)[0], 'templates', 'default.pptx'
3232
)
3333

34-
def after_unmarshal(self):
35-
"""
36-
Called by loading code after all parts and relationships have been
37-
loaded, to afford the opportunity for any required post-processing.
38-
"""
39-
# gather image parts into _images
40-
self._images.load(self.parts)
41-
4234
@lazyproperty
4335
def core_properties(self):
4436
"""
@@ -79,14 +71,6 @@ def presentation(self):
7971
"""
8072
return self.main_document
8173

82-
@lazyproperty
83-
def _images(self):
84-
"""
85-
Collection containing a reference to each of the image parts in this
86-
package.
87-
"""
88-
return ImageCollection()
89-
9074
@lazyproperty
9175
def _image_parts(self):
9276
"""

pptx/parts/image.py

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616
from StringIO import StringIO
1717

1818
from pptx.opc.package import Part
19-
from pptx.opc.packuri import PackURI
2019
from pptx.opc.spec import image_content_types
21-
from pptx.parts.part import PartCollection
2220
from pptx.util import lazyproperty, Px
2321

2422

@@ -162,61 +160,6 @@ def _size(self):
162160
return width_px, height_px
163161

164162

165-
class ImageCollection(PartCollection):
166-
"""
167-
Immutable sequence of images, typically belonging to an instance of
168-
|Package|. An image part containing a particular image blob appears only
169-
once in an instance, regardless of how many times it is referenced by a
170-
pic shape in a slide.
171-
"""
172-
def __init__(self):
173-
super(ImageCollection, self).__init__()
174-
175-
def add_image(self, file):
176-
"""
177-
Return image part containing the image in *file*, which is either a
178-
path to an image file or a file-like object containing an image. If an
179-
image instance containing this same image already exists, that
180-
instance is returned. If it does not yet exist, a new one is created.
181-
"""
182-
# use Image constructor to validate and characterize image file
183-
partname = PackURI('/ppt/media/image1.jpeg') # dummy just for baseURI
184-
image = ImagePart.new(partname, file)
185-
# return matching image if found
186-
for existing_image in self._values:
187-
if existing_image.sha1 == image.sha1:
188-
return existing_image
189-
# otherwise add it to collection and return new image
190-
self._values.append(image)
191-
self._rename_images()
192-
return image
193-
194-
def load(self, parts):
195-
"""
196-
Load the image collection with all the image parts in iterable
197-
*parts*.
198-
"""
199-
def is_image_part(part):
200-
return (
201-
isinstance(part, ImagePart) and
202-
part.partname.startswith('/ppt/media/')
203-
)
204-
for part in parts:
205-
if is_image_part(part):
206-
self.add_part(part)
207-
208-
def _rename_images(self):
209-
"""
210-
Assign partnames like ``/ppt/media/image9.png`` to all images in the
211-
collection. The name portion is always ``image``. The number part
212-
forms a continuous sequence starting at 1 (e.g. 1, 2, 3, ...). The
213-
extension is preserved during renaming.
214-
"""
215-
for idx, image in enumerate(self._values):
216-
partname_str = '/ppt/media/image%d.%s' % (idx+1, image.ext)
217-
image.partname = PackURI(partname_str)
218-
219-
220163
class Image(object):
221164
"""
222165
Immutable value object representing an image such as a JPEG, PNG, or GIF.

pptx/parts/part.py

Lines changed: 0 additions & 32 deletions
This file was deleted.

tests/parts/test_image.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
from pptx.opc.constants import CONTENT_TYPE as CT
1414
from pptx.parts.image import Image, ImagePart
15-
from pptx.package import Package
1615
from pptx.util import Px
1716

1817
from ..unitutil.file import absjoin, test_file_dir
@@ -156,30 +155,3 @@ def image_(self, request):
156155
@pytest.fixture
157156
def _init_(self, request):
158157
return initializer_mock(request, Image)
159-
160-
161-
class DescribeImageCollection(object):
162-
163-
def it_finds_a_matching_image_part_if_there_is_one(self):
164-
pkg = Package.open(images_pptx_path)
165-
matching_idx = 4
166-
matching_image = pkg._images[matching_idx]
167-
# exercise ---------------------
168-
image = pkg._images.add_image(test_image_path)
169-
# verify -----------------------
170-
msg = ("expected images[%d], got images[%d]"
171-
% (matching_idx, pkg._images.index(image)))
172-
assert image is matching_image, msg
173-
174-
def it_adds_an_image_part_if_no_matching_found(self):
175-
pkg = Package.open(images_pptx_path)
176-
expected_partname = '/ppt/media/image8.png'
177-
expected_len = len(pkg._images) + 1
178-
expected_sha1 = '79769f1e202add2e963158b532e36c2c0f76a70c'
179-
# exercise ---------------------
180-
image = pkg._images.add_image(new_image_path)
181-
# verify -----------------------
182-
expected = (expected_partname, expected_len, expected_sha1)
183-
actual = (image.partname, len(pkg._images), image.sha1)
184-
msg = "\nExpected: %s\n Got: %s" % (expected, actual)
185-
assert actual == expected, msg

tests/parts/test_part.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

tests/test_files/with_images.pptx

-368 KB
Binary file not shown.

tests/test_package.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,12 @@
1616
from pptx.parts.presentation import PresentationPart
1717

1818

19-
from .unitutil.file import absjoin, test_file_dir
19+
from .unitutil.file import absjoin
2020
from .unitutil.mock import (
2121
class_mock, instance_mock, method_mock, property_mock
2222
)
2323

2424

25-
images_pptx_path = absjoin(test_file_dir, 'with_images.pptx')
26-
27-
2825
class DescribePackage(object):
2926

3027
def it_loads_default_template_when_opened_with_no_path(self):
@@ -37,10 +34,6 @@ def it_loads_default_template_when_opened_with_no_path(self):
3734
assert slide_layouts is not None
3835
assert len(slide_layouts) == 11
3936

40-
def it_gathers_package_image_parts_on_open(self):
41-
pkg = Package.open(images_pptx_path)
42-
assert len(pkg._images) == 7
43-
4437
def it_provides_ref_to_package_presentation_part(self):
4538
pkg = Package.open()
4639
assert isinstance(pkg.presentation, PresentationPart)

0 commit comments

Comments
 (0)