Skip to content

Commit deb8222

Browse files
authored
Merge pull request #1873 from docker/hongbin-image-load
Return Image objects in ImageCollection.load
2 parents 84d4545 + 8b5a52a commit deb8222

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

docker/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ class BuildError(Exception):
144144
pass
145145

146146

147+
class ImageLoadError(DockerException):
148+
pass
149+
150+
147151
def create_unexpected_kwargs_error(name, kwargs):
148152
quoted_kwargs = ["'{}'".format(k) for k in sorted(kwargs)]
149153
text = ["{}() ".format(name)]

docker/models/images.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import six
44

55
from ..api import APIClient
6-
from ..errors import BuildError
6+
from ..errors import BuildError, ImageLoadError
77
from ..utils.json_stream import json_stream
88
from .resource import Collection, Model
99

@@ -241,13 +241,27 @@ def load(self, data):
241241
data (binary): Image data to be loaded.
242242
243243
Returns:
244-
(generator): Progress output as JSON objects
244+
(list of :py:class:`Image`): The images.
245245
246246
Raises:
247247
:py:class:`docker.errors.APIError`
248248
If the server returns an error.
249249
"""
250-
return self.client.api.load_image(data)
250+
resp = self.client.api.load_image(data)
251+
images = []
252+
for chunk in resp:
253+
if 'stream' in chunk:
254+
match = re.search(
255+
r'(^Loaded image ID: |^Loaded image: )(.+)$',
256+
chunk['stream']
257+
)
258+
if match:
259+
image_id = match.group(2)
260+
images.append(image_id)
261+
if 'error' in chunk:
262+
raise ImageLoadError(chunk['error'])
263+
264+
return [self.get(i) for i in images]
251265

252266
def pull(self, name, tag=None, **kwargs):
253267
"""

tests/integration/models_images_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def test_pull_with_tag(self):
7171
image = client.images.pull('alpine', tag='3.3')
7272
assert 'alpine:3.3' in image.attrs['RepoTags']
7373

74+
def test_load_error(self):
75+
client = docker.from_env(version=TEST_API_VERSION)
76+
with pytest.raises(docker.errors.ImageLoadError):
77+
client.images.load('abc')
78+
7479

7580
class ImageTest(BaseIntegrationTest):
7681

0 commit comments

Comments
 (0)