mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Python code modernization, 5/n (#1165)
* Address raise-missing-from. * Address simplifiable-if-expression. * Address unnecessary-dunder-call. * Address unnecessary-pass. * Address use-list-literal. * Address unused-variable. * Address use-dict-literal.
This commit is contained in:
@@ -167,8 +167,8 @@ class Connection(ConnectionBase):
|
||||
else:
|
||||
try:
|
||||
self.docker_cmd = get_bin_path("docker")
|
||||
except ValueError:
|
||||
raise AnsibleError("docker command not found in PATH")
|
||||
except ValueError as exc:
|
||||
raise AnsibleError("docker command not found in PATH") from exc
|
||||
|
||||
@staticmethod
|
||||
def _sanitize_version(version):
|
||||
@@ -426,7 +426,7 @@ class Connection(ConnectionBase):
|
||||
)
|
||||
|
||||
chunks = b""
|
||||
for key, event in events:
|
||||
for key, dummy_event in events:
|
||||
if key.fileobj == p.stdout:
|
||||
chunk = p.stdout.read()
|
||||
if chunk:
|
||||
@@ -523,10 +523,10 @@ class Connection(ConnectionBase):
|
||||
p = subprocess.Popen(
|
||||
args, stdin=in_file, stdout=subprocess.PIPE, stderr=subprocess.PIPE
|
||||
)
|
||||
except OSError:
|
||||
except OSError as exc:
|
||||
raise AnsibleError(
|
||||
"docker connection requires dd command in the container to put files"
|
||||
)
|
||||
) from exc
|
||||
stdout, stderr = p.communicate()
|
||||
|
||||
if p.returncode != 0:
|
||||
@@ -588,10 +588,10 @@ class Connection(ConnectionBase):
|
||||
stdout=out_file,
|
||||
stderr=subprocess.PIPE,
|
||||
)
|
||||
except OSError:
|
||||
except OSError as exc:
|
||||
raise AnsibleError(
|
||||
"docker connection requires dd command in the container to put files"
|
||||
)
|
||||
) from exc
|
||||
stdout, stderr = pp.communicate()
|
||||
|
||||
if pp.returncode != 0:
|
||||
|
||||
@@ -158,15 +158,15 @@ class Connection(ConnectionBase):
|
||||
if not_found_can_be_resource:
|
||||
raise AnsibleConnectionFailure(
|
||||
f'Could not find container "{remote_addr}" or resource in it ({e})'
|
||||
)
|
||||
) from e
|
||||
raise AnsibleConnectionFailure(
|
||||
f'Could not find container "{remote_addr}" ({e})'
|
||||
)
|
||||
) from e
|
||||
except APIError as e:
|
||||
if e.response is not None and e.response.status_code == 409:
|
||||
raise AnsibleConnectionFailure(
|
||||
f'The container "{remote_addr}" has been paused ({e})'
|
||||
)
|
||||
) from e
|
||||
self.client.fail(
|
||||
f'An unexpected Docker error occurred for container "{remote_addr}": {e}'
|
||||
)
|
||||
@@ -183,7 +183,7 @@ class Connection(ConnectionBase):
|
||||
super().__init__(play_context, new_stdin, *args, **kwargs)
|
||||
|
||||
self.client = None
|
||||
self.ids = dict()
|
||||
self.ids = {}
|
||||
|
||||
# Windows uses Powershell modules
|
||||
if getattr(self._shell, "_IS_WINDOWS", False):
|
||||
@@ -239,7 +239,7 @@ class Connection(ConnectionBase):
|
||||
host=self.get_option("remote_addr"),
|
||||
)
|
||||
|
||||
need_stdin = True if (in_data is not None) or do_become else False
|
||||
need_stdin = bool((in_data is not None) or do_become)
|
||||
|
||||
data = {
|
||||
"Container": self.get_option("remote_addr"),
|
||||
@@ -393,7 +393,7 @@ class Connection(ConnectionBase):
|
||||
except Exception as e:
|
||||
raise AnsibleConnectionFailure(
|
||||
f'Error while determining user and group ID of current user in container "{remote_addr}": {e}\nGot value: {ids!r}'
|
||||
)
|
||||
) from e
|
||||
|
||||
user_id, group_id = self.ids[self.actual_user]
|
||||
try:
|
||||
@@ -411,9 +411,9 @@ class Connection(ConnectionBase):
|
||||
not_found_can_be_resource=True,
|
||||
)
|
||||
except DockerFileNotFound as exc:
|
||||
raise AnsibleFileNotFound(to_native(exc))
|
||||
raise AnsibleFileNotFound(to_native(exc)) from exc
|
||||
except DockerFileCopyError as exc:
|
||||
raise AnsibleConnectionFailure(to_native(exc))
|
||||
raise AnsibleConnectionFailure(to_native(exc)) from exc
|
||||
|
||||
def fetch_file(self, in_path, out_path):
|
||||
"""Fetch a file from container to local."""
|
||||
@@ -439,9 +439,9 @@ class Connection(ConnectionBase):
|
||||
not_found_can_be_resource=True,
|
||||
)
|
||||
except DockerFileNotFound as exc:
|
||||
raise AnsibleFileNotFound(to_native(exc))
|
||||
raise AnsibleFileNotFound(to_native(exc)) from exc
|
||||
except DockerFileCopyError as exc:
|
||||
raise AnsibleConnectionFailure(to_native(exc))
|
||||
raise AnsibleConnectionFailure(to_native(exc)) from exc
|
||||
|
||||
def close(self):
|
||||
"""Terminate the connection. Nothing to do for Docker"""
|
||||
|
||||
@@ -178,7 +178,7 @@ class Connection(ConnectionBase):
|
||||
)
|
||||
|
||||
chunks = b""
|
||||
for key, event in events:
|
||||
for key, dummy_event in events:
|
||||
if key.fileobj == p.stdout:
|
||||
chunk = p.stdout.read()
|
||||
if chunk:
|
||||
@@ -244,11 +244,13 @@ class Connection(ConnectionBase):
|
||||
try:
|
||||
with open(to_bytes(in_path, errors="surrogate_or_strict"), "rb") as in_file:
|
||||
in_data = in_file.read()
|
||||
rc, out, err = self.exec_command(cmd=["tee", out_path], in_data=in_data)
|
||||
rc, dummy_out, err = self.exec_command(
|
||||
cmd=["tee", out_path], in_data=in_data
|
||||
)
|
||||
if rc != 0:
|
||||
raise AnsibleError(f"failed to transfer file to {out_path}: {err}")
|
||||
except IOError as e:
|
||||
raise AnsibleError(f"failed to transfer file to {out_path}: {e}")
|
||||
raise AnsibleError(f"failed to transfer file to {out_path}: {e}") from e
|
||||
|
||||
def fetch_file(self, in_path, out_path):
|
||||
super().fetch_file(in_path, out_path)
|
||||
@@ -268,7 +270,9 @@ class Connection(ConnectionBase):
|
||||
) as out_file:
|
||||
out_file.write(out)
|
||||
except IOError as e:
|
||||
raise AnsibleError(f"failed to transfer file to {to_native(out_path)}: {e}")
|
||||
raise AnsibleError(
|
||||
f"failed to transfer file to {to_native(out_path)}: {e}"
|
||||
) from e
|
||||
|
||||
def close(self):
|
||||
"""terminate the connection; nothing to do here"""
|
||||
|
||||
Reference in New Issue
Block a user