1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
parent ecc048bc12
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

@ -0,0 +1,21 @@
minor_changes:
- credstash lookup plugin - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- keycloak module utils - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- deco MH module utils - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- redfish_utils module utils - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- scaleway module utils - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- etcd3 - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- gitlab_project - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- hwc_ecs_instance - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- hwc_evs_disk - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- hwc_vpc_eip - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- hwc_vpc_peering_connect - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- hwc_vpc_port - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- hwc_vpc_subnet - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- ipa_otptoken - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- keycloak_user_federation - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- lxc_container - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- proxmox_kvm - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- scaleway_security_group - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- ufw - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).
- vmadm - replace Python 2.6 construct with dict comprehensions (https://github.com/ansible-collections/community.general/pull/8822).

View file

@ -120,10 +120,10 @@ class LookupModule(LookupBase):
aws_secret_access_key = self.get_option('aws_secret_access_key')
aws_session_token = self.get_option('aws_session_token')
context = dict(
(k, v) for k, v in kwargs.items()
context = {
k: v for k, v in kwargs.items()
if k not in ('version', 'region', 'table', 'profile_name', 'aws_access_key_id', 'aws_secret_access_key', 'aws_session_token')
)
}
kwargs_pass = {
'profile_name': profile_name,

View file

@ -185,8 +185,7 @@ def get_token(module_params):
'password': auth_password,
}
# Remove empty items, for instance missing client_secret
payload = dict(
(k, v) for k, v in temp_payload.items() if v is not None)
payload = {k: v for k, v in temp_payload.items() if v is not None}
try:
r = json.loads(to_native(open_url(auth_url, method='POST',
validate_certs=validate_certs, http_agent=http_agent, timeout=connection_timeout,

View file

@ -45,11 +45,11 @@ def module_fails_on_exception(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
def fix_key(k):
return k if k not in conflict_list else "_" + k
def fix_var_conflicts(output):
result = dict([
(k if k not in conflict_list else "_" + k, v)
for k, v in output.items()
])
result = {fix_key(k): v for k, v in output.items()}
return result
try:

View file

@ -613,9 +613,11 @@ class RedfishUtils(object):
ai = dict((p['Name'], p)
for p in params if 'Name' in p)
if not ai:
ai = dict((k[:-24],
{'AllowableValues': v}) for k, v in action.items()
if k.endswith('@Redfish.AllowableValues'))
ai = {
k[:-24]: {'AllowableValues': v}
for k, v in action.items()
if k.endswith('@Redfish.AllowableValues')
}
return ai
def _get_allowable_values(self, action, name, default_values=None):
@ -2242,7 +2244,7 @@ class RedfishUtils(object):
continue
# If already set to requested value, remove it from PATCH payload
if data[u'Attributes'][attr_name] == attributes[attr_name]:
if data[u'Attributes'][attr_name] == attr_value:
del attrs_to_patch[attr_name]
warning = ""
@ -2780,9 +2782,11 @@ class RedfishUtils(object):
def virtual_media_insert_via_patch(self, options, param_map, uri, data, image_only=False):
# get AllowableValues
ai = dict((k[:-24],
{'AllowableValues': v}) for k, v in data.items()
if k.endswith('@Redfish.AllowableValues'))
ai = {
k[:-24]: {'AllowableValues': v}
for k, v in data.items()
if k.endswith('@Redfish.AllowableValues')
}
# construct payload
payload = self._insert_virt_media_payload(options, param_map, data, ai)
if 'Inserted' not in payload and not image_only:

View file

@ -51,11 +51,11 @@ def scaleway_waitable_resource_argument_spec():
def payload_from_object(scw_object):
return dict(
(k, v)
return {
k: v
for k, v in scw_object.items()
if k != 'id' and v is not None
)
}
class ScalewayException(Exception):
@ -117,10 +117,7 @@ class SecretVariables(object):
@staticmethod
def list_to_dict(source_list, hashed=False):
key_value = 'hashed_value' if hashed else 'value'
return dict(
(var['key'], var[key_value])
for var in source_list
)
return {var['key']: var[key_value] for var in source_list}
@classmethod
def decode(cls, secrets_list, values_list):

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)