Python code modernization, 4/n (#1162)

* Address attribute-defined-outside-init.

* Address broad-exception-raised.

* Address broad-exception-caught.

* Address consider-iterating-dictionary.

* Address consider-using-dict-comprehension.

* Address consider-using-f-string.

* Address consider-using-in.

* Address consider-using-max-builtin.

* Address some consider-using-with.

* Address invalid-name.

* Address keyword-arg-before-vararg.

* Address line-too-long.

* Address no-else-continue.

* Address no-else-raise.

* Address no-else-return.

* Remove broken dead code.

* Make consider-using-f-string changes compatible with older Python versions.

* Python 3.11 and earlier apparently do not like multi-line f-strings.
This commit is contained in:
Felix Fontein
2025-10-11 23:06:50 +02:00
committed by GitHub
parent 33c8a49191
commit cad22de628
59 changed files with 556 additions and 630 deletions
+1 -1
View File
@@ -243,7 +243,7 @@ class ConfigManager(DockerBaseClass):
try:
with open(data_src, "rb") as f:
self.data = f.read()
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.client.fail(f"Error while reading {data_src}: {exc}")
self.labels = parameters.get("labels")
self.force = parameters.get("force")
+8 -10
View File
@@ -343,31 +343,31 @@ def retrieve_diff(
diff["before_header"] = container_path
diff["before"] = "(directory)"
return
elif regular_stat["mode"] & (1 << (32 - 4)) != 0:
if regular_stat["mode"] & (1 << (32 - 4)) != 0:
diff["before_header"] = container_path
diff["before"] = "(temporary file)"
return
elif regular_stat["mode"] & (1 << (32 - 5)) != 0:
if regular_stat["mode"] & (1 << (32 - 5)) != 0:
diff["before_header"] = container_path
diff["before"] = link_target
return
elif regular_stat["mode"] & (1 << (32 - 6)) != 0:
if regular_stat["mode"] & (1 << (32 - 6)) != 0:
diff["before_header"] = container_path
diff["before"] = "(device)"
return
elif regular_stat["mode"] & (1 << (32 - 7)) != 0:
if regular_stat["mode"] & (1 << (32 - 7)) != 0:
diff["before_header"] = container_path
diff["before"] = "(named pipe)"
return
elif regular_stat["mode"] & (1 << (32 - 8)) != 0:
if regular_stat["mode"] & (1 << (32 - 8)) != 0:
diff["before_header"] = container_path
diff["before"] = "(socket)"
return
elif regular_stat["mode"] & (1 << (32 - 11)) != 0:
if regular_stat["mode"] & (1 << (32 - 11)) != 0:
diff["before_header"] = container_path
diff["before"] = "(character device)"
return
elif regular_stat["mode"] & (1 << (32 - 13)) != 0:
if regular_stat["mode"] & (1 << (32 - 13)) != 0:
diff["before_header"] = container_path
diff["before"] = "(unknown filesystem object)"
return
@@ -1084,9 +1084,7 @@ def main():
if client.module.params["content_is_b64"]:
try:
content = base64.b64decode(content)
except (
Exception
) as e: # depending on Python version and error, multiple different exceptions can be raised
except Exception as e: # pylint: disable=broad-exception-caught
client.fail(f"Cannot Base64 decode the content option: {e}")
else:
content = to_bytes(content)
+1 -2
View File
@@ -271,8 +271,7 @@ class DockerHostManager(DockerBaseClass):
try:
if self.verbose_output:
return self.client.df()
else:
return dict(LayersSize=self.client.df()["LayersSize"])
return dict(LayersSize=self.client.df()["LayersSize"])
except APIError as exc:
self.client.fail(f"Error inspecting docker host: {exc}")
+16 -18
View File
@@ -618,7 +618,7 @@ class ImageManager(DockerBaseClass):
except NotFound:
# If the image vanished while we were trying to remove it, do not fail
pass
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error removing image {name} - {exc}")
self.results["changed"] = True
@@ -656,17 +656,16 @@ class ImageManager(DockerBaseClass):
if archived is None:
return build_msg("since none present")
elif (
if (
current_image_id == api_image_id(archived.image_id)
and [current_image_name] == archived.repo_tags
):
return None
else:
name = ", ".join(archived.repo_tags)
name = ", ".join(archived.repo_tags)
return build_msg(
f"overwriting archive with image {archived.image_id} named {name}"
)
return build_msg(
f"overwriting archive with image {archived.image_id} named {name}"
)
def archive_image(self, name, tag):
"""
@@ -714,14 +713,14 @@ class ImageManager(DockerBaseClass):
DEFAULT_DATA_CHUNK_SIZE,
False,
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error getting image {image_name} - {exc}")
try:
with open(self.archive_path, "wb") as fd:
for chunk in saved_image:
fd.write(chunk)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error writing image archive {self.archive_path} - {exc}")
self.results["image"] = image
@@ -779,12 +778,12 @@ class ImageManager(DockerBaseClass):
for line in self.client._stream_helper(response, decode=True):
self.log(line, pretty_print=True)
if line.get("errorDetail"):
raise Exception(line["errorDetail"]["message"])
raise RuntimeError(line["errorDetail"]["message"])
status = line.get("status")
if status == "Pushing":
changed = True
self.results["changed"] = changed
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
if "unauthorized" in str(exc):
if "authentication required" in str(exc):
self.fail(
@@ -842,8 +841,8 @@ class ImageManager(DockerBaseClass):
)
self.client._raise_for_status(res)
if res.status_code != 201:
raise Exception("Tag operation failed.")
except Exception as exc:
raise RuntimeError("Tag operation failed.")
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error: failed to tag image - {exc}")
self.results["image"] = self.client.find_image(name=repo, tag=repo_tag)
if image and image["Id"] == self.results["image"]["Id"]:
@@ -969,9 +968,9 @@ class ImageManager(DockerBaseClass):
if line.get("error"):
if line.get("errorDetail"):
errorDetail = line.get("errorDetail")
error_detail = line.get("errorDetail")
self.fail(
f"Error building {self.name} - code: {errorDetail.get('code')}, message: {errorDetail.get('message')}, logs: {build_output}"
f"Error building {self.name} - code: {error_detail.get('code')}, message: {error_detail.get('message')}, logs: {build_output}"
)
else:
self.fail(
@@ -1019,7 +1018,7 @@ class ImageManager(DockerBaseClass):
f"Error loading image {self.name} - {exc}",
stdout="\n".join(load_output),
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.client.fail(
f"Error loading image {self.name} - {exc}",
stdout="\n".join(load_output),
@@ -1076,8 +1075,7 @@ class ImageManager(DockerBaseClass):
if is_image_name_id(self.name):
return self.client.find_image_by_id(self.name, accept_missing_image=True)
else:
return self.client.find_image(self.name, self.tag)
return self.client.find_image(self.name, self.tag)
def main():
+3 -3
View File
@@ -189,7 +189,7 @@ class ImageExportManager(DockerBaseClass):
with open(self.path, "wb") as fd:
for chunk in chunks:
fd.write(chunk)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error writing image archive {self.path} - {exc}")
def export_images(self):
@@ -205,7 +205,7 @@ class ImageExportManager(DockerBaseClass):
DEFAULT_DATA_CHUNK_SIZE,
False,
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error getting image {image_names[0]} - {exc}")
else:
self.log(f"Getting archive of images {image_names_str}")
@@ -219,7 +219,7 @@ class ImageExportManager(DockerBaseClass):
DEFAULT_DATA_CHUNK_SIZE,
False,
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error getting images {image_names_str} - {exc}")
self.write_chunks(chunks)
+1 -1
View File
@@ -212,7 +212,7 @@ class ImageManager(DockerBaseClass):
inspection = self.client.get_json("/images/{0}/json", image["Id"])
except NotFound:
inspection = None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error inspecting image {image['Id']} - {exc}")
results.append(inspection)
return results
+1 -1
View File
@@ -141,7 +141,7 @@ class ImageManager(DockerBaseClass):
f"Error loading archive {self.path} - {exc}",
stdout="\n".join(load_output),
)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.client.fail(
f"Error loading archive {self.path} - {exc}",
stdout="\n".join(load_output),
+2 -2
View File
@@ -155,11 +155,11 @@ class ImagePusher(DockerBaseClass):
for line in self.client._stream_helper(response, decode=True):
self.log(line, pretty_print=True)
if line.get("errorDetail"):
raise Exception(line["errorDetail"]["message"])
raise RuntimeError(line["errorDetail"]["message"])
status = line.get("status")
if status == "Pushing":
results["changed"] = True
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
if "unauthorized" in str(exc):
if "authentication required" in str(exc):
self.client.fail(
+1 -1
View File
@@ -194,7 +194,7 @@ class ImageRemover(DockerBaseClass):
except NotFound:
# If the image vanished while we were trying to remove it, do not fail
res = []
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error removing image {name} - {exc}")
for entry in res:
+2 -2
View File
@@ -214,8 +214,8 @@ class ImageTagger(DockerBaseClass):
)
self.client._raise_for_status(res)
if res.status_code != 201:
raise Exception("Tag operation failed.")
except Exception as exc:
raise RuntimeError("Tag operation failed.")
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(f"Error: failed to tag image as {name}:{tag} - {exc}")
return True, msg, tagged_image
+2 -2
View File
@@ -309,7 +309,7 @@ class LoginManager(DockerBaseClass):
self.log(f"Log into {self.registry_url} with username {self.username}")
try:
response = self._login(self.reauthorize)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Logging into {self.registry_url} for user {self.username} failed - {exc}"
)
@@ -322,7 +322,7 @@ class LoginManager(DockerBaseClass):
if not self.reauthorize and response["password"] != self.password:
try:
response = self._login(True)
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.fail(
f"Logging into {self.registry_url} for user {self.username} failed - {exc}"
)
+1 -1
View File
@@ -359,7 +359,7 @@ def validate_cidr(cidr):
"""
if CIDR_IPV4.match(cidr):
return "ipv4"
elif CIDR_IPV6.match(cidr):
if CIDR_IPV6.match(cidr):
return "ipv6"
raise ValueError(f'"{cidr}" is not a valid CIDR')
+1 -1
View File
@@ -235,7 +235,7 @@ class SecretManager(DockerBaseClass):
try:
with open(data_src, "rb") as f:
self.data = f.read()
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
self.client.fail(f"Error while reading {data_src}: {exc}")
self.labels = parameters.get("labels")
self.force = parameters.get("force")
+2 -3
View File
@@ -196,9 +196,8 @@ def docker_service_inspect(client, service_name):
rc, out, err = client.call_cli("service", "inspect", service_name)
if rc != 0:
return None
else:
ret = json.loads(out)[0]["Spec"]
return ret
ret = json.loads(out)[0]["Spec"]
return ret
def docker_stack_deploy(client, stack_name, compose_files):
+1
View File
@@ -323,6 +323,7 @@ class TaskParameters(DockerBaseClass):
self.join_token = None
self.data_path_addr = None
self.data_path_port = None
self.spec = None
# Spec
self.snapshot_interval = None
+15 -19
View File
@@ -932,8 +932,7 @@ def get_docker_environment(env, env_files):
if not env_list:
if env is not None or env_files is not None:
return []
else:
return None
return None
return sorted(env_list)
@@ -992,17 +991,16 @@ def get_docker_networks(networks, network_ids):
def get_nanoseconds_from_raw_option(name, value):
if value is None:
return None
elif isinstance(value, int):
if isinstance(value, int):
return value
elif isinstance(value, str):
if isinstance(value, str):
try:
return int(value)
except ValueError:
return convert_duration_to_nanosecond(value)
else:
raise ValueError(
f"Invalid type for {name} {value} ({type(value)}). Only string or int allowed."
)
raise ValueError(
f"Invalid type for {name} {value} ({type(value)}). Only string or int allowed."
)
def get_value(key, values, default=None):
@@ -1046,9 +1044,8 @@ def has_list_changed(new_list, old_list, sort_lists=True, sort_key=None):
if unsorted_list and isinstance(unsorted_list[0], dict):
if not sort_key:
raise Exception("A sort key was not specified when sorting list")
else:
return sorted(unsorted_list, key=lambda k: k[sort_key])
raise ValueError("A sort key was not specified when sorting list")
return sorted(unsorted_list, key=lambda k: k[sort_key])
# Either the list is empty or does not contain dictionaries
try:
@@ -1081,8 +1078,7 @@ def has_list_changed(new_list, old_list, sort_lists=True, sort_key=None):
old_item_casted = new_item_type(old_item)
if new_item != old_item_casted:
return True
else:
continue
continue
except UnicodeEncodeError:
# Fallback to assuming the strings are different
return True
@@ -1374,7 +1370,7 @@ class DockerService(DockerBaseClass):
try:
memory = human_to_bytes(memory)
except ValueError as exc:
raise Exception(f"Failed to convert limit_memory to bytes: {exc}")
raise ValueError(f"Failed to convert limit_memory to bytes: {exc}")
return {
"limit_cpu": cpus,
"limit_memory": memory,
@@ -1396,7 +1392,7 @@ class DockerService(DockerBaseClass):
try:
memory = human_to_bytes(memory)
except ValueError as exc:
raise Exception(f"Failed to convert reserve_memory to bytes: {exc}")
raise ValueError(f"Failed to convert reserve_memory to bytes: {exc}")
return {
"reserve_cpu": cpus,
"reserve_memory": memory,
@@ -1470,7 +1466,7 @@ class DockerService(DockerBaseClass):
for index, item in invalid_items
]
)
raise Exception(
raise ValueError(
"All items in a command list need to be strings. "
f"Check quoting. Invalid items: {errors}."
)
@@ -2339,7 +2335,7 @@ class DockerServiceManager:
ds.mode = to_text("replicated-job", encoding="utf-8")
ds.replicas = mode["ReplicatedJob"]["TotalCompletions"]
else:
raise Exception(f"Unknown service mode: {mode}")
raise ValueError(f"Unknown service mode: {mode}")
raw_data_mounts = task_template_data["ContainerSpec"].get("Mounts")
if raw_data_mounts:
@@ -2510,7 +2506,7 @@ class DockerServiceManager:
try:
current_service = self.get_service(module.params["name"])
except Exception as e:
except Exception as e: # pylint: disable=broad-exception-caught
self.client.fail(
f"Error looking for service named {module.params['name']}: {e}"
)
@@ -2527,7 +2523,7 @@ class DockerServiceManager:
network_ids,
self.client,
)
except Exception as e:
except Exception as e: # pylint: disable=broad-exception-caught
return self.client.fail(f"Error parsing module parameters: {e}")
changed = False
+1 -1
View File
@@ -87,7 +87,7 @@ def get_existing_volume(client, volume_name):
return client.get_json("/volumes/{0}", volume_name)
except NotFound:
return None
except Exception as exc:
except Exception as exc: # pylint: disable=broad-exception-caught
client.fail(f"Error inspecting volume: {exc}")