use dict comprehension in plugins, part 2 (#8822)

* use dict comprehension in plugins

* add changelog frag
This commit is contained in:
Alexei Znamensky 2024-09-06 07:47:28 +12:00 committed by GitHub
commit 7e978c77b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 76 additions and 68 deletions

View file

@ -193,13 +193,8 @@ def run_module():
allowed_keys = ['host', 'port', 'ca_cert', 'cert_cert', 'cert_key',
'timeout', 'user', 'password']
# TODO(evrardjp): Move this back to a dict comprehension when python 2.7 is
# the minimum supported version
# client_params = {key: value for key, value in module.params.items() if key in allowed_keys}
client_params = dict()
for key, value in module.params.items():
if key in allowed_keys:
client_params[key] = value
client_params = {key: value for key, value in module.params.items() if key in allowed_keys}
try:
etcd = etcd3.client(**client_params)
except Exception as exp:

View file

@ -534,11 +534,7 @@ class GitLabProject(object):
@param arguments Attributes of the project
'''
def get_options_with_value(self, arguments):
ret_arguments = dict()
for arg_key, arg_value in arguments.items():
if arguments[arg_key] is not None:
ret_arguments[arg_key] = arg_value
ret_arguments = {k: v for k, v in arguments.items() if v is not None}
return ret_arguments
'''
@ -549,8 +545,8 @@ class GitLabProject(object):
changed = False
for arg_key, arg_value in arguments.items():
if arguments[arg_key] is not None:
if getattr(project, arg_key, None) != arguments[arg_key]:
if arg_value is not None:
if getattr(project, arg_key, None) != arg_value:
if arg_key == 'container_expiration_policy':
old_val = getattr(project, arg_key, {})
final_val = {key: value for key, value in arg_value.items() if value is not None}

View file

@ -1163,8 +1163,7 @@ def send_delete_volume_request(module, params, client, info):
path_parameters = {
"volume_id": ["volume_id"],
}
data = dict((key, navigate_value(info, path))
for key, path in path_parameters.items())
data = {key: navigate_value(info, path) for key, path in path_parameters.items()}
url = build_path(module, "cloudservers/{id}/detachvolume/{volume_id}", data)

View file

@ -771,8 +771,7 @@ def async_wait(config, result, client, timeout):
path_parameters = {
"job_id": ["job_id"],
}
data = dict((key, navigate_value(result, path))
for key, path in path_parameters.items())
data = {key: navigate_value(result, path) for key, path in path_parameters.items()}
url = build_path(module, "jobs/{job_id}", data)

View file

@ -547,8 +547,7 @@ def async_wait_create(config, result, client, timeout):
path_parameters = {
"publicip_id": ["publicip", "id"],
}
data = dict((key, navigate_value(result, path))
for key, path in path_parameters.items())
data = {key: navigate_value(result, path) for key, path in path_parameters.items()}
url = build_path(module, "publicips/{publicip_id}", data)

View file

@ -407,8 +407,7 @@ def async_wait_create(config, result, client, timeout):
path_parameters = {
"peering_id": ["peering", "id"],
}
data = dict((key, navigate_value(result, path))
for key, path in path_parameters.items())
data = {key: navigate_value(result, path) for key, path in path_parameters.items()}
url = build_path(module, "v2.0/vpc/peerings/{peering_id}", data)

View file

@ -560,8 +560,7 @@ def async_wait_create(config, result, client, timeout):
path_parameters = {
"port_id": ["port", "id"],
}
data = dict((key, navigate_value(result, path))
for key, path in path_parameters.items())
data = {key: navigate_value(result, path) for key, path in path_parameters.items()}
url = build_path(module, "ports/{port_id}", data)

View file

@ -440,8 +440,7 @@ def async_wait_create(config, result, client, timeout):
path_parameters = {
"subnet_id": ["subnet", "id"],
}
data = dict((key, navigate_value(result, path))
for key, path in path_parameters.items())
data = {key: navigate_value(result, path) for key, path in path_parameters.items()}
url = build_path(module, "subnets/{subnet_id}", data)
@ -538,8 +537,7 @@ def async_wait_update(config, result, client, timeout):
path_parameters = {
"subnet_id": ["subnet", "id"],
}
data = dict((key, navigate_value(result, path))
for key, path in path_parameters.items())
data = {key: navigate_value(result, path) for key, path in path_parameters.items()}
url = build_path(module, "subnets/{subnet_id}", data)

View file

@ -392,9 +392,7 @@ def ensure(module, client):
'counter': 'ipatokenhotpcounter'}
# Create inverse dictionary for mapping return values
ipa_to_ansible = {}
for (k, v) in ansible_to_ipa.items():
ipa_to_ansible[v] = k
ipa_to_ansible = {v: k for k, v in ansible_to_ipa.items()}
unmodifiable_after_creation = ['otptype', 'secretkey', 'algorithm',
'digits', 'offset', 'interval', 'counter']

View file

@ -847,8 +847,11 @@ def main():
# Keycloak API expects config parameters to be arrays containing a single string element
if config is not None:
module.params['config'] = dict((k, [str(v).lower() if not isinstance(v, str) else v])
for k, v in config.items() if config[k] is not None)
module.params['config'] = {
k: [str(v).lower() if not isinstance(v, str) else v]
for k, v in config.items()
if config[k] is not None
}
if mappers is not None:
for mapper in mappers:

View file

@ -683,11 +683,11 @@ class LxcContainerManagement(object):
variables.pop(v, None)
false_values = BOOLEANS_FALSE.union([None, ''])
result = dict(
(v, self.module.params[k])
result = {
v: self.module.params[k]
for k, v in variables.items()
if self.module.params[k] not in false_values
)
}
return result
def _config(self):

View file

@ -1032,7 +1032,7 @@ class ProxmoxKvmAnsible(ProxmoxAnsible):
# Sanitize kwargs. Remove not defined args and ensure True and False converted to int.
kwargs = {k: v for k, v in kwargs.items() if v is not None}
kwargs.update(dict([k, int(v)] for k, v in kwargs.items() if isinstance(v, bool)))
kwargs.update({k: int(v) for k, v in kwargs.items() if isinstance(v, bool)})
version = self.version()
pve_major_version = 3 if version < LooseVersion('4.0') else version.version[0]
@ -1163,7 +1163,7 @@ class ProxmoxKvmAnsible(ProxmoxAnsible):
for param in valid_clone_params:
if self.module.params[param] is not None:
clone_params[param] = self.module.params[param]
clone_params.update(dict([k, int(v)] for k, v in clone_params.items() if isinstance(v, bool)))
clone_params.update({k: int(v) for k, v in clone_params.items() if isinstance(v, bool)})
taskid = proxmox_node.qemu(vmid).clone.post(newid=newid, name=name, **clone_params)
else:
taskid = proxmox_node.qemu.create(vmid=vmid, name=name, memory=memory, cpu=cpu, cores=cores, sockets=sockets, **kwargs)

View file

@ -135,11 +135,11 @@ from uuid import uuid4
def payload_from_security_group(security_group):
return dict(
(k, v)
return {
k: v
for k, v in security_group.items()
if k != 'id' and v is not None
)
}
def present_strategy(api, security_group):

View file

@ -446,7 +446,7 @@ def main():
params = module.params
commands = dict((key, params[key]) for key in command_keys if params[key])
commands = {key: params[key] for key in command_keys if params[key]}
# Ensure ufw is available
ufw_bin = module.get_bin_path('ufw', True)

View file

@ -558,9 +558,11 @@ def create_payload(module, uuid):
# Filter out the few options that are not valid VM properties.
module_options = ['force', 'state']
# @TODO make this a simple {} comprehension as soon as py2 is ditched
# @TODO {k: v for k, v in p.items() if k not in module_options}
vmdef = dict([(k, v) for k, v in module.params.items() if k not in module_options and v])
vmdef = {
k: v
for k, v in module.params.items()
if k not in module_options and v
}
try:
vmdef_json = json.dumps(vmdef)