This decorator allows you to make sure that a function is called once and
    only once. Note that recursive functions will still work.

    WARNING: Not thread-safe!!!

def singleton(func):
    """
    This decorator allows you to make sure that a function is called once and
    only once. Note that recursive functions will still work.

    WARNING: Not thread-safe!!!
    """

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        if wrapper.__call_state__ == CALLED:
            return
        ret = func(*args, **kwargs)
        wrapper.__call_state__ = CALLED
        return ret

    def reset():
        wrapper.__call_state__ = NOT_CALLED

    wrapper.reset = reset
    reset()

    # save original func to be able to patch and restore multiple times from
    # unit tests
    wrapper.__original_func = func
    return wrapper