@@ -160,10 +160,10 @@ def __init__(self, base_url=None, version=None,
160
160
base_url , timeout , pool_connections = num_pools ,
161
161
max_pool_size = max_pool_size
162
162
)
163
- except NameError :
163
+ except NameError as err :
164
164
raise DockerException (
165
165
'Install pypiwin32 package to enable npipe:// support'
166
- )
166
+ ) from err
167
167
self .mount ('http+docker://' , self ._custom_adapter )
168
168
self .base_url = 'http+docker://localnpipe'
169
169
elif base_url .startswith ('ssh://' ):
@@ -172,10 +172,10 @@ def __init__(self, base_url=None, version=None,
172
172
base_url , timeout , pool_connections = num_pools ,
173
173
max_pool_size = max_pool_size , shell_out = use_ssh_client
174
174
)
175
- except NameError :
175
+ except NameError as err :
176
176
raise DockerException (
177
177
'Install paramiko package to enable ssh:// support'
178
- )
178
+ ) from err
179
179
self .mount ('http+docker://ssh' , self ._custom_adapter )
180
180
self ._unmount ('http://' , 'https://' )
181
181
self .base_url = 'http+docker://ssh'
@@ -199,28 +199,27 @@ def __init__(self, base_url=None, version=None,
199
199
self ._version = version
200
200
if not isinstance (self ._version , str ):
201
201
raise DockerException (
202
- 'Version parameter must be a string or None. Found {}' .format (
203
- type (version ).__name__
204
- )
202
+ 'Version parameter must be a string or None. '
203
+ f'Found { type (version ).__name__ } '
205
204
)
206
205
if utils .version_lt (self ._version , MINIMUM_DOCKER_API_VERSION ):
207
206
raise InvalidVersion (
208
- 'API versions below {} are no longer supported by this '
209
- ' library.'. format ( MINIMUM_DOCKER_API_VERSION )
207
+ f 'API versions below { MINIMUM_DOCKER_API_VERSION } are '
208
+ f'no longer supported by this library.'
210
209
)
211
210
212
211
def _retrieve_server_version (self ):
213
212
try :
214
213
return self .version (api_version = False )["ApiVersion" ]
215
- except KeyError :
214
+ except KeyError as ke :
216
215
raise DockerException (
217
216
'Invalid response from docker daemon: key "ApiVersion"'
218
217
' is missing.'
219
- )
218
+ ) from ke
220
219
except Exception as e :
221
220
raise DockerException (
222
221
f'Error while fetching server API version: { e } '
223
- )
222
+ ) from e
224
223
225
224
def _set_request_timeout (self , kwargs ):
226
225
"""Prepare the kwargs for an HTTP request by inserting the timeout
@@ -248,19 +247,17 @@ def _url(self, pathfmt, *args, **kwargs):
248
247
for arg in args :
249
248
if not isinstance (arg , str ):
250
249
raise ValueError (
251
- 'Expected a string but found {} ({}) '
252
- 'instead' .format (arg , type (arg ))
250
+ f'Expected a string but found { arg } ({ type (arg )} ) instead'
253
251
)
254
252
255
253
quote_f = partial (urllib .parse .quote , safe = "/:" )
256
254
args = map (quote_f , args )
257
255
256
+ formatted_path = pathfmt .format (* args )
258
257
if kwargs .get ('versioned_api' , True ):
259
- return '{}/v{}{}' .format (
260
- self .base_url , self ._version , pathfmt .format (* args )
261
- )
258
+ return f'{ self .base_url } /v{ self ._version } { formatted_path } '
262
259
else :
263
- return f'{ self .base_url } { pathfmt . format ( * args ) } '
260
+ return f'{ self .base_url } { formatted_path } '
264
261
265
262
def _raise_for_status (self , response ):
266
263
"""Raises stored :class:`APIError`, if one occurred."""
@@ -479,7 +476,7 @@ def _get_result_tty(self, stream, res, is_tty):
479
476
return self ._multiplexed_response_stream_helper (res )
480
477
else :
481
478
return sep .join (
482
- [ x for x in self ._multiplexed_buffer_helper (res )]
479
+ list ( self ._multiplexed_buffer_helper (res ))
483
480
)
484
481
485
482
def _unmount (self , * args ):
0 commit comments