def fileopen(fun):
    """ Open read-only filehandle if first argument is string

    This function can be used to decorate functions that expect a filehandle as
    the first argument. If the first argument is a string, it is assumed to be
    the path to the input file, and the file is opened in read mode.

    Args:
        fun: A function that expects a read-only filehandle as the first arg.

    Returns:
        function: A function that expects a path (str) OR filehandle as the
            first argument.

    """
    def wrapper(*args, **kwargs):
        if isinstance(args[0], str):
            try:
                fh = open(args[0], 'rU')
                return fun(fh, *args[1:], **kwargs)
            except IOError as e:
                raise e
        elif hasattr(args[0], 'read'):
                return fun(*args, **kwargs)
        else:
            raise IOError("Unknown argument for fileopen '%s'" % str(args[0]))
    return wrapper