import gzip
import bz2


def read_open(input_file, *, binary=False, errors=None):
    """
    Open text file for reading, assuming compression from extension
    :param input_file:
    :return:
    """
    if binary:
        if input_file.endswith(".gz"):
            return gzip.open(input_file, "rb")
        elif input_file.endswith('.bz2'):
            return bz2.open(input_file, "rb")
        else:
            return open(input_file, "rb")
    else:
        if input_file.endswith(".gz"):
            return gzip.open(input_file, "rt", encoding='utf-8', errors=errors)
        elif input_file.endswith('.bz2'):
            return bz2.open(input_file, "rt", encoding='utf-8', errors=errors)
        else:
            return open(input_file, "r", encoding='utf-8', errors=errors)