Skip to content

Commit dbd7236

Browse files
committed
Add container.get() which removes the duplication of container.inspect() in every property, and provides a nicer interface for querying container data.
Signed-off-by: Daniel Nephin <dnephin@gmail.com>
1 parent 6b221d5 commit dbd7236

2 files changed

Lines changed: 31 additions & 17 deletions

File tree

fig/container.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def number(self):
6767
@property
6868
def ports(self):
6969
self.inspect_if_not_inspected()
70-
return self.dictionary['NetworkSettings']['Ports'] or {}
70+
return self.get('NetworkSettings.Ports') or {}
7171

7272
@property
7373
def human_readable_ports(self):
@@ -82,33 +82,35 @@ def format_port(private, public):
8282

8383
@property
8484
def human_readable_state(self):
85-
self.inspect_if_not_inspected()
86-
if self.dictionary['State']['Running']:
87-
if self.dictionary['State'].get('Ghost'):
88-
return 'Ghost'
89-
else:
90-
return 'Up'
85+
if self.is_running:
86+
return 'Ghost' if self.get('State.Ghost') else 'Up'
9187
else:
92-
return 'Exit %s' % self.dictionary['State']['ExitCode']
88+
return 'Exit %s' % self.get('State.ExitCode')
9389

9490
@property
9591
def human_readable_command(self):
96-
self.inspect_if_not_inspected()
97-
if self.dictionary['Config']['Cmd']:
98-
return ' '.join(self.dictionary['Config']['Cmd'])
99-
else:
100-
return ''
92+
return ' '.join(self.get('Config.Cmd') or '')
10193

10294
@property
10395
def environment(self):
104-
self.inspect_if_not_inspected()
105-
return dict(var.split("=", 1)
106-
for var in self.dictionary.get('Config', {}).get('Env', []))
96+
return dict(var.split("=", 1) for var in self.get('Config.Env') or [])
10797

10898
@property
10999
def is_running(self):
100+
return self.get('State.Running')
101+
102+
def get(self, key):
103+
"""Return a value from the container or None if the value is not set.
104+
105+
:param key: a string using dotted notation for nested dictionary
106+
lookups
107+
"""
110108
self.inspect_if_not_inspected()
111-
return self.dictionary['State']['Running']
109+
110+
def get_value(dictionary, key):
111+
return (dictionary or {}).get(key)
112+
113+
return reduce(get_value, key.split('.'), self.dictionary)
112114

113115
def get_local_port(self, port, protocol='tcp'):
114116
port = self.ports.get("%s/%s" % (port, protocol))

tests/unit/container_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,15 @@ def test_get_local_port(self):
105105
self.assertEqual(
106106
container.get_local_port(45454, protocol='tcp'),
107107
'0.0.0.0:49197')
108+
109+
def test_get(self):
110+
container = Container(None, {
111+
"Status":"Up 8 seconds",
112+
"HostConfig": {
113+
"VolumesFrom": ["volume_id",]
114+
},
115+
}, has_been_inspected=True)
116+
117+
self.assertEqual(container.get('Status'), "Up 8 seconds")
118+
self.assertEqual(container.get('HostConfig.VolumesFrom'), ["volume_id",])
119+
self.assertEqual(container.get('Foo.Bar.DoesNotExist'), None)

0 commit comments

Comments
 (0)