Skip to content

Commit 16a588f

Browse files
author
Steve Canny
committed
img: refactor Image.from_file()
… to remove duplication and to improve naming.
1 parent 994b290 commit 16a588f

File tree

4 files changed

+31
-44
lines changed

4 files changed

+31
-44
lines changed

docx/package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def get_or_add_image_part(self, image_descriptor):
7272
*image_descriptor*, newly created if a matching one is not present in
7373
the collection.
7474
"""
75-
image = Image.load(image_descriptor)
75+
image = Image.from_file(image_descriptor)
7676
matching_image_part = self._get_by_sha1(image.sha1)
7777
if matching_image_part is not None:
7878
return matching_image_part

docx/parts/image.py

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,26 @@ def filename(self):
7373
@classmethod
7474
def from_blob(cls, blob):
7575
stream = StringIO(blob)
76-
content_type, px_width, px_height, horz_dpi, vert_dpi = (
77-
cls._analyze_image(stream)
78-
)
79-
stream.close()
80-
filename = 'image%s' % cls._def_mime_ext(content_type)
81-
return cls(
82-
blob, filename, content_type, px_width, px_height, horz_dpi,
83-
vert_dpi
84-
)
76+
return cls._from_stream(stream, blob)
77+
78+
@classmethod
79+
def from_file(cls, image_descriptor):
80+
"""
81+
Return a new |Image| instance loaded from the image file identified
82+
by *image_descriptor*, a path or file-like object.
83+
"""
84+
if isinstance(image_descriptor, basestring):
85+
path = image_descriptor
86+
with open(path, 'rb') as f:
87+
blob = f.read()
88+
stream = StringIO(blob)
89+
filename = os.path.basename(path)
90+
else:
91+
stream = image_descriptor
92+
stream.seek(0)
93+
blob = stream.read()
94+
filename = None
95+
return cls._from_stream(stream, blob, filename)
8596

8697
@property
8798
def horz_dpi(self):
@@ -91,16 +102,6 @@ def horz_dpi(self):
91102
"""
92103
return self._horz_dpi
93104

94-
@classmethod
95-
def load(cls, image_descriptor):
96-
"""
97-
Return a new |Image| instance loaded from the image file identified
98-
by *image_descriptor*, a path or file-like object.
99-
"""
100-
if isinstance(image_descriptor, basestring):
101-
return cls._load_from_path(image_descriptor)
102-
return cls._load_from_stream(image_descriptor)
103-
104105
@property
105106
def px_width(self):
106107
"""
@@ -166,26 +167,12 @@ def _format_content_type(cls, format):
166167
return format_content_types[format]
167168

168169
@classmethod
169-
def _load_from_path(cls, path):
170-
with open(path, 'rb') as f:
171-
blob = f.read()
172-
content_type, px_width, px_height, horz_dpi, vert_dpi = (
173-
cls._analyze_image(f)
174-
)
175-
filename = os.path.basename(path)
176-
return cls(
177-
blob, filename, content_type, px_width, px_height, horz_dpi,
178-
vert_dpi
179-
)
180-
181-
@classmethod
182-
def _load_from_stream(cls, stream):
183-
stream.seek(0)
184-
blob = stream.read()
170+
def _from_stream(cls, stream, blob, filename=None):
185171
content_type, px_width, px_height, horz_dpi, vert_dpi = (
186172
cls._analyze_image(stream)
187173
)
188-
filename = 'image%s' % cls._def_mime_ext(content_type)
174+
if filename is None:
175+
filename = 'image%s' % cls._def_mime_ext(content_type)
189176
return cls(
190177
blob, filename, content_type, px_width, px_height, horz_dpi,
191178
vert_dpi

tests/parts/test_image.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,28 @@ class DescribeImage(object):
2323

2424
def it_can_construct_from_an_image_path(self):
2525
image_file_path = test_file('monty-truth.png')
26-
image = Image.load(image_file_path)
26+
image = Image.from_file(image_file_path)
2727
assert isinstance(image, Image)
2828
assert image.sha1 == '79769f1e202add2e963158b532e36c2c0f76a70c'
2929
assert image.filename == 'monty-truth.png'
3030

3131
def it_can_construct_from_an_image_stream(self):
3232
image_file_path = test_file('monty-truth.png')
3333
with open(image_file_path, 'rb') as image_file_stream:
34-
image = Image.load(image_file_stream)
34+
image = Image.from_file(image_file_stream)
3535
assert isinstance(image, Image)
3636
assert image.sha1 == '79769f1e202add2e963158b532e36c2c0f76a70c'
3737
assert image.filename == 'image.png'
3838

3939
def it_knows_the_extension_of_a_file_based_image(self):
4040
image_file_path = test_file('monty-truth.png')
41-
image = Image.load(image_file_path)
41+
image = Image.from_file(image_file_path)
4242
assert image.ext == '.png'
4343

4444
def it_knows_the_extension_of_a_stream_based_image(self):
4545
image_file_path = test_file('monty-truth.png')
4646
with open(image_file_path, 'rb') as image_file_stream:
47-
image = Image.load(image_file_stream)
47+
image = Image.from_file(image_file_stream)
4848
assert image.ext == '.png'
4949

5050
def it_correctly_characterizes_a_few_known_images(
@@ -54,7 +54,7 @@ def it_correctly_characterizes_a_few_known_images(
5454
characteristics
5555
)
5656
with open(test_file(image_path), 'rb') as stream:
57-
image = Image.load(stream)
57+
image = Image.from_file(stream)
5858
assert image.ext == ext
5959
assert image.content_type == content_type
6060
assert image.px_width == px_width
@@ -121,7 +121,7 @@ def blob_(self, request):
121121
@pytest.fixture(params=['loaded', 'new'])
122122
def dimensions_fixture(self, request):
123123
image_file_path = test_file('monty-truth.png')
124-
image = Image.load(image_file_path)
124+
image = Image.from_file(image_file_path)
125125
expected_cx, expected_cy = 1905000, 2717800
126126

127127
# case 1: image part is loaded by PartFactory w/no Image inst

tests/test_package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def get_image_part_fixture(self, Image_, image_part_, image_descriptor_):
8181
@pytest.fixture
8282
def Image_(self, request, image_):
8383
Image_ = class_mock(request, 'docx.package.Image')
84-
Image_.load.return_value = image_
84+
Image_.from_file.return_value = image_
8585
return Image_
8686

8787
@pytest.fixture

0 commit comments

Comments
 (0)