@@ -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 ))
0 commit comments