Skip to content

Commit c79684c

Browse files
author
Steve Canny
committed
img: add ImageParts._add_image_part()
1 parent fe8a489 commit c79684c

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

docx/package.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from docx.opc.constants import RELATIONSHIP_TYPE as RT
1010
from docx.opc.package import OpcPackage
11-
from docx.parts.image import Image
11+
from docx.parts.image import Image, ImagePart
1212
from docx.shared import lazyproperty
1313

1414

@@ -76,10 +76,12 @@ def get_or_add_image_part(self, image_descriptor):
7676

7777
def _add_image_part(self, image):
7878
"""
79-
Return the image part in this collection having a SHA1 hash matching
80-
*sha1*, or |None| if not found.
79+
Return an |ImagePart| instance newly created from image and appended
80+
to the collection.
8181
"""
82-
raise NotImplementedError
82+
image_part = ImagePart.from_image(image)
83+
self.append(image_part)
84+
return image_part
8385

8486
def _get_by_sha1(self, sha1):
8587
"""

docx/parts/image.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ def filename(self):
7272
"""
7373
raise NotImplementedError
7474

75+
@classmethod
76+
def from_image(cls, image):
77+
"""
78+
Return an |ImagePart| instance newly created from *image*.
79+
"""
80+
raise NotImplementedError
81+
7582
@property
7683
def height(self):
7784
"""

tests/test_package.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ def it_can_add_a_new_image_part(self, add_image_part_fixture):
3636
image_parts._add_image_part.assert_called_once_with(image_)
3737
assert image_part is image_part_
3838

39+
def it_can_really_add_a_new_image_part(
40+
self, really_add_image_part_fixture):
41+
image_parts, image_, ImagePart_, image_part_ = (
42+
really_add_image_part_fixture
43+
)
44+
image_part = image_parts._add_image_part(image_)
45+
ImagePart_.from_image.assert_called_once_with(image_)
46+
assert image_part in image_parts
47+
assert image_part is image_part_
48+
3949
# fixtures -------------------------------------------------------
4050

4151
@pytest.fixture
@@ -74,6 +84,12 @@ def image_(self, request, sha1):
7484
def image_descriptor_(self, request):
7585
return instance_mock(request, str)
7686

87+
@pytest.fixture
88+
def ImagePart_(self, request, image_part_):
89+
ImagePart_ = class_mock(request, 'docx.package.ImagePart')
90+
ImagePart_.from_image.return_value = image_part_
91+
return ImagePart_
92+
7793
@pytest.fixture
7894
def image_part_(self, request, sha1):
7995
image_part_ = instance_mock(request, ImagePart)
@@ -84,6 +100,11 @@ def image_part_(self, request, sha1):
84100
def new_image_part_(self, request):
85101
return instance_mock(request, ImagePart)
86102

103+
@pytest.fixture
104+
def really_add_image_part_fixture(self, image_, ImagePart_, image_part_):
105+
image_parts = ImageParts()
106+
return image_parts, image_, ImagePart_, image_part_
107+
87108
@pytest.fixture
88109
def sha1(self):
89110
return 'F008AH'

0 commit comments

Comments
 (0)