import functools


def event_handler(event_type):
    """Call the decorated func on each event of the given type."""
    def event_handler_decorator(func):
        @functools.wraps(func)
        def out_func(scene):
            events = scene.em.get(event_type)
            for event in events:
                func(scene, event)
        return out_func
    return event_handler_decorator


def consumer(event_type):
    """Clear events of the given type after calling the function."""
    def consumer_decorator(func):
        @functools.wraps(func)
        def out_func(scene):
            func(scene)
            scene.em.clear(event_type)
        return out_func
    return consumer_decorator

