1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-02-04 07:51:50 +00:00

Cleanup: use f-strings instead of str.format() (#11017)

Address UP032: use f-strings instead of str.format().
This commit is contained in:
Felix Fontein 2025-11-01 12:04:33 +01:00 committed by GitHub
parent 5d5392786c
commit 3f2213791a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 72 additions and 135 deletions

View file

@ -219,17 +219,13 @@ class DimensionDataVlanModule(DimensionDataModule):
if not vlan:
if self.module.check_mode:
self.module.exit_json(
msg='VLAN "{0}" is absent from network domain "{1}" (should be present).'.format(
self.name, self.network_domain_selector
),
msg=f'VLAN "{self.name}" is absent from network domain "{self.network_domain_selector}" (should be present).',
changed=True
)
vlan = self._create_vlan(network_domain)
self.module.exit_json(
msg='Created VLAN "{0}" in network domain "{1}".'.format(
self.name, self.network_domain_selector
),
msg=f'Created VLAN "{self.name}" in network domain "{self.network_domain_selector}".',
vlan=vlan_to_dict(vlan),
changed=True
)
@ -237,9 +233,7 @@ class DimensionDataVlanModule(DimensionDataModule):
diff = VlanDiff(vlan, self.module.params)
if not diff.has_changes():
self.module.exit_json(
msg='VLAN "{0}" is present in network domain "{1}" (no changes detected).'.format(
self.name, self.network_domain_selector
),
msg=f'VLAN "{self.name}" is present in network domain "{self.network_domain_selector}" (no changes detected).',
vlan=vlan_to_dict(vlan),
changed=False
)
@ -250,25 +244,19 @@ class DimensionDataVlanModule(DimensionDataModule):
diff.ensure_legal_change()
except InvalidVlanChangeError as invalid_vlan_change:
self.module.fail_json(
msg='Unable to update VLAN "{0}" in network domain "{1}": {2}'.format(
self.name, self.network_domain_selector, invalid_vlan_change
)
msg=f'Unable to update VLAN "{self.name}" in network domain "{self.network_domain_selector}": {invalid_vlan_change}'
)
if diff.needs_expand() and not self.allow_expand:
self.module.fail_json(
msg='The configured private IPv4 network size ({0}-bit prefix) for '.format(
self.private_ipv4_prefix_size
) + 'the VLAN differs from its current network size ({0}-bit prefix) '.format(
vlan.private_ipv4_range_size
) + 'and needs to be expanded. Use allow_expand=true if this is what you want.'
msg=f'The configured private IPv4 network size ({self.private_ipv4_prefix_size}-bit prefix) for '
f'the VLAN differs from its current network size ({vlan.private_ipv4_range_size}-bit prefix) '
'and needs to be expanded. Use allow_expand=true if this is what you want.'
)
if self.module.check_mode:
self.module.exit_json(
msg='VLAN "{0}" is present in network domain "{1}" (changes detected).'.format(
self.name, self.network_domain_selector
),
msg=f'VLAN "{self.name}" is present in network domain "{self.network_domain_selector}" (changes detected).',
vlan=vlan_to_dict(vlan),
changed=True
)
@ -284,9 +272,7 @@ class DimensionDataVlanModule(DimensionDataModule):
self.driver.ex_expand_vlan(vlan)
self.module.exit_json(
msg='Updated VLAN "{0}" in network domain "{1}".'.format(
self.name, self.network_domain_selector
),
msg=f'Updated VLAN "{self.name}" in network domain "{self.network_domain_selector}".',
vlan=vlan_to_dict(vlan),
changed=True
)
@ -306,9 +292,7 @@ class DimensionDataVlanModule(DimensionDataModule):
)
else:
self.module.fail_json(
msg='VLAN "{0}" does not exist in network domain "{1}".'.format(
self.name, self.network_domain_selector
)
msg=f'VLAN "{self.name}" does not exist in network domain "{self.network_domain_selector}".'
)
def state_absent(self):
@ -321,9 +305,7 @@ class DimensionDataVlanModule(DimensionDataModule):
vlan = self._get_vlan(network_domain)
if not vlan:
self.module.exit_json(
msg='VLAN "{0}" is absent from network domain "{1}".'.format(
self.name, self.network_domain_selector
),
msg=f'VLAN "{self.name}" is absent from network domain "{self.network_domain_selector}".',
changed=False
)
@ -331,9 +313,7 @@ class DimensionDataVlanModule(DimensionDataModule):
if self.module.check_mode:
self.module.exit_json(
msg='VLAN "{0}" is present in network domain "{1}" (should be absent).'.format(
self.name, self.network_domain_selector
),
msg=f'VLAN "{self.name}" is present in network domain "{self.network_domain_selector}" (should be absent).',
vlan=vlan_to_dict(vlan),
changed=True
)
@ -341,9 +321,7 @@ class DimensionDataVlanModule(DimensionDataModule):
self._delete_vlan(vlan)
self.module.exit_json(
msg='Deleted VLAN "{0}" from network domain "{1}".'.format(
self.name, self.network_domain_selector
),
msg=f'Deleted VLAN "{self.name}" from network domain "{self.network_domain_selector}".',
changed=True
)
@ -390,9 +368,7 @@ class DimensionDataVlanModule(DimensionDataModule):
except DimensionDataAPIException as api_exception:
self.module.fail_json(
msg='Failed to delete VLAN "{0}" due to unexpected error from the CloudControl API: {1}'.format(
vlan.id, api_exception.msg
)
msg=f'Failed to delete VLAN "{vlan.id}" due to unexpected error from the CloudControl API: {api_exception.msg}'
)
def _wait_for_vlan_state(self, vlan, state_to_wait_for):
@ -444,9 +420,7 @@ class DimensionDataVlanModule(DimensionDataModule):
)
except UnknownNetworkError:
self.module.fail_json(
msg='Cannot find network domain "{0}" in datacenter "{1}".'.format(
self.network_domain_selector, self.location
)
msg=f'Cannot find network domain "{self.network_domain_selector}" in datacenter "{self.location}".'
)
return None

View file

@ -205,7 +205,7 @@ def sensu_check(module, path, name, state='present', backup=False):
else:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
except ValueError:
msg = '{path} contains invalid JSON'.format(path=path)
msg = f'{path} contains invalid JSON'
module.fail_json(msg=msg)
finally:
if stream:
@ -255,36 +255,36 @@ def sensu_check(module, path, name, state='present', backup=False):
if opt not in check or check[opt] != module.params[opt]:
check[opt] = module.params[opt]
changed = True
reasons.append('`{opt}\' did not exist or was different'.format(opt=opt))
reasons.append(f'`{opt}\' did not exist or was different')
else:
if opt in check:
del check[opt]
changed = True
reasons.append('`{opt}\' was removed'.format(opt=opt))
reasons.append(f'`{opt}\' was removed')
if module.params['custom']:
# Convert to json
custom_params = module.params['custom']
overwrited_fields = set(custom_params.keys()) & set(simple_opts + ['type', 'subdue', 'subdue_begin', 'subdue_end'])
if overwrited_fields:
msg = 'You can\'t overwriting standard module parameters via "custom". You are trying overwrite: {opt}'.format(opt=list(overwrited_fields))
msg = f'You can\'t overwriting standard module parameters via "custom". You are trying overwrite: {list(overwrited_fields)}'
module.fail_json(msg=msg)
for k, v in custom_params.items():
if k in config['checks'][name]:
if not config['checks'][name][k] == v:
changed = True
reasons.append('`custom param {opt}\' was changed'.format(opt=k))
reasons.append(f'`custom param {k}\' was changed')
else:
changed = True
reasons.append('`custom param {opt}\' was added'.format(opt=k))
reasons.append(f'`custom param {k}\' was added')
check[k] = v
simple_opts += custom_params.keys()
# Remove obsolete custom params
for opt in set(config['checks'][name].keys()) - set(simple_opts + ['type', 'subdue', 'subdue_begin', 'subdue_end']):
changed = True
reasons.append('`custom param {opt}\' was deleted'.format(opt=opt))
reasons.append(f'`custom param {opt}\' was deleted')
del check[opt]
if module.params['metric']:

View file

@ -208,12 +208,12 @@ def main():
if state == 'absent':
if os.path.exists(path):
if module.check_mode:
msg = '{path} would have been deleted'.format(path=path)
msg = f'{path} would have been deleted'
module.exit_json(msg=msg, changed=True)
else:
try:
os.remove(path)
msg = '{path} deleted successfully'.format(path=path)
msg = f'{path} deleted successfully'
module.exit_json(msg=msg, changed=True)
except OSError as e:
msg = 'Exception when trying to delete {path}: {exception}'
@ -221,7 +221,7 @@ def main():
msg=msg.format(path=path, exception=str(e)))
else:
# Idempotency: it is okay if the file doesn't exist
msg = '{path} already does not exist'.format(path=path)
msg = f'{path} already does not exist'
module.exit_json(msg=msg)
# Build client configuration from module arguments
@ -253,8 +253,7 @@ def main():
try:
os.makedirs(os.path.dirname(path))
except OSError as e:
module.fail_json(msg='Unable to create {0}: {1}'.format(os.path.dirname(path),
str(e)))
module.fail_json(msg=f'Unable to create {os.path.dirname(path)}: {e}')
if module.check_mode:
module.exit_json(msg='Client configuration would have been updated',
@ -270,8 +269,7 @@ def main():
config=config['client'],
file=path)
except (OSError, IOError) as e:
module.fail_json(msg='Unable to write file {0}: {1}'.format(path,
str(e)))
module.fail_json(msg=f'Unable to write file {path}: {e}')
if __name__ == '__main__':

View file

@ -208,17 +208,17 @@ def main():
state = module.params['state']
name = module.params['name']
path = '/etc/sensu/conf.d/handlers/{0}.json'.format(name)
path = f'/etc/sensu/conf.d/handlers/{name}.json'
if state == 'absent':
if os.path.exists(path):
if module.check_mode:
msg = '{path} would have been deleted'.format(path=path)
msg = f'{path} would have been deleted'
module.exit_json(msg=msg, changed=True)
else:
try:
os.remove(path)
msg = '{path} deleted successfully'.format(path=path)
msg = f'{path} deleted successfully'
module.exit_json(msg=msg, changed=True)
except OSError as e:
msg = 'Exception when trying to delete {path}: {exception}'
@ -226,7 +226,7 @@ def main():
msg=msg.format(path=path, exception=str(e)))
else:
# Idempotency: it is okay if the file doesn't exist
msg = '{path} already does not exist'.format(path=path)
msg = f'{path} already does not exist'
module.exit_json(msg=msg)
# Build handler configuration from module arguments
@ -259,8 +259,7 @@ def main():
try:
os.makedirs(os.path.dirname(path))
except OSError as e:
module.fail_json(msg='Unable to create {0}: {1}'.format(os.path.dirname(path),
str(e)))
module.fail_json(msg=f'Unable to create {os.path.dirname(path)}: {e}')
if module.check_mode:
module.exit_json(msg='Handler configuration would have been updated',
@ -278,8 +277,7 @@ def main():
file=path,
name=name)
except (OSError, IOError) as e:
module.fail_json(msg='Unable to write file {0}: {1}'.format(path,
str(e)))
module.fail_json(msg=f'Unable to write file {path}: {e}')
if __name__ == '__main__':

View file

@ -93,7 +93,7 @@ def sensu_subscription(module, path, name, state='present', backup=False):
else:
module.fail_json(msg=to_native(e), exception=traceback.format_exc())
except ValueError:
msg = '{path} contains invalid JSON'.format(path=path)
msg = f'{path} contains invalid JSON'
module.fail_json(msg=msg)
if 'client' not in config: