import gzip


def get_opener(filename):
    """
    Auto gzip decoder. Returns an open filehandle in binary mode.
    """

    # Can't rewind stdin. Assume plain text.
    if filename == '/dev/stdin':
        return open(filename, 'rb')

    with open(filename, 'rb') as f:
        if f.read(2) == b'\x1f\x8b':
            return gzip.open(filename, 'rb')

    return open(filename, 'rb')