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:
Felix Fontein
2025-10-12 16:02:27 +02:00
committed by GitHub
parent cad22de628
commit c75aa5dd64
66 changed files with 1549 additions and 1429 deletions
+26 -24
View File
@@ -228,7 +228,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
}
containers = client.get_json("/containers/json", params=params)
except APIError as exc:
raise AnsibleError(f"Error listing containers: {exc}")
raise AnsibleError(f"Error listing containers: {exc}") from exc
if add_legacy_groups:
self.inventory.add_group("running")
@@ -247,26 +247,28 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
short_container_id = container_id[:13]
try:
name = container.get("Names", list())[0].lstrip("/")
name = container.get("Names", [])[0].lstrip("/")
full_name = name
except IndexError:
name = short_container_id
full_name = container_id
facts = dict(
docker_name=make_unsafe(name),
docker_short_id=make_unsafe(short_container_id),
)
full_facts = dict()
facts = {
"docker_name": make_unsafe(name),
"docker_short_id": make_unsafe(short_container_id),
}
full_facts = {}
try:
inspect = client.get_json("/containers/{0}/json", container_id)
except APIError as exc:
raise AnsibleError(f"Error inspecting container {name} - {exc}")
raise AnsibleError(
f"Error inspecting container {name} - {exc}"
) from exc
state = inspect.get("State") or dict()
config = inspect.get("Config") or dict()
labels = config.get("Labels") or dict()
state = inspect.get("State") or {}
config = inspect.get("Config") or {}
labels = config.get("Labels") or {}
running = state.get("Running")
@@ -298,7 +300,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
port_settings = network_settings.get("Ports") or {}
port = port_settings.get(f"{ssh_port}/tcp")[0]
except (IndexError, AttributeError, TypeError):
port = dict()
port = {}
try:
ip = default_ip if port["HostIp"] == "0.0.0.0" else port["HostIp"]
@@ -306,23 +308,23 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
ip = ""
facts.update(
dict(
ansible_ssh_host=ip,
ansible_ssh_port=port.get("HostPort", 0),
)
{
"ansible_ssh_host": ip,
"ansible_ssh_port": port.get("HostPort", 0),
}
)
elif connection_type == "docker-cli":
facts.update(
dict(
ansible_host=full_name,
)
{
"ansible_host": full_name,
}
)
ansible_connection = "community.docker.docker"
elif connection_type == "docker-api":
facts.update(
dict(
ansible_host=full_name,
)
{
"ansible_host": full_name,
}
)
facts.update(extra_facts)
ansible_connection = "community.docker.docker_api"
@@ -401,8 +403,8 @@ class InventoryModule(BaseInventoryPlugin, Constructable):
try:
self._populate(client)
except DockerException as e:
raise AnsibleError(f"An unexpected Docker error occurred: {e}")
raise AnsibleError(f"An unexpected Docker error occurred: {e}") from e
except RequestException as e:
raise AnsibleError(
f"An unexpected requests error occurred when trying to talk to the Docker daemon: {e}"
)
) from e