Skip to content

Commit d19572a

Browse files
authored
Merge pull request #1493 from funkyfuture/model_properties
Adds a few model properties
2 parents dec80d9 + 9536c86 commit d19572a

File tree

10 files changed

+63
-18
lines changed

10 files changed

+63
-18
lines changed

.dockerignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ html/*
1313
__pycache__
1414

1515
# Compiled Documentation
16-
site/
17-
Makefile
16+
docs/_build

Dockerfile-docs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
FROM python:3.5
22

3-
RUN mkdir /src
4-
WORKDIR /src
3+
ARG uid=1000
4+
ARG gid=1000
55

6-
COPY requirements.txt /src/requirements.txt
7-
RUN pip install -r requirements.txt
6+
RUN addgroup --gid $gid sphinx \
7+
&& useradd --uid $uid --gid $gid -M sphinx
88

9-
COPY docs-requirements.txt /src/docs-requirements.txt
10-
RUN pip install -r docs-requirements.txt
9+
WORKDIR /src
10+
COPY requirements.txt docs-requirements.txt ./
11+
RUN pip install -r requirements.txt -r docs-requirements.txt
1112

12-
COPY . /src
13+
USER sphinx

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ build-py3:
1616

1717
.PHONY: build-docs
1818
build-docs:
19-
docker build -t docker-sdk-python-docs -f Dockerfile-docs .
19+
docker build -t docker-sdk-python-docs -f Dockerfile-docs --build-arg uid=$(shell id -u) --build-arg gid=$(shell id -g) .
2020

2121
.PHONY: build-dind-certs
2222
build-dind-certs:
@@ -77,7 +77,7 @@ flake8: build
7777

7878
.PHONY: docs
7979
docs: build-docs
80-
docker run --rm -it -v `pwd`:/code docker-sdk-python-docs sphinx-build docs ./_build
80+
docker run --rm -it -v `pwd`:/src docker-sdk-python-docs sphinx-build docs docs/_build
8181

8282
.PHONY: shell
8383
shell: build

docker/models/containers.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,24 @@ def name(self):
1818
if self.attrs.get('Name') is not None:
1919
return self.attrs['Name'].lstrip('/')
2020

21+
@property
22+
def image(self):
23+
"""
24+
The image of the container.
25+
"""
26+
image_id = self.attrs['Image']
27+
if image_id is None:
28+
return None
29+
return self.client.images.get(image_id.split(':')[1])
30+
31+
@property
32+
def labels(self):
33+
"""
34+
The labels of a container as dictionary.
35+
"""
36+
result = self.attrs['Config'].get('Labels')
37+
return result or {}
38+
2139
@property
2240
def status(self):
2341
"""

docker/models/images.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ class Image(Model):
1515
def __repr__(self):
1616
return "<%s: '%s'>" % (self.__class__.__name__, "', '".join(self.tags))
1717

18+
@property
19+
def labels(self):
20+
"""
21+
The labels of an image as dictionary.
22+
"""
23+
result = self.attrs['Config'].get('Labels')
24+
return result or {}
25+
1826
@property
1927
def short_id(self):
2028
"""

docs/containers.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ Container objects
2121

2222
.. autoclass:: Container()
2323

24+
.. py:attribute:: attrs
2425
.. autoattribute:: id
25-
.. autoattribute:: short_id
26+
.. autoattribute:: image
27+
.. autoattribute:: labels
2628
.. autoattribute:: name
29+
.. autoattribute:: short_id
2730
.. autoattribute:: status
28-
.. py:attribute:: attrs
2931

3032
The raw representation of this object from the server.
3133

docs/images.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ Image objects
2626

2727
.. autoclass:: Image()
2828

29-
.. autoattribute:: id
30-
.. autoattribute:: short_id
31-
.. autoattribute:: tags
32-
.. py:attribute:: attrs
29+
.. py:attribute:: attrs
30+
.. autoattribute:: id
31+
.. autoattribute:: labels
32+
.. autoattribute:: short_id
33+
.. autoattribute:: tags
3334

3435
The raw representation of this object from the server.
3536

tests/unit/fake_api.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def get_fake_inspect_container(tty=False):
134134
status_code = 200
135135
response = {
136136
'Id': FAKE_CONTAINER_ID,
137-
'Config': {'Privileged': True, 'Tty': tty},
137+
'Config': {'Labels': {'foo': 'bar'}, 'Privileged': True, 'Tty': tty},
138138
'ID': FAKE_CONTAINER_ID,
139139
'Image': 'busybox:latest',
140140
'Name': 'foobar',
@@ -158,6 +158,7 @@ def get_fake_inspect_image():
158158
'Parent': "27cf784147099545",
159159
'Created': "2013-03-23T22:24:18.818426-07:00",
160160
'Container': FAKE_CONTAINER_ID,
161+
'Config': {'Labels': {'bar': 'foo'}},
161162
'ContainerConfig':
162163
{
163164
"Hostname": "",

tests/unit/models_containers_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,22 @@ def test_get_archive(self):
384384
container.get_archive('foo')
385385
client.api.get_archive.assert_called_with(FAKE_CONTAINER_ID, 'foo')
386386

387+
def test_image(self):
388+
client = make_fake_client()
389+
container = client.containers.get(FAKE_CONTAINER_ID)
390+
assert container.image.id == FAKE_IMAGE_ID
391+
387392
def test_kill(self):
388393
client = make_fake_client()
389394
container = client.containers.get(FAKE_CONTAINER_ID)
390395
container.kill(signal=5)
391396
client.api.kill.assert_called_with(FAKE_CONTAINER_ID, signal=5)
392397

398+
def test_labels(self):
399+
client = make_fake_client()
400+
container = client.containers.get(FAKE_CONTAINER_ID)
401+
assert container.labels == {'foo': 'bar'}
402+
393403
def test_logs(self):
394404
client = make_fake_client()
395405
container = client.containers.get(FAKE_CONTAINER_ID)

tests/unit/models_images_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ def test_get(self):
2121
assert isinstance(image, Image)
2222
assert image.id == FAKE_IMAGE_ID
2323

24+
def test_labels(self):
25+
client = make_fake_client()
26+
image = client.images.get(FAKE_IMAGE_ID)
27+
assert image.labels == {'bar': 'foo'}
28+
2429
def test_list(self):
2530
client = make_fake_client()
2631
images = client.images.list(all=True)

0 commit comments

Comments
 (0)