1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-05-03 08:52:55 +00:00

use f-strings in module utils (#10901)

* use f-strings in module utils

* Apply suggestions from code review

Co-authored-by: Felix Fontein <felix@fontein.de>

* remove unused imports

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
Alexei Znamensky 2025-10-11 22:43:43 +13:00 committed by GitHub
parent 74b6a0294a
commit b85e263466
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
51 changed files with 270 additions and 382 deletions

View file

@ -40,7 +40,7 @@ class ModuleHelperBase(object):
def __getattr__(self, attr):
if attr in self._delegated_to_module:
return getattr(self.module, attr)
raise AttributeError("ModuleHelperBase has no attribute '%s'" % (attr, ))
raise AttributeError(f"ModuleHelperBase has no attribute '{attr}'")
def __init_module__(self):
pass

View file

@ -38,7 +38,7 @@ 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
return k if k not in conflict_list else f"_{k}"
def fix_var_conflicts(output):
result = {fix_key(k): v for k, v in output.items()}
@ -56,7 +56,7 @@ def module_fails_on_exception(func):
except Exception as e:
# patchy solution to resolve conflict with output variables
output = fix_var_conflicts(self.output)
msg = "Module failed with exception: {0}".format(str(e).strip())
msg = f"Module failed with exception: {str(e).strip()}"
self.module.fail_json(msg=msg, exception=traceback.format_exc(),
output=self.output, vars=self.vars.output(), **output)
return wrapper

View file

@ -10,7 +10,7 @@ from ansible.module_utils.common.text.converters import to_native
class ModuleHelperException(Exception):
def __init__(self, msg, update_output=None, *args, **kwargs):
self.msg = to_native(msg or "Module failed with exception: {0}".format(self))
self.msg = to_native(msg or f"Module failed with exception: {self}")
if update_output is None:
update_output = {}
self.update_output = update_output

View file

@ -15,7 +15,7 @@ class DeprecateAttrsMixin(object):
if target is None:
target = self
if not hasattr(target, attr):
raise ValueError("Target {0} has no attribute {1}".format(target, attr))
raise ValueError(f"Target {target} has no attribute {attr}")
if module is None:
if isinstance(target, AnsibleModule):
module = target
@ -57,4 +57,4 @@ class DeprecateAttrsMixin(object):
# override attribute
prop = property(_getter)
setattr(target, attr, prop)
setattr(target, "_{0}_setter".format(attr), prop.setter(_setter))
setattr(target, f"_{attr}_setter", prop.setter(_setter))

View file

@ -15,7 +15,7 @@ class StateMixin(object):
return self.default_state if state is None else state
def _method(self, state):
return "{0}_{1}".format(self.state_param, state)
return f"{self.state_param}_{state}"
def __run__(self):
state = self._state()
@ -35,4 +35,4 @@ class StateMixin(object):
return func()
def __state_fallback__(self):
raise ValueError("Cannot find method: {0}".format(self._method(self._state())))
raise ValueError(f"Cannot find method: {self._method(self._state())}")