MH deco: minor refactor (#8766)

* MH deco: minor refactor

* adjustments and improvement in test

* sanity fix

* use func.__self__

* simplify use of self

* add changelog frag
This commit is contained in:
Alexei Znamensky 2024-08-18 01:20:00 +12:00 committed by GitHub
parent 14e86bde07
commit c84fb5577b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 41 additions and 37 deletions

View file

@ -13,23 +13,26 @@ from functools import wraps
from ansible_collections.community.general.plugins.module_utils.mh.exceptions import ModuleHelperException
def cause_changes(on_success=None, on_failure=None):
def cause_changes(on_success=None, on_failure=None, when=None):
def deco(func):
if on_success is None and on_failure is None:
return func
@wraps(func)
def wrapper(*args, **kwargs):
def wrapper(self, *args, **kwargs):
try:
self = args[0]
func(*args, **kwargs)
func(self, *args, **kwargs)
if on_success is not None:
self.changed = on_success
elif when == "success":
self.changed = True
except Exception:
if on_failure is not None:
self.changed = on_failure
elif when == "failure":
self.changed = True
raise
finally:
if when == "always":
self.changed = True
return wrapper
@ -50,8 +53,6 @@ def module_fails_on_exception(func):
try:
func(self, *args, **kwargs)
except SystemExit:
raise
except ModuleHelperException as e:
if e.update_output:
self.update_output(e.update_output)
@ -73,6 +74,7 @@ def check_mode_skip(func):
def wrapper(self, *args, **kwargs):
if not self.module.check_mode:
return func(self, *args, **kwargs)
return wrapper
@ -87,7 +89,7 @@ def check_mode_skip_returns(callable=None, value=None):
return func(self, *args, **kwargs)
return wrapper_callable
if value is not None:
else:
@wraps(func)
def wrapper_value(self, *args, **kwargs):
if self.module.check_mode:
@ -95,7 +97,4 @@ def check_mode_skip_returns(callable=None, value=None):
return func(self, *args, **kwargs)
return wrapper_value
if callable is None and value is None:
return check_mode_skip
return deco