import csv


def _sniff_file_info(fname, comment='#', check_header=True, quiet=False):
    """
    Infer number of header rows and delimiter of a file.

    Parameters
    ----------
    fname : string
        CSV file containing the genotype information.
    comment : string, default '#'
        Character that starts a comment row.
    check_header : bool, default True
        If True, check number of header rows, assuming a row
        that begins with a non-digit character is header.
    quiet : bool, default False
        If True, suppress output to screen.

    Returns
    -------
    n_header : int or None
        Number of header rows. None is retured if `check_header`
        is False.
    delimiter : str
        Inferred delimiter
    line : str
        The first line of data in the file.

    Notes
    -----
    .. Valid delimiters are: ['\t', ',', ';', '|', ' ']
    """

    valid_delimiters = ['\t', ',', ';', '|', ' ']

    with open(fname, 'r') as f:
        # Read through comments
        line = f.readline()
        while line != '' and line[0] == comment:
            line = f.readline()

        # Read through header, counting rows
        if check_header:
            n_header = 0
            while line != '' and (not line[0].isdigit()):
                line = f.readline()
                n_header += 1
        else:
            n_header = None

        if line == '':
            delimiter = None
            if not quiet:
                print('Unable to determine delimiter, returning None')
        else:
            # If no tab, comma, ;, |, or space, assume single entry per column
            if not any(d in line for d in valid_delimiters):
                delimiter = None
                if not quiet:
                    print('Unable to determine delimiter, returning None')
            else:
                delimiter = csv.Sniffer().sniff(line).delimiter

    # Return number of header rows and delimiter
    return n_header, delimiter, line