Use dict comprehension in plugins (#8814)

* use dict comprehension in plugins

* Apply suggestions from code review

* add changelog frag

* fix references in changelog frag
This commit is contained in:
Alexei Znamensky 2024-09-02 06:22:53 +12:00 committed by GitHub
parent 593d302f0b
commit ecc048bc12
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
24 changed files with 81 additions and 45 deletions

View file

@ -286,7 +286,7 @@ class BalancerMember(object):
'hot_standby': 'Stby',
'ignore_errors': 'Ign'}
actual_status = str(self.attributes['Status'])
status = dict((mode, patt in actual_status) for mode, patt in iteritems(status_mapping))
status = {mode: patt in actual_status for mode, patt in iteritems(status_mapping)}
return status
def set_member_status(self, values):

View file

@ -261,7 +261,7 @@ class GitLabGroup(object):
try:
# Filter out None values
filtered = dict((arg_key, arg_value) for arg_key, arg_value in arguments.items() if arg_value is not None)
filtered = {arg_key: arg_value for arg_key, arg_value in arguments.items() if arg_value is not None}
group = self._gitlab.groups.create(filtered)
except (gitlab.exceptions.GitlabCreateError) as e:

View file

@ -1006,7 +1006,7 @@ def main():
# Unfortunately, the ansible argument spec checker introduces variables with null values when
# they are not specified
if client_param == 'protocol_mappers':
new_param_value = [dict((k, v) for k, v in x.items() if x[k] is not None) for x in new_param_value]
new_param_value = [{k: v for k, v in x.items() if v is not None} for x in new_param_value]
elif client_param == 'authentication_flow_binding_overrides':
new_param_value = flow_binding_from_dict_to_model(new_param_value, realm, kc)

View file

@ -428,7 +428,7 @@ def main():
# Unfortunately, the ansible argument spec checker introduces variables with null values when
# they are not specified
if clientscope_param == 'protocol_mappers':
new_param_value = [dict((k, v) for k, v in x.items() if x[k] is not None) for x in new_param_value]
new_param_value = [{k: v for k, v in x.items() if v is not None} for x in new_param_value]
changeset[camel(clientscope_param)] = new_param_value
# Prepare the desired values using the existing values (non-existence results in a dict that is save to use as a basis)

View file

@ -534,7 +534,7 @@ def main():
# special handling of mappers list to allow change detection
if module.params.get('mappers') is not None:
for change in module.params['mappers']:
change = dict((k, v) for k, v in change.items() if change[k] is not None)
change = {k: v for k, v in change.items() if v is not None}
if change.get('id') is None and change.get('name') is None:
module.fail_json(msg='Either `name` or `id` has to be specified on each mapper.')
if before_idp == dict():

View file

@ -724,7 +724,7 @@ from copy import deepcopy
def sanitize(comp):
compcopy = deepcopy(comp)
if 'config' in compcopy:
compcopy['config'] = dict((k, v[0]) for k, v in compcopy['config'].items())
compcopy['config'] = {k: v[0] for k, v in compcopy['config'].items()}
if 'bindCredential' in compcopy['config']:
compcopy['config']['bindCredential'] = '**********'
# an empty string is valid for krbPrincipalAttribute but is filtered out in diff
@ -733,7 +733,7 @@ def sanitize(comp):
if 'mappers' in compcopy:
for mapper in compcopy['mappers']:
if 'config' in mapper:
mapper['config'] = dict((k, v[0]) for k, v in mapper['config'].items())
mapper['config'] = {k: v[0] for k, v in mapper['config'].items()}
return compcopy
@ -886,7 +886,7 @@ def main():
new_param_value = module.params.get(param)
old_value = before_comp[camel(param)] if camel(param) in before_comp else None
if param == 'mappers':
new_param_value = [dict((k, v) for k, v in x.items() if x[k] is not None) for x in new_param_value]
new_param_value = [{k: v for k, v in x.items() if v is not None} for x in new_param_value]
if new_param_value != old_value:
changeset[camel(param)] = new_param_value
@ -895,7 +895,7 @@ def main():
if module.params['provider_id'] in ['kerberos', 'sssd']:
module.fail_json(msg='Cannot configure mappers for {type} provider.'.format(type=module.params['provider_id']))
for change in module.params['mappers']:
change = dict((k, v) for k, v in change.items() if change[k] is not None)
change = {k: v for k, v in change.items() if v is not None}
if change.get('id') is None and change.get('name') is None:
module.fail_json(msg='Either `name` or `id` has to be specified on each mapper.')
if cid is None:

View file

@ -670,7 +670,7 @@ def main():
backupwindow=backupwindow,
)
kwargs = dict((k, v) for k, v in check_items.items() if v is not None)
kwargs = {k: v for k, v in check_items.items() if v is not None}
# setup the auth
try:

View file

@ -616,8 +616,15 @@ class LXDContainerManagement(object):
def _instance_ipv4_addresses(self, ignore_devices=None):
ignore_devices = ['lo'] if ignore_devices is None else ignore_devices
data = (self._get_instance_state_json() or {}).get('metadata', None) or {}
network = dict((k, v) for k, v in (data.get('network', None) or {}).items() if k not in ignore_devices)
addresses = dict((k, [a['address'] for a in v['addresses'] if a['family'] == 'inet']) for k, v in network.items())
network = {
k: v
for k, v in data.get('network', {}).items()
if k not in ignore_devices
}
addresses = {
k: [a['address'] for a in v['addresses'] if a['family'] == 'inet']
for k, v in network.items()
}
return addresses
@staticmethod
@ -748,19 +755,22 @@ class LXDContainerManagement(object):
def run(self):
"""Run the main method."""
def adjust_content(content):
return content if not isinstance(content, dict) else {
k: v for k, v in content.items() if not (self.ignore_volatile_options and k.startswith('volatile.'))
}
try:
if self.trust_password is not None:
self.client.authenticate(self.trust_password)
self.ignore_volatile_options = self.module.params.get('ignore_volatile_options')
self.old_instance_json = self._get_instance_json()
self.old_sections = dict(
(section, content) if not isinstance(content, dict)
else (section, dict((k, v) for k, v in content.items()
if not (self.ignore_volatile_options and k.startswith('volatile.'))))
for section, content in (self.old_instance_json.get('metadata', None) or {}).items()
self.old_sections = {
section: adjust_content(content)
for section, content in self.old_instance_json.get('metadata', {}).items()
if section in set(CONFIG_PARAMS) - set(CONFIG_CREATION_PARAMS)
)
}
self.diff['before']['instance'] = self.old_sections
# preliminary, will be overwritten in _apply_instance_configs() if called

View file

@ -715,7 +715,7 @@ def delete_nulls(h):
if isinstance(h, list):
return [delete_nulls(i) for i in h]
if isinstance(h, dict):
return dict((k, delete_nulls(v)) for k, v in h.items() if v is not None)
return {k: delete_nulls(v) for k, v in h.items() if v is not None}
return h

View file

@ -339,7 +339,7 @@ def get_service_info(module, auth, service):
def create_service(module, auth, template_id, service_name, custom_attrs, unique, wait, wait_timeout):
# make sure that the values in custom_attrs dict are strings
custom_attrs_with_str = dict((k, str(v)) for k, v in custom_attrs.items())
custom_attrs_with_str = {k: str(v) for k, v in custom_attrs.items()}
data = {
"action": {

View file

@ -1559,11 +1559,11 @@ def main():
one_client = pyone.OneServer(auth.url, session=auth.username + ':' + auth.password)
if attributes:
attributes = dict((key.upper(), value) for key, value in attributes.items())
attributes = {key.upper(): value for key, value in attributes.items()}
check_attributes(module, attributes)
if count_attributes:
count_attributes = dict((key.upper(), value) for key, value in count_attributes.items())
count_attributes = {key.upper(): value for key, value in count_attributes.items()}
if not attributes:
import copy
module.warn('When you pass `count_attributes` without `attributes` option when deploying, `attributes` option will have same values implicitly.')

View file

@ -771,7 +771,7 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
)
# Remove all empty kwarg entries
kwargs = dict((key, val) for key, val in kwargs.items() if val is not None)
kwargs = {key: val for key, val in kwargs.items() if val is not None}
if cpus is not None:
kwargs["cpulimit"] = cpus
@ -842,7 +842,7 @@ class ProxmoxLxcAnsible(ProxmoxAnsible):
proxmox_node = self.proxmox_api.nodes(node)
# Remove all empty kwarg entries
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)
kwargs = {k: v for k, v in kwargs.items() if v is not None}
pve_version = self.version()

View file

@ -524,8 +524,11 @@ class ProxmoxDiskAnsible(ProxmoxAnsible):
# - Remove not defined args
# - Ensure True and False converted to int.
# - Remove unnecessary parameters
params = dict((k, v) for k, v in self.module.params.items() if v is not None and k in self.create_update_fields)
params.update(dict((k, int(v)) for k, v in params.items() if isinstance(v, bool)))
params = {
k: int(v) if isinstance(v, bool) else v
for k, v in self.module.params.items()
if v is not None and k in self.create_update_fields
}
return params
def wait_till_complete_or_timeout(self, node_name, task_id):
@ -598,7 +601,7 @@ class ProxmoxDiskAnsible(ProxmoxAnsible):
if iso_image is not None:
playbook_config['volume'] = iso_image
# Values in params are numbers, but strings are needed to compare with disk_config
playbook_config = dict((k, str(v)) for k, v in playbook_config.items())
playbook_config = {k: str(v) for k, v in playbook_config.items()}
# Now compare old and new config to detect if changes are needed
if proxmox_config == playbook_config:
@ -626,7 +629,7 @@ class ProxmoxDiskAnsible(ProxmoxAnsible):
params['format'] = self.module.params['format']
params['delete'] = 1 if self.module.params.get('delete_moved', False) else 0
# Remove not defined args
params = dict((k, v) for k, v in params.items() if v is not None)
params = {k: v for k, v in params.items() if v is not None}
if params.get('storage', False):
disk_config = disk_conf_str_to_dict(vm_config[disk])

View file

@ -970,7 +970,7 @@ class ProxmoxKvmAnsible(ProxmoxAnsible):
self.module.fail_json(msg='Getting information for VM with vmid = %s failed with exception: %s' % (vmid, e))
# Sanitize kwargs. Remove not defined args and ensure True and False converted to int.
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)
kwargs = {k: v for k, v in kwargs.items() if v is not None}
# Convert all dict in kwargs to elements.
# For hostpci[n], ide[n], net[n], numa[n], parallel[n], sata[n], scsi[n], serial[n], virtio[n]
@ -996,7 +996,7 @@ class ProxmoxKvmAnsible(ProxmoxAnsible):
proxmox_node = self.proxmox_api.nodes(node)
# Sanitize kwargs. Remove not defined args and ensure True and False converted to int.
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)
kwargs = {k: v for k, v in kwargs.items() if v is not None}
return proxmox_node.qemu(vmid).config.set(**kwargs) is None
@ -1031,7 +1031,7 @@ class ProxmoxKvmAnsible(ProxmoxAnsible):
proxmox_node = self.proxmox_api.nodes(node)
# Sanitize kwargs. Remove not defined args and ensure True and False converted to int.
kwargs = dict((k, v) for k, v in kwargs.items() if v is not None)
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)))
version = self.version()