Пример #1
0
def guess_colspec(otpl_reader):
    """
    Note that for guessing to work, the optionally present global enumeration
    column must be placed *before* the (also optional) local enumeration
    column.

    If the input file has a colspec header, that header is used instead of any
    guessing.

    :param otpl_reader: a reader instance
    :type otpl_reader: OtplReader
    :raises AttributeError: if the reader has an undefined separator property
    :returns: a :class:`ColumnSpecification` or ``None`` if the guessing fails
    """
    try:
        guess = _make_guess(otpl_reader)
    except (IOError, UnicodeDecodeError, DataFormatError) as e:
        L.warning(str(e))
        guess = []

    if isinstance(guess, Spec):
        L.info(u'from header: %s', str(guess))
        return guess
    elif len(guess) < 2:
        L.warning(u'failed for "%s"', otpl_reader.path)
        L.debug(u'discarded guess was: %s', Spec.to_string(guess))
        return None
    else:
        L.debug(u'as: %s', Spec.to_string(guess))
        return Spec.from_integers(guess)