From b02ad20a346a3075341362320f4aabc1ae41ad8e Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 7 Oct 2025 21:20:43 +0200 Subject: [PATCH] Address redefined-builtin. --- .pylintrc | 1 - plugins/connection/docker_api.py | 4 +- plugins/inventory/docker_containers.py | 20 +- plugins/inventory/docker_machine.py | 6 +- plugins/module_utils/_common.py | 48 +++-- plugins/module_utils/_common_api.py | 48 +++-- plugins/module_utils/_compose_v2.py | 4 +- .../module_utils/_module_container/base.py | 186 +++++++++--------- .../_module_container/docker_api.py | 10 +- plugins/module_utils/_socket_handler.py | 8 +- plugins/modules/docker_image_build.py | 10 +- plugins/modules/docker_login.py | 6 +- .../test_docker_container_copy_into.py | 34 ++-- .../modules/test_docker_image_build.py | 6 +- 14 files changed, 216 insertions(+), 175 deletions(-) diff --git a/.pylintrc b/.pylintrc index e6735c8d..49150be7 100644 --- a/.pylintrc +++ b/.pylintrc @@ -408,7 +408,6 @@ disable=raw-checker-failed, protected-access, raise-missing-from, redefined-argument-from-local, - redefined-builtin, redefined-outer-name, simplifiable-if-expression, subprocess-popen-preexec-fn, diff --git a/plugins/connection/docker_api.py b/plugins/connection/docker_api.py index 3f5920bb..a262dbe4 100644 --- a/plugins/connection/docker_api.py +++ b/plugins/connection/docker_api.py @@ -149,10 +149,10 @@ class Connection(ConnectionBase): transport = "community.docker.docker_api" has_pipelining = True - def _call_client(self, callable, not_found_can_be_resource=False): + def _call_client(self, f, not_found_can_be_resource=False): remote_addr = self.get_option("remote_addr") try: - return callable() + return f() except NotFound as e: if not_found_can_be_resource: raise AnsibleConnectionFailure( diff --git a/plugins/inventory/docker_containers.py b/plugins/inventory/docker_containers.py index 3f7c0405..91f433f4 100644 --- a/plugins/inventory/docker_containers.py +++ b/plugins/inventory/docker_containers.py @@ -243,24 +243,24 @@ class InventoryModule(BaseInventoryPlugin, Constructable): filters = parse_filters(self.get_option("filters")) for container in containers: - id = container.get("Id") - short_id = id[:13] + container_id = container.get("Id") + short_container_id = container_id[:13] try: name = container.get("Names", list())[0].lstrip("/") full_name = name except IndexError: - name = short_id - full_name = id + name = short_container_id + full_name = container_id facts = dict( docker_name=make_unsafe(name), - docker_short_id=make_unsafe(short_id), + docker_short_id=make_unsafe(short_container_id), ) full_facts = dict() try: - inspect = client.get_json("/containers/{0}/json", id) + inspect = client.get_json("/containers/{0}/json", container_id) except APIError as exc: raise AnsibleError(f"Error inspecting container {name} - {exc}") @@ -371,12 +371,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable): # When we do this before a set_variable() call, the variables are assigned # to the group, and not to the host. if add_legacy_groups: - self.inventory.add_group(id) - self.inventory.add_host(name, group=id) + self.inventory.add_group(container_id) + self.inventory.add_host(name, group=container_id) self.inventory.add_group(name) self.inventory.add_host(name, group=name) - self.inventory.add_group(short_id) - self.inventory.add_host(name, group=short_id) + self.inventory.add_group(short_container_id) + self.inventory.add_host(name, group=short_container_id) self.inventory.add_group(hostname) self.inventory.add_host(name, group=hostname) diff --git a/plugins/inventory/docker_machine.py b/plugins/inventory/docker_machine.py index fc77e7ba..86e0e8d2 100644 --- a/plugins/inventory/docker_machine.py +++ b/plugins/inventory/docker_machine.py @@ -170,15 +170,15 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable): # capture any of the DOCKER_xxx variables that were output and create Ansible host vars # with the same name and value but with a dm_ name prefix. - vars = [] + env_vars = [] for line in env_lines: match = re.search('(DOCKER_[^=]+)="([^"]+)"', line) if match: env_var_name = match.group(1) env_var_value = match.group(2) - vars.append((env_var_name, env_var_value)) + env_vars.append((env_var_name, env_var_value)) - return vars + return env_vars def _get_machine_names(self): # Filter out machines that are not in the Running state as we probably cannot do anything useful actions diff --git a/plugins/module_utils/_common.py b/plugins/module_utils/_common.py index f7b3d90c..2746b5c9 100644 --- a/plugins/module_utils/_common.py +++ b/plugins/module_utils/_common.py @@ -258,16 +258,18 @@ class AnsibleDockerClientBase(Client): pass @staticmethod - def _get_value(param_name, param_value, env_variable, default_value, type="str"): + def _get_value( + param_name, param_value, env_variable, default_value, value_type="str" + ): if param_value is not None: # take module parameter value - if type == "bool": + if value_type == "bool": if param_value in BOOLEANS_TRUE: return True if param_value in BOOLEANS_FALSE: return False return bool(param_value) - if type == "int": + if value_type == "int": return int(param_value) return param_value @@ -281,13 +283,13 @@ class AnsibleDockerClientBase(Client): return os.path.join(env_value, "ca.pem") if param_name == "key_path": return os.path.join(env_value, "key.pem") - if type == "bool": + if value_type == "bool": if env_value in BOOLEANS_TRUE: return True if env_value in BOOLEANS_FALSE: return False return bool(env_value) - if type == "int": + if value_type == "int": return int(env_value) return env_value @@ -317,50 +319,66 @@ class AnsibleDockerClientBase(Client): params["docker_host"], "DOCKER_HOST", DEFAULT_DOCKER_HOST, - type="str", + value_type="str", ), tls_hostname=self._get_value( "tls_hostname", params["tls_hostname"], "DOCKER_TLS_HOSTNAME", None, - type="str", + value_type="str", ), api_version=self._get_value( "api_version", params["api_version"], "DOCKER_API_VERSION", "auto", - type="str", + value_type="str", ), cacert_path=self._get_value( - "cacert_path", params["ca_path"], "DOCKER_CERT_PATH", None, type="str" + "cacert_path", + params["ca_path"], + "DOCKER_CERT_PATH", + None, + value_type="str", ), cert_path=self._get_value( - "cert_path", params["client_cert"], "DOCKER_CERT_PATH", None, type="str" + "cert_path", + params["client_cert"], + "DOCKER_CERT_PATH", + None, + value_type="str", ), key_path=self._get_value( - "key_path", params["client_key"], "DOCKER_CERT_PATH", None, type="str" + "key_path", + params["client_key"], + "DOCKER_CERT_PATH", + None, + value_type="str", ), tls=self._get_value( - "tls", params["tls"], "DOCKER_TLS", DEFAULT_TLS, type="bool" + "tls", params["tls"], "DOCKER_TLS", DEFAULT_TLS, value_type="bool" ), tls_verify=self._get_value( "validate_certs", params["validate_certs"], "DOCKER_TLS_VERIFY", DEFAULT_TLS_VERIFY, - type="bool", + value_type="bool", ), timeout=self._get_value( "timeout", params["timeout"], "DOCKER_TIMEOUT", DEFAULT_TIMEOUT_SECONDS, - type="int", + value_type="int", ), use_ssh_client=self._get_value( - "use_ssh_client", params["use_ssh_client"], None, False, type="bool" + "use_ssh_client", + params["use_ssh_client"], + None, + False, + value_type="bool", ), ) diff --git a/plugins/module_utils/_common_api.py b/plugins/module_utils/_common_api.py index 4ad665c5..a725bc10 100644 --- a/plugins/module_utils/_common_api.py +++ b/plugins/module_utils/_common_api.py @@ -151,16 +151,18 @@ class AnsibleDockerClientBase(Client): pass @staticmethod - def _get_value(param_name, param_value, env_variable, default_value, type="str"): + def _get_value( + param_name, param_value, env_variable, default_value, value_type="str" + ): if param_value is not None: # take module parameter value - if type == "bool": + if value_type == "bool": if param_value in BOOLEANS_TRUE: return True if param_value in BOOLEANS_FALSE: return False return bool(param_value) - if type == "int": + if value_type == "int": return int(param_value) return param_value @@ -174,13 +176,13 @@ class AnsibleDockerClientBase(Client): return os.path.join(env_value, "ca.pem") if param_name == "key_path": return os.path.join(env_value, "key.pem") - if type == "bool": + if value_type == "bool": if env_value in BOOLEANS_TRUE: return True if env_value in BOOLEANS_FALSE: return False return bool(env_value) - if type == "int": + if value_type == "int": return int(env_value) return env_value @@ -210,50 +212,66 @@ class AnsibleDockerClientBase(Client): params["docker_host"], "DOCKER_HOST", DEFAULT_DOCKER_HOST, - type="str", + value_type="str", ), tls_hostname=self._get_value( "tls_hostname", params["tls_hostname"], "DOCKER_TLS_HOSTNAME", None, - type="str", + value_type="str", ), api_version=self._get_value( "api_version", params["api_version"], "DOCKER_API_VERSION", "auto", - type="str", + value_type="str", ), cacert_path=self._get_value( - "cacert_path", params["ca_path"], "DOCKER_CERT_PATH", None, type="str" + "cacert_path", + params["ca_path"], + "DOCKER_CERT_PATH", + None, + value_type="str", ), cert_path=self._get_value( - "cert_path", params["client_cert"], "DOCKER_CERT_PATH", None, type="str" + "cert_path", + params["client_cert"], + "DOCKER_CERT_PATH", + None, + value_type="str", ), key_path=self._get_value( - "key_path", params["client_key"], "DOCKER_CERT_PATH", None, type="str" + "key_path", + params["client_key"], + "DOCKER_CERT_PATH", + None, + value_type="str", ), tls=self._get_value( - "tls", params["tls"], "DOCKER_TLS", DEFAULT_TLS, type="bool" + "tls", params["tls"], "DOCKER_TLS", DEFAULT_TLS, value_type="bool" ), tls_verify=self._get_value( "validate_certs", params["validate_certs"], "DOCKER_TLS_VERIFY", DEFAULT_TLS_VERIFY, - type="bool", + value_type="bool", ), timeout=self._get_value( "timeout", params["timeout"], "DOCKER_TIMEOUT", DEFAULT_TIMEOUT_SECONDS, - type="int", + value_type="int", ), use_ssh_client=self._get_value( - "use_ssh_client", params["use_ssh_client"], None, False, type="bool" + "use_ssh_client", + params["use_ssh_client"], + None, + False, + value_type="bool", ), ) diff --git a/plugins/module_utils/_compose_v2.py b/plugins/module_utils/_compose_v2.py index 300d50c9..834d6d88 100644 --- a/plugins/module_utils/_compose_v2.py +++ b/plugins/module_utils/_compose_v2.py @@ -942,9 +942,9 @@ class BaseComposeManager(DockerBaseClass): result.pop(res) def cleanup(self): - for dir in self.cleanup_dirs: + for directory in self.cleanup_dirs: try: - shutil.rmtree(dir, True) + shutil.rmtree(directory, True) except Exception: # should not happen, but simply ignore to be on the safe side pass diff --git a/plugins/module_utils/_module_container/base.py b/plugins/module_utils/_module_container/base.py index 0d350138..5ac5a7f3 100644 --- a/plugins/module_utils/_module_container/base.py +++ b/plugins/module_utils/_module_container/base.py @@ -53,19 +53,19 @@ _MOUNT_OPTION_TYPES = dict( ) -def _get_ansible_type(type): - if type == "set": +def _get_ansible_type(value_type): + if value_type == "set": return "list" - if type not in ("list", "dict", "bool", "int", "float", "str"): - raise Exception(f'Invalid type "{type}"') - return type + if value_type not in ("list", "dict", "bool", "int", "float", "str"): + raise Exception(f'Invalid type "{value_type}"') + return value_type class Option(object): def __init__( self, name, - type, + value_type, owner, ansible_type=None, elements=None, @@ -81,9 +81,9 @@ class Option(object): compare=None, ): self.name = name - self.type = type - self.ansible_type = ansible_type or _get_ansible_type(type) - needs_elements = self.type in ("list", "set") + self.value_type = value_type + self.ansible_type = ansible_type or _get_ansible_type(value_type) + needs_elements = self.value_type in ("list", "set") needs_ansible_elements = self.ansible_type in ("list",) if elements is not None and not needs_elements: raise Exception("elements only allowed for lists/sets") @@ -118,7 +118,7 @@ class Option(object): self.ansible_suboptions = ansible_suboptions if needs_suboptions else None self.ansible_aliases = ansible_aliases or [] self.ansible_choices = ansible_choices - comparison_type = self.type + comparison_type = self.value_type if comparison_type == "set" and self.elements == "dict": comparison_type = "set(dict)" elif comparison_type not in ("set", "list", "dict"): @@ -920,55 +920,55 @@ def _compare_platform(option, param_value, container_value): return param_value == container_value -OPTION_AUTO_REMOVE = OptionGroup().add_option("auto_remove", type="bool") +OPTION_AUTO_REMOVE = OptionGroup().add_option("auto_remove", value_type="bool") -OPTION_BLKIO_WEIGHT = OptionGroup().add_option("blkio_weight", type="int") +OPTION_BLKIO_WEIGHT = OptionGroup().add_option("blkio_weight", value_type="int") OPTION_CAPABILITIES = OptionGroup().add_option( - "capabilities", type="set", elements="str" + "capabilities", value_type="set", elements="str" ) -OPTION_CAP_DROP = OptionGroup().add_option("cap_drop", type="set", elements="str") +OPTION_CAP_DROP = OptionGroup().add_option("cap_drop", value_type="set", elements="str") OPTION_CGROUP_NS_MODE = OptionGroup().add_option( - "cgroupns_mode", type="str", ansible_choices=["private", "host"] + "cgroupns_mode", value_type="str", ansible_choices=["private", "host"] ) -OPTION_CGROUP_PARENT = OptionGroup().add_option("cgroup_parent", type="str") +OPTION_CGROUP_PARENT = OptionGroup().add_option("cgroup_parent", value_type="str") OPTION_COMMAND = OptionGroup(preprocess=_preprocess_command).add_option( - "command", type="list", elements="str", ansible_type="raw" + "command", value_type="list", elements="str", ansible_type="raw" ) -OPTION_CPU_PERIOD = OptionGroup().add_option("cpu_period", type="int") +OPTION_CPU_PERIOD = OptionGroup().add_option("cpu_period", value_type="int") -OPTION_CPU_QUOTA = OptionGroup().add_option("cpu_quota", type="int") +OPTION_CPU_QUOTA = OptionGroup().add_option("cpu_quota", value_type="int") -OPTION_CPUSET_CPUS = OptionGroup().add_option("cpuset_cpus", type="str") +OPTION_CPUSET_CPUS = OptionGroup().add_option("cpuset_cpus", value_type="str") -OPTION_CPUSET_MEMS = OptionGroup().add_option("cpuset_mems", type="str") +OPTION_CPUSET_MEMS = OptionGroup().add_option("cpuset_mems", value_type="str") -OPTION_CPU_SHARES = OptionGroup().add_option("cpu_shares", type="int") +OPTION_CPU_SHARES = OptionGroup().add_option("cpu_shares", value_type="int") OPTION_ENTRYPOINT = OptionGroup(preprocess=_preprocess_entrypoint).add_option( - "entrypoint", type="list", elements="str" + "entrypoint", value_type="list", elements="str" ) -OPTION_CPUS = OptionGroup().add_option("cpus", type="int", ansible_type="float") +OPTION_CPUS = OptionGroup().add_option("cpus", value_type="int", ansible_type="float") OPTION_DETACH_INTERACTIVE = ( OptionGroup() - .add_option("detach", type="bool") - .add_option("interactive", type="bool") + .add_option("detach", value_type="bool") + .add_option("interactive", value_type="bool") ) OPTION_DEVICES = OptionGroup().add_option( - "devices", type="set", elements="dict", ansible_elements="str" + "devices", value_type="set", elements="dict", ansible_elements="str" ) OPTION_DEVICE_READ_BPS = OptionGroup().add_option( "device_read_bps", - type="set", + value_type="set", elements="dict", ansible_suboptions=dict( path=dict(required=True, type="str"), @@ -978,7 +978,7 @@ OPTION_DEVICE_READ_BPS = OptionGroup().add_option( OPTION_DEVICE_WRITE_BPS = OptionGroup().add_option( "device_write_bps", - type="set", + value_type="set", elements="dict", ansible_suboptions=dict( path=dict(required=True, type="str"), @@ -988,7 +988,7 @@ OPTION_DEVICE_WRITE_BPS = OptionGroup().add_option( OPTION_DEVICE_READ_IOPS = OptionGroup().add_option( "device_read_iops", - type="set", + value_type="set", elements="dict", ansible_suboptions=dict( path=dict(required=True, type="str"), @@ -998,7 +998,7 @@ OPTION_DEVICE_READ_IOPS = OptionGroup().add_option( OPTION_DEVICE_WRITE_IOPS = OptionGroup().add_option( "device_write_iops", - type="set", + value_type="set", elements="dict", ansible_suboptions=dict( path=dict(required=True, type="str"), @@ -1008,7 +1008,7 @@ OPTION_DEVICE_WRITE_IOPS = OptionGroup().add_option( OPTION_DEVICE_REQUESTS = OptionGroup().add_option( "device_requests", - type="set", + value_type="set", elements="dict", ansible_suboptions=dict( capabilities=dict(type="list", elements="list"), @@ -1020,29 +1020,33 @@ OPTION_DEVICE_REQUESTS = OptionGroup().add_option( ) OPTION_DEVICE_CGROUP_RULES = OptionGroup().add_option( - "device_cgroup_rules", type="list", elements="str" + "device_cgroup_rules", value_type="list", elements="str" ) OPTION_DNS_SERVERS = OptionGroup().add_option( - "dns_servers", type="list", elements="str" + "dns_servers", value_type="list", elements="str" ) -OPTION_DNS_OPTS = OptionGroup().add_option("dns_opts", type="set", elements="str") +OPTION_DNS_OPTS = OptionGroup().add_option("dns_opts", value_type="set", elements="str") OPTION_DNS_SEARCH_DOMAINS = OptionGroup().add_option( - "dns_search_domains", type="list", elements="str" + "dns_search_domains", value_type="list", elements="str" ) -OPTION_DOMAINNAME = OptionGroup().add_option("domainname", type="str") +OPTION_DOMAINNAME = OptionGroup().add_option("domainname", value_type="str") OPTION_ENVIRONMENT = ( OptionGroup(preprocess=_preprocess_env) .add_option( - "env", type="set", ansible_type="dict", elements="str", needs_no_suboptions=True + "env", + value_type="set", + ansible_type="dict", + elements="str", + needs_no_suboptions=True, ) .add_option( "env_file", - type="set", + value_type="set", ansible_type="path", elements="str", not_a_container_option=True, @@ -1051,17 +1055,17 @@ OPTION_ENVIRONMENT = ( OPTION_ETC_HOSTS = OptionGroup().add_option( "etc_hosts", - type="set", + value_type="set", ansible_type="dict", elements="str", needs_no_suboptions=True, ) -OPTION_GROUPS = OptionGroup().add_option("groups", type="set", elements="str") +OPTION_GROUPS = OptionGroup().add_option("groups", value_type="set", elements="str") OPTION_HEALTHCHECK = OptionGroup(preprocess=_preprocess_healthcheck).add_option( "healthcheck", - type="dict", + value_type="dict", ansible_suboptions=dict( test=dict(type="raw"), test_cli_compatible=dict(type="bool", default=False), @@ -1073,69 +1077,71 @@ OPTION_HEALTHCHECK = OptionGroup(preprocess=_preprocess_healthcheck).add_option( ), ) -OPTION_HOSTNAME = OptionGroup().add_option("hostname", type="str") +OPTION_HOSTNAME = OptionGroup().add_option("hostname", value_type="str") -OPTION_IMAGE = OptionGroup().add_option("image", type="str") +OPTION_IMAGE = OptionGroup().add_option("image", value_type="str") -OPTION_INIT = OptionGroup().add_option("init", type="bool") +OPTION_INIT = OptionGroup().add_option("init", value_type="bool") -OPTION_IPC_MODE = OptionGroup().add_option("ipc_mode", type="str") +OPTION_IPC_MODE = OptionGroup().add_option("ipc_mode", value_type="str") OPTION_KERNEL_MEMORY = OptionGroup( preprocess=partial(_preprocess_convert_to_bytes, name="kernel_memory") -).add_option("kernel_memory", type="int", ansible_type="str") +).add_option("kernel_memory", value_type="int", ansible_type="str") OPTION_LABELS = OptionGroup(preprocess=_preprocess_labels).add_option( - "labels", type="dict", needs_no_suboptions=True + "labels", value_type="dict", needs_no_suboptions=True ) OPTION_LINKS = OptionGroup().add_option( - "links", type="set", elements="list", ansible_elements="str" + "links", value_type="set", elements="list", ansible_elements="str" ) OPTION_LOG_DRIVER_OPTIONS = ( OptionGroup( preprocess=_preprocess_log, ansible_required_by={"log_options": ["log_driver"]} ) - .add_option("log_driver", type="str") + .add_option("log_driver", value_type="str") .add_option( "log_options", - type="dict", + value_type="dict", ansible_aliases=["log_opt"], needs_no_suboptions=True, ) ) OPTION_MAC_ADDRESS = OptionGroup(preprocess=_preprocess_mac_address).add_option( - "mac_address", type="str" + "mac_address", value_type="str" ) OPTION_MEMORY = OptionGroup( preprocess=partial(_preprocess_convert_to_bytes, name="memory") -).add_option("memory", type="int", ansible_type="str") +).add_option("memory", value_type="int", ansible_type="str") OPTION_MEMORY_RESERVATION = OptionGroup( preprocess=partial(_preprocess_convert_to_bytes, name="memory_reservation") -).add_option("memory_reservation", type="int", ansible_type="str") +).add_option("memory_reservation", value_type="int", ansible_type="str") OPTION_MEMORY_SWAP = OptionGroup( preprocess=partial( _preprocess_convert_to_bytes, name="memory_swap", unlimited_value=-1 ) -).add_option("memory_swap", type="int", ansible_type="str") +).add_option("memory_swap", value_type="int", ansible_type="str") -OPTION_MEMORY_SWAPPINESS = OptionGroup().add_option("memory_swappiness", type="int") +OPTION_MEMORY_SWAPPINESS = OptionGroup().add_option( + "memory_swappiness", value_type="int" +) OPTION_STOP_TIMEOUT = OptionGroup().add_option( - "stop_timeout", type="int", default_comparison="ignore" + "stop_timeout", value_type="int", default_comparison="ignore" ) OPTION_NETWORK = ( OptionGroup(preprocess=_preprocess_networks) - .add_option("network_mode", type="str") + .add_option("network_mode", value_type="str") .add_option( "networks", - type="set", + value_type="set", elements="dict", ansible_suboptions=dict( name=dict(type="str", required=True), @@ -1150,81 +1156,81 @@ OPTION_NETWORK = ( ) ) -OPTION_OOM_KILLER = OptionGroup().add_option("oom_killer", type="bool") +OPTION_OOM_KILLER = OptionGroup().add_option("oom_killer", value_type="bool") -OPTION_OOM_SCORE_ADJ = OptionGroup().add_option("oom_score_adj", type="int") +OPTION_OOM_SCORE_ADJ = OptionGroup().add_option("oom_score_adj", value_type="int") -OPTION_PID_MODE = OptionGroup().add_option("pid_mode", type="str") +OPTION_PID_MODE = OptionGroup().add_option("pid_mode", value_type="str") -OPTION_PIDS_LIMIT = OptionGroup().add_option("pids_limit", type="int") +OPTION_PIDS_LIMIT = OptionGroup().add_option("pids_limit", value_type="int") OPTION_PLATFORM = OptionGroup().add_option( - "platform", type="str", compare=_compare_platform + "platform", value_type="str", compare=_compare_platform ) -OPTION_PRIVILEGED = OptionGroup().add_option("privileged", type="bool") +OPTION_PRIVILEGED = OptionGroup().add_option("privileged", value_type="bool") -OPTION_READ_ONLY = OptionGroup().add_option("read_only", type="bool") +OPTION_READ_ONLY = OptionGroup().add_option("read_only", value_type="bool") OPTION_RESTART_POLICY = ( OptionGroup(ansible_required_by={"restart_retries": ["restart_policy"]}) .add_option( "restart_policy", - type="str", + value_type="str", ansible_choices=["no", "on-failure", "always", "unless-stopped"], ) - .add_option("restart_retries", type="int") + .add_option("restart_retries", value_type="int") ) -OPTION_RUNTIME = OptionGroup().add_option("runtime", type="str") +OPTION_RUNTIME = OptionGroup().add_option("runtime", value_type="str") OPTION_SECURITY_OPTS = OptionGroup().add_option( - "security_opts", type="set", elements="str" + "security_opts", value_type="set", elements="str" ) OPTION_SHM_SIZE = OptionGroup( preprocess=partial(_preprocess_convert_to_bytes, name="shm_size") -).add_option("shm_size", type="int", ansible_type="str") +).add_option("shm_size", value_type="int", ansible_type="str") -OPTION_STOP_SIGNAL = OptionGroup().add_option("stop_signal", type="str") +OPTION_STOP_SIGNAL = OptionGroup().add_option("stop_signal", value_type="str") OPTION_STORAGE_OPTS = OptionGroup().add_option( - "storage_opts", type="dict", needs_no_suboptions=True + "storage_opts", value_type="dict", needs_no_suboptions=True ) OPTION_SYSCTLS = OptionGroup(preprocess=_preprocess_sysctls).add_option( - "sysctls", type="dict", needs_no_suboptions=True + "sysctls", value_type="dict", needs_no_suboptions=True ) OPTION_TMPFS = OptionGroup(preprocess=_preprocess_tmpfs).add_option( - "tmpfs", type="dict", ansible_type="list", ansible_elements="str" + "tmpfs", value_type="dict", ansible_type="list", ansible_elements="str" ) -OPTION_TTY = OptionGroup().add_option("tty", type="bool") +OPTION_TTY = OptionGroup().add_option("tty", value_type="bool") OPTION_ULIMITS = OptionGroup(preprocess=_preprocess_ulimits).add_option( - "ulimits", type="set", elements="dict", ansible_elements="str" + "ulimits", value_type="set", elements="dict", ansible_elements="str" ) -OPTION_USER = OptionGroup().add_option("user", type="str") +OPTION_USER = OptionGroup().add_option("user", value_type="str") -OPTION_USERNS_MODE = OptionGroup().add_option("userns_mode", type="str") +OPTION_USERNS_MODE = OptionGroup().add_option("userns_mode", value_type="str") -OPTION_UTS = OptionGroup().add_option("uts", type="str") +OPTION_UTS = OptionGroup().add_option("uts", value_type="str") -OPTION_VOLUME_DRIVER = OptionGroup().add_option("volume_driver", type="str") +OPTION_VOLUME_DRIVER = OptionGroup().add_option("volume_driver", value_type="str") OPTION_VOLUMES_FROM = OptionGroup().add_option( - "volumes_from", type="set", elements="str" + "volumes_from", value_type="set", elements="str" ) -OPTION_WORKING_DIR = OptionGroup().add_option("working_dir", type="str") +OPTION_WORKING_DIR = OptionGroup().add_option("working_dir", value_type="str") OPTION_MOUNTS_VOLUMES = ( OptionGroup(preprocess=_preprocess_mounts) .add_option( "mounts", - type="set", + value_type="set", elements="dict", ansible_suboptions=dict( target=dict(type="str", required=True), @@ -1256,10 +1262,10 @@ OPTION_MOUNTS_VOLUMES = ( tmpfs_options=dict(type="list", elements="dict"), ), ) - .add_option("volumes", type="set", elements="str") + .add_option("volumes", value_type="set", elements="str") .add_option( "volume_binds", - type="set", + value_type="set", elements="str", not_an_ansible_option=True, copy_comparison_from="volumes", @@ -1270,21 +1276,21 @@ OPTION_PORTS = ( OptionGroup(preprocess=_preprocess_ports) .add_option( "exposed_ports", - type="set", + value_type="set", elements="str", ansible_aliases=["exposed", "expose"], ) - .add_option("publish_all_ports", type="bool") + .add_option("publish_all_ports", value_type="bool") .add_option( "published_ports", - type="dict", + value_type="dict", ansible_type="list", ansible_elements="str", ansible_aliases=["ports"], ) .add_option( "ports", - type="set", + value_type="set", elements="str", not_an_ansible_option=True, default_comparison="ignore", diff --git a/plugins/module_utils/_module_container/docker_api.py b/plugins/module_utils/_module_container/docker_api.py index 1604eebc..21b57bba 100644 --- a/plugins/module_utils/_module_container/docker_api.py +++ b/plugins/module_utils/_module_container/docker_api.py @@ -119,12 +119,12 @@ _DEFAULT_IP_REPLACEMENT_STRING = ( ) -def _get_ansible_type(type): - if type == "set": +def _get_ansible_type(our_type): + if our_type == "set": return "list" - if type not in ("list", "dict", "bool", "int", "float", "str"): - raise Exception(f'Invalid type "{type}"') - return type + if our_type not in ("list", "dict", "bool", "int", "float", "str"): + raise Exception(f'Invalid type "{our_type}"') + return our_type _SENTRY = object() diff --git a/plugins/module_utils/_socket_handler.py b/plugins/module_utils/_socket_handler.py index 0476e918..840cd199 100644 --- a/plugins/module_utils/_socket_handler.py +++ b/plugins/module_utils/_socket_handler.py @@ -56,7 +56,7 @@ class DockerSocketHandlerBase(object): def __enter__(self): return self - def __exit__(self, type, value, tb): + def __exit__(self, type_, value, tb): self._selector.close() def set_block_done_callback(self, block_done_callback): @@ -204,9 +204,9 @@ class DockerSocketHandlerBase(object): self.select() return b"".join(stdout), b"".join(stderr) - def write(self, str): - self._write_buffer += str - if len(self._write_buffer) == len(str): + def write(self, str_to_write): + self._write_buffer += str_to_write + if len(self._write_buffer) == len(str_to_write): self._write() diff --git a/plugins/modules/docker_image_build.py b/plugins/modules/docker_image_build.py index 1fe60548..5beefdf6 100644 --- a/plugins/modules/docker_image_build.py +++ b/plugins/modules/docker_image_build.py @@ -318,11 +318,11 @@ def dict_to_list(dictionary, concat="="): return [f"{k}{concat}{v}" for k, v in sorted(dictionary.items())] -def _quote_csv(input): - if input.strip() == input and all(i not in input for i in '",\r\n'): - return input - input = input.replace('"', '""') - return f'"{input}"' +def _quote_csv(text): + if text.strip() == text and all(i not in text for i in '",\r\n'): + return text + text = text.replace('"', '""') + return f'"{text}"' class ImageBuilder(DockerBaseClass): diff --git a/plugins/modules/docker_login.py b/plugins/modules/docker_login.py index 355466a0..aee2c9c8 100644 --- a/plugins/modules/docker_login.py +++ b/plugins/modules/docker_login.py @@ -197,9 +197,9 @@ class DockerFileStore(object): Write config back out to disk. """ # Make sure directory exists - dir = os.path.dirname(self._config_path) - if not os.path.exists(dir): - os.makedirs(dir) + directory = os.path.dirname(self._config_path) + if not os.path.exists(directory): + os.makedirs(directory) # Write config; make sure it has permissions 0x600 content = json.dumps(self._config, indent=4, sort_keys=True).encode("utf-8") f = os.open(self._config_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600) diff --git a/tests/unit/plugins/modules/test_docker_container_copy_into.py b/tests/unit/plugins/modules/test_docker_container_copy_into.py index 3b93e84d..b54bcc72 100644 --- a/tests/unit/plugins/modules/test_docker_container_copy_into.py +++ b/tests/unit/plugins/modules/test_docker_container_copy_into.py @@ -15,7 +15,7 @@ from ansible_collections.community.docker.plugins.modules.docker_container_copy_ @pytest.mark.parametrize( - "input, expected", + "value, expected", [ ("0777", 0o777), ("777", 0o777), @@ -32,13 +32,13 @@ from ansible_collections.community.docker.plugins.modules.docker_container_copy_ ("-1", -1), ], ) -def test_parse_string(input, expected): - assert parse_modern(input) == expected - assert parse_octal_string_only(input) == expected +def test_parse_string(value, expected): + assert parse_modern(value) == expected + assert parse_octal_string_only(value) == expected @pytest.mark.parametrize( - "input", + "value", [ 0o777, 0o755, @@ -47,14 +47,14 @@ def test_parse_string(input, expected): 123456789012345678901234567890123456789012345678901234567890, ], ) -def test_parse_int(input): - assert parse_modern(input) == input - with pytest.raises(TypeError, match=f"^must be an octal string, got {input}L?$"): - parse_octal_string_only(input) +def test_parse_int(value): + assert parse_modern(value) == value + with pytest.raises(TypeError, match=f"^must be an octal string, got {value}L?$"): + parse_octal_string_only(value) @pytest.mark.parametrize( - "input", + "value", [ 1.0, 755.5, @@ -62,23 +62,23 @@ def test_parse_int(input): {}, ], ) -def test_parse_bad_type(input): +def test_parse_bad_type(value): with pytest.raises(TypeError, match="^must be an octal string or an integer, got "): - parse_modern(input) + parse_modern(value) with pytest.raises(TypeError, match="^must be an octal string, got "): - parse_octal_string_only(input) + parse_octal_string_only(value) @pytest.mark.parametrize( - "input", + "value", [ "foo", "8", "9", ], ) -def test_parse_bad_value(input): +def test_parse_bad_value(value): with pytest.raises(ValueError): - parse_modern(input) + parse_modern(value) with pytest.raises(ValueError): - parse_octal_string_only(input) + parse_octal_string_only(value) diff --git a/tests/unit/plugins/modules/test_docker_image_build.py b/tests/unit/plugins/modules/test_docker_image_build.py index 900112d6..e5ced656 100644 --- a/tests/unit/plugins/modules/test_docker_image_build.py +++ b/tests/unit/plugins/modules/test_docker_image_build.py @@ -14,7 +14,7 @@ from ansible_collections.community.docker.plugins.modules.docker_image_build imp @pytest.mark.parametrize( - "input, expected", + "value, expected", [ ("", ""), (" ", '" "'), @@ -23,5 +23,5 @@ from ansible_collections.community.docker.plugins.modules.docker_image_build imp ('\rhello, "hi" !\n', '"\rhello, ""hi"" !\n"'), ], ) -def test__quote_csv(input, expected): - assert _quote_csv(input) == expected +def test__quote_csv(value, expected): + assert _quote_csv(value) == expected