1import logging
2import time
3
4log = logging.getLogger(__name__)
5
6# To give us some leeway when making time-calculations
7BLUR_FACTOR = 0.95
8
9
10def wait_for(func, timeout=10, step=1, default=None, func_args=(), func_kwargs=None):
11    """
12    Call `func` at regular intervals and Waits until the given function returns
13    a truthy value within the given timeout and returns that value.
14
15    @param func:
16    @type func: function
17    @param timeout:
18    @type timeout: int | float
19    @param step: Interval at which we should check for the value
20    @type step: int | float
21    @param default: Value that should be returned should `func` not return a truthy value
22    @type default:
23    @param func_args: *args for `func`
24    @type func_args: list | tuple
25    @param func_kwargs: **kwargs for `func`
26    @type func_kwargs: dict
27    @return: `default` or result of `func`
28    """
29    if func_kwargs is None:
30        func_kwargs = dict()
31    max_time = time.time() + timeout
32    # Time moves forward so we might not reenter the loop if we step too long
33    step = min(step or 1, timeout) * BLUR_FACTOR
34
35    ret = default
36    while time.time() <= max_time:
37        call_ret = func(*func_args, **func_kwargs)
38        if call_ret:
39            ret = call_ret
40            break
41        else:
42            time.sleep(step)
43
44            # Don't allow cases of over-stepping the timeout
45            step = min(step, max_time - time.time()) * BLUR_FACTOR
46    if time.time() > max_time:
47        log.warning("Exceeded waiting time (%s seconds) to exectute %s", timeout, func)
48    return ret
49