mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Python code modernization, 8/n (#1179)
* Use to_text instead of to_native. * Remove no longer needed pylint ignores. * Remove another pylint ignore. * Remove no longer needed ignore. * Address redefined-outer-name. * Address consider-using-with.
This commit is contained in:
@@ -171,7 +171,7 @@ import stat
|
||||
import traceback
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_bytes, to_native, to_text
|
||||
from ansible.module_utils.common.text.converters import to_bytes, to_text
|
||||
from ansible.module_utils.common.validation import check_type_int
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.errors import (
|
||||
@@ -1041,7 +1041,7 @@ def copy_content_into_container(
|
||||
|
||||
def parse_modern(mode: str | int) -> int:
|
||||
if isinstance(mode, str):
|
||||
return int(to_native(mode), 8)
|
||||
return int(to_text(mode), 8)
|
||||
if isinstance(mode, int):
|
||||
return mode
|
||||
raise TypeError(f"must be an octal string or an integer, got {mode!r}")
|
||||
@@ -1049,7 +1049,7 @@ def parse_modern(mode: str | int) -> int:
|
||||
|
||||
def parse_octal_string_only(mode: str) -> int:
|
||||
if isinstance(mode, str):
|
||||
return int(to_native(mode), 8)
|
||||
return int(to_text(mode), 8)
|
||||
raise TypeError(f"must be an octal string, got {mode!r}")
|
||||
|
||||
|
||||
@@ -1185,7 +1185,7 @@ def main() -> None:
|
||||
except DockerUnexpectedError as exc:
|
||||
client.fail(f"Unexpected error: {exc}", exception=traceback.format_exc())
|
||||
except DockerFileCopyError as exc:
|
||||
client.fail(to_native(exc))
|
||||
client.fail(to_text(exc))
|
||||
except OSError as exc:
|
||||
client.fail(f"Unexpected error: {exc}", exception=traceback.format_exc())
|
||||
|
||||
|
||||
@@ -369,7 +369,7 @@ import os
|
||||
import traceback
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
from ansible.module_utils.common.text.formatters import human_to_bytes
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.auth import (
|
||||
@@ -899,7 +899,7 @@ class ImageManager(DockerBaseClass):
|
||||
buildargs = {}
|
||||
if self.buildargs:
|
||||
for key, value in self.buildargs.items():
|
||||
buildargs[key] = to_native(value)
|
||||
buildargs[key] = to_text(value)
|
||||
|
||||
container_limits = self.container_limits or {}
|
||||
for key in container_limits.keys():
|
||||
|
||||
@@ -284,7 +284,7 @@ import os
|
||||
import traceback
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
from ansible.module_utils.common.text.formatters import human_to_bytes
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.utils.utils import (
|
||||
@@ -545,12 +545,12 @@ class ImageBuilder(DockerBaseClass):
|
||||
if rc != 0:
|
||||
self.fail(
|
||||
f"Building {self.name}:{self.tag} failed",
|
||||
stdout=to_native(stdout),
|
||||
stderr=to_native(stderr),
|
||||
stdout=to_text(stdout),
|
||||
stderr=to_text(stderr),
|
||||
command=args,
|
||||
)
|
||||
results["stdout"] = to_native(stdout)
|
||||
results["stderr"] = to_native(stderr)
|
||||
results["stdout"] = to_text(stdout)
|
||||
results["stderr"] = to_text(stderr)
|
||||
results["image"] = self.client.find_image(self.name, self.tag) or {}
|
||||
results["command"] = args
|
||||
|
||||
|
||||
@@ -286,7 +286,7 @@ import time
|
||||
import traceback
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.errors import (
|
||||
DockerException,
|
||||
@@ -412,7 +412,7 @@ class DockerNetworkManager:
|
||||
for ipam_config in self.parameters.ipam_config:
|
||||
validate_cidr(ipam_config["subnet"])
|
||||
except ValueError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
|
||||
if self.parameters.driver_options:
|
||||
self.parameters.driver_options = clean_dict_booleans_for_docker_api(
|
||||
|
||||
@@ -143,7 +143,7 @@ except ImportError:
|
||||
# missing Docker SDK for Python handled in ansible.module_utils.docker.common
|
||||
pass
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._common import (
|
||||
RequestException,
|
||||
@@ -237,11 +237,12 @@ class SwarmNodeManager(DockerBaseClass):
|
||||
node_spec["Labels"] = self.parameters.labels
|
||||
changed = True
|
||||
elif self.parameters.labels_state == "merge":
|
||||
node_spec["Labels"] = dict(node_info["Spec"]["Labels"] or {})
|
||||
labels: dict[str, str] = dict(node_info["Spec"]["Labels"] or {})
|
||||
node_spec["Labels"] = labels
|
||||
if self.parameters.labels is not None:
|
||||
for key, value in self.parameters.labels.items():
|
||||
if node_spec["Labels"].get(key) != value:
|
||||
node_spec["Labels"][key] = value
|
||||
if labels.get(key) != value:
|
||||
labels[key] = value
|
||||
changed = True
|
||||
|
||||
if self.parameters.labels_to_remove is not None:
|
||||
@@ -253,7 +254,7 @@ class SwarmNodeManager(DockerBaseClass):
|
||||
changed = True
|
||||
else:
|
||||
self.client.module.warn(
|
||||
f"Label '{to_native(key)}' listed both in 'labels' and 'labels_to_remove'. "
|
||||
f"Label '{to_text(key)}' listed both in 'labels' and 'labels_to_remove'. "
|
||||
"Keeping the assigned label value."
|
||||
)
|
||||
else:
|
||||
|
||||
@@ -131,7 +131,7 @@ actions:
|
||||
import traceback
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api import auth
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.errors import (
|
||||
@@ -215,7 +215,7 @@ class DockerPluginManager:
|
||||
except NotFound:
|
||||
return None
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
|
||||
def has_different_config(self) -> DifferenceTracker:
|
||||
"""
|
||||
@@ -293,7 +293,7 @@ class DockerPluginManager:
|
||||
"/plugins/{0}/set", self.preferred_name, data=data
|
||||
)
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
|
||||
self.actions.append(f"Installed plugin {self.preferred_name}")
|
||||
self.changed = True
|
||||
@@ -307,7 +307,7 @@ class DockerPluginManager:
|
||||
"/plugins/{0}", self.preferred_name, params={"force": force}
|
||||
)
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
|
||||
self.actions.append(f"Removed plugin {self.preferred_name}")
|
||||
self.changed = True
|
||||
@@ -323,7 +323,7 @@ class DockerPluginManager:
|
||||
"/plugins/{0}/set", self.preferred_name, data=data
|
||||
)
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
self.actions.append(f"Updated plugin {self.preferred_name} settings")
|
||||
self.changed = True
|
||||
else:
|
||||
@@ -361,7 +361,7 @@ class DockerPluginManager:
|
||||
params={"timeout": timeout},
|
||||
)
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
self.actions.append(f"Enabled plugin {self.preferred_name}")
|
||||
self.changed = True
|
||||
else:
|
||||
@@ -374,7 +374,7 @@ class DockerPluginManager:
|
||||
params={"timeout": timeout},
|
||||
)
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
self.actions.append(f"Enabled plugin {self.preferred_name}")
|
||||
self.changed = True
|
||||
|
||||
@@ -387,7 +387,7 @@ class DockerPluginManager:
|
||||
"/plugins/{0}/disable", self.preferred_name
|
||||
)
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
self.actions.append(f"Disable plugin {self.preferred_name}")
|
||||
self.changed = True
|
||||
else:
|
||||
|
||||
@@ -161,7 +161,7 @@ import traceback
|
||||
import typing as t
|
||||
from time import sleep
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._common_cli import (
|
||||
AnsibleModuleDockerClient,
|
||||
@@ -190,9 +190,9 @@ def docker_stack_services(
|
||||
dummy_rc, out, err = client.call_cli(
|
||||
"stack", "services", stack_name, "--format", "{{.Name}}"
|
||||
)
|
||||
if to_native(err) == f"Nothing found in stack: {stack_name}\n":
|
||||
if to_text(err) == f"Nothing found in stack: {stack_name}\n":
|
||||
return []
|
||||
return to_native(out).strip().split("\n")
|
||||
return to_text(out).strip().split("\n")
|
||||
|
||||
|
||||
def docker_service_inspect(
|
||||
@@ -221,7 +221,7 @@ def docker_stack_deploy(
|
||||
command += ["--compose-file", compose_file]
|
||||
command += [stack_name]
|
||||
rc, out, err = client.call_cli(*command)
|
||||
return rc, to_native(out), to_native(err)
|
||||
return rc, to_text(out), to_text(err)
|
||||
|
||||
|
||||
def docker_stack_inspect(
|
||||
@@ -244,11 +244,11 @@ def docker_stack_rm(
|
||||
command += ["--detach=false"]
|
||||
rc, out, err = client.call_cli(*command)
|
||||
|
||||
while to_native(err) != f"Nothing found in stack: {stack_name}\n" and retries > 0:
|
||||
while to_text(err) != f"Nothing found in stack: {stack_name}\n" and retries > 0:
|
||||
sleep(interval)
|
||||
retries = retries - 1
|
||||
rc, out, err = client.call_cli(*command)
|
||||
return rc, to_native(out), to_native(err)
|
||||
return rc, to_text(out), to_text(err)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
||||
@@ -77,7 +77,7 @@ EXAMPLES = r"""
|
||||
import json
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._common_cli import (
|
||||
AnsibleModuleDockerClient,
|
||||
@@ -99,7 +99,7 @@ def main() -> None:
|
||||
changed=False,
|
||||
rc=rc,
|
||||
stdout="\n".join([json.dumps(entry) for entry in ret]),
|
||||
stderr=to_native(stderr).strip(),
|
||||
stderr=to_text(stderr).strip(),
|
||||
results=ret,
|
||||
)
|
||||
except DockerException as e:
|
||||
|
||||
@@ -85,7 +85,7 @@ EXAMPLES = r"""
|
||||
import json
|
||||
import traceback
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._common_cli import (
|
||||
AnsibleModuleDockerClient,
|
||||
@@ -108,7 +108,7 @@ def main() -> None:
|
||||
changed=False,
|
||||
rc=rc,
|
||||
stdout="\n".join([json.dumps(entry) for entry in ret]),
|
||||
stderr=to_native(stderr).strip(),
|
||||
stderr=to_text(stderr).strip(),
|
||||
results=ret,
|
||||
)
|
||||
except DockerException as e:
|
||||
|
||||
@@ -120,7 +120,7 @@ volume:
|
||||
import traceback
|
||||
import typing as t
|
||||
|
||||
from ansible.module_utils.common.text.converters import to_native
|
||||
from ansible.module_utils.common.text.converters import to_text
|
||||
|
||||
from ansible_collections.community.docker.plugins.module_utils._api.errors import (
|
||||
APIError,
|
||||
@@ -185,7 +185,7 @@ class DockerVolumeManager:
|
||||
try:
|
||||
volumes = self.client.get_json("/volumes")
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
|
||||
if volumes["Volumes"] is None:
|
||||
return None
|
||||
@@ -259,7 +259,7 @@ class DockerVolumeManager:
|
||||
"/volumes/{0}", resp["Name"]
|
||||
)
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
|
||||
self.actions.append(
|
||||
f"Created volume {self.parameters.volume_name} with driver {self.parameters.driver}"
|
||||
@@ -272,7 +272,7 @@ class DockerVolumeManager:
|
||||
try:
|
||||
self.client.delete_call("/volumes/{0}", self.parameters.volume_name)
|
||||
except APIError as e:
|
||||
self.client.fail(to_native(e))
|
||||
self.client.fail(to_text(e))
|
||||
|
||||
self.actions.append(f"Removed volume {self.parameters.volume_name}")
|
||||
self.results["changed"] = True
|
||||
|
||||
Reference in New Issue
Block a user