def wait_func_accept_retry_state(wait_func): """Wrap wait function to accept "retry_state" parameter.""" if not six.callable(wait_func): return wait_func if func_takes_retry_state(wait_func): return wait_func if func_takes_last_result(wait_func): @_utils.wraps(wait_func) def wrapped_wait_func(retry_state): warn_about_non_retry_state_deprecation( 'wait', wait_func, stacklevel=4) return wait_func( retry_state.attempt_number, retry_state.seconds_since_start, last_result=retry_state.outcome, ) else: @_utils.wraps(wait_func) def wrapped_wait_func(retry_state): warn_about_non_retry_state_deprecation( 'wait', wait_func, stacklevel=4) return wait_func( retry_state.attempt_number, retry_state.seconds_since_start, ) return wrapped_wait_func
def func_takes_retry_state(func): if not six.callable(func): raise Exception(func) return False if not inspect.isfunction(func) and not inspect.ismethod(func): # func is a callable object rather than a function/method func = func.__call__ func_spec = _utils.getargspec(func) return 'retry_state' in func_spec.args
def func_takes_last_result(waiter): """Check if function has a "last_result" parameter. Needed to provide backward compatibility for wait functions that didn't take "last_result" in the beginning. """ if not six.callable(waiter): return False if not inspect.isfunction(waiter) and not inspect.ismethod(waiter): # waiter is a class, check dunder-call rather than dunder-init. waiter = waiter.__call__ waiter_spec = _utils.getargspec(waiter) return 'last_result' in waiter_spec.args
def retry_error_callback_accept_retry_state(fn): if not six.callable(fn): return fn if func_takes_retry_state(fn): return fn @_utils.wraps(fn) def wrapped_retry_error_callback(retry_state): warn_about_non_retry_state_deprecation( 'retry_error_callback', fn, stacklevel=4) return fn(retry_state.outcome) return wrapped_retry_error_callback
def retry_func_accept_retry_state(retry_func): """Wrap "retry" function to accept "retry_state" parameter.""" if not six.callable(retry_func): return retry_func if func_takes_retry_state(retry_func): return retry_func @_utils.wraps(retry_func) def wrapped_retry_func(retry_state): warn_about_non_retry_state_deprecation( 'retry', retry_func, stacklevel=4) return retry_func(retry_state.outcome) return wrapped_retry_func
def after_func_accept_retry_state(fn): """Wrap "after" function to accept "retry_state".""" if not six.callable(fn): return fn if func_takes_retry_state(fn): return fn @_utils.wraps(fn) def wrapped_after_sleep_func(retry_state): # func, trial_number, trial_time_taken warn_about_non_retry_state_deprecation('after', fn, stacklevel=4) return fn( retry_state.fn, retry_state.attempt_number, retry_state.seconds_since_start) return wrapped_after_sleep_func
def before_func_accept_retry_state(fn): """Wrap "before" function to accept "retry_state".""" if not six.callable(fn): return fn if func_takes_retry_state(fn): return fn @_utils.wraps(fn) def wrapped_before_func(retry_state): # func, trial_number, trial_time_taken warn_about_non_retry_state_deprecation('before', fn, stacklevel=4) return fn( retry_state.fn, retry_state.attempt_number, ) return wrapped_before_func
def stop_func_accept_retry_state(stop_func): """Wrap "stop" function to accept "retry_state" parameter.""" if not six.callable(stop_func): return stop_func if func_takes_retry_state(stop_func): return stop_func @_utils.wraps(stop_func) def wrapped_stop_func(retry_state): warn_about_non_retry_state_deprecation( 'stop', stop_func, stacklevel=4) return stop_func( retry_state.attempt_number, retry_state.seconds_since_start, ) return wrapped_stop_func
def before_sleep_func_accept_retry_state(fn): """Wrap "before_sleep" function to accept "retry_state".""" if not six.callable(fn): return fn if func_takes_retry_state(fn): return fn @_utils.wraps(fn) def wrapped_before_sleep_func(retry_state): # retry_object, sleep, last_result warn_about_non_retry_state_deprecation( 'before_sleep', fn, stacklevel=4) return fn( retry_state.retry_object, sleep=getattr(retry_state.next_action, 'sleep'), last_result=retry_state.outcome) return wrapped_before_sleep_func