import functools


def check_communicating(func):
    """
    Decorator that makes a method first check that there is a connection
    to the component, before allowing the wrapped function to proceed

    :param func: the wrapped function

    :return: the wrapped function
    """

    @functools.wraps(func)
    def _wrapper(component_manager, *args, **kwargs):
        """
        Wrapper function that checks that there is a connection to the
        component before invoking the wrapped function

        :param component_manager: the component_manager to check
        :param args: positional arguments to the wrapped function
        :param kwargs: keyword arguments to the wrapped function

        :return: whatever the wrapped function returns
        """
        if not component_manager.is_communicating:
            raise ConnectionError("Not connected")
        return func(component_manager, *args, **kwargs)

    return _wrapper