import functools


def require_format(required_format):
    """Method decorator, raises a ValueError when the decorated method
    is called if the default format is not set to ``required_format``.
    """

    def decorator(method):
        @functools.wraps(method)
        def decorated(self, *args, **kwargs):
            # If everything is okay, call the method
            if self.default_format == required_format:
                return method(self, *args, **kwargs)

            # Otherwise raise an exception
            msg = 'Function %s requires that you use ' \
                  'ElementTree ("etree") as the communication format, ' \
                  'while the current format is set to "%s".'
            raise ValueError(msg % (method.__name__, self.default_format))

        return decorated

    return decorator