(fp, allowQP=True)
Iterate through a stream, yielding one logical line at a time.

    Because many applications still use vCard 2.1, we have to deal with the
    quoted-printable encoding for long lines, as well as the vCard 3.0 and
    vCalendar line folding technique, a whitespace character at the start
    of the line.

    Quoted-printable data will be decoded in the Behavior decoding phase.

    # We're leaving this test in for awhile, because the unittest was ugly and dumb.
    >>> from six import StringIO
    >>> f=StringIO(testLines)
    >>> for n, l in enumerate(getLogicalLines(f)):
    ...     print("Line %s: %s" % (n, l[0]))
    ...
    Line 0: Line 0 text, Line 0 continued.
    Line 1: Line 1;encoding=quoted-printable:this is an evil=
     evil=
     format.
    Line 2: Line 2 is a new line, it does not start with whitespace.
Iterate through a stream, yielding one logical line at a time.
Iterate through a stream yielding one logical line at a time .
def getLogicalLines(fp, allowQP=True):
    """
    Iterate through a stream, yielding one logical line at a time.

    Because many applications still use vCard 2.1, we have to deal with the
    quoted-printable encoding for long lines, as well as the vCard 3.0 and
    vCalendar line folding technique, a whitespace character at the start
    of the line.

    Quoted-printable data will be decoded in the Behavior decoding phase.

    # We're leaving this test in for awhile, because the unittest was ugly and dumb.
    >>> from six import StringIO
    >>> f=StringIO(testLines)
    >>> for n, l in enumerate(getLogicalLines(f)):
    ...     print("Line %s: %s" % (n, l[0]))
    ...
    Line 0: Line 0 text, Line 0 continued.
    Line 1: Line 1;encoding=quoted-printable:this is an evil=
     evil=
     format.
    Line 2: Line 2 is a new line, it does not start with whitespace.
    """
    if not allowQP:
        val = fp.read(-1)

        lineNumber = 1
        for match in logical_lines_re.finditer(val):
            line, n = wrap_re.subn('', match.group())
            if line != '':
                yield line, lineNumber
            lineNumber += n

    else:
        quotedPrintable = False
        newbuffer = six.StringIO
        logicalLine = newbuffer()
        lineNumber = 0
        lineStartNumber = 0
        while True:
            line = fp.readline()
            if line == '':
                break
            else:
                line = line.rstrip(CRLF)
                lineNumber += 1
            if line.rstrip() == '':
                if logicalLine.tell() > 0:
                    yield logicalLine.getvalue(), lineStartNumber
                lineStartNumber = lineNumber
                logicalLine = newbuffer()
                quotedPrintable = False
                continue

            if quotedPrintable and allowQP:
                logicalLine.write('\n')
                logicalLine.write(line)
                quotedPrintable = False
            elif line[0] in SPACEORTAB:
                logicalLine.write(line[1:])
            elif logicalLine.tell() > 0:
                yield logicalLine.getvalue(), lineStartNumber
                lineStartNumber = lineNumber
                logicalLine = newbuffer()
                logicalLine.write(line)
            else:
                logicalLine = newbuffer()
                logicalLine.write(line)

            # vCard 2.1 allows parameters to be encoded without a parameter name
            # False positives are unlikely, but possible.
            val = logicalLine.getvalue()
            if val[-1]=='=' and val.lower().find('quoted-printable') >= 0:
                quotedPrintable=True

        if logicalLine.tell() > 0:
            yield logicalLine.getvalue(), lineStartNumber
def getLogicalLines ( fp , allowQP = True ) : if not allowQP : val = fp . read ( - 1 ) lineNumber = 1 for match in logical_lines_re . finditer ( val ) : line , n = wrap_re . subn ( '' , match . group ( ) ) if line != '' : yield line , lineNumber lineNumber += n else : quotedPrintable = False newbuffer = six . StringIO logicalLine = newbuffer ( ) lineNumber = 0 lineStartNumber = 0 while True : line = fp . readline ( ) if line == '' : break else : line = line . rstrip ( CRLF ) lineNumber += 1 if line . rstrip ( ) == '' : if logicalLine . tell ( ) > 0 : yield logicalLine . getvalue ( ) , lineStartNumber lineStartNumber = lineNumber logicalLine = newbuffer ( ) quotedPrintable = False continue if quotedPrintable and allowQP : logicalLine . write ( '\n' ) logicalLine . write ( line ) quotedPrintable = False elif line [ 0 ] in SPACEORTAB : logicalLine . write ( line [ 1 : ] ) elif logicalLine . tell ( ) > 0 : yield logicalLine . getvalue ( ) , lineStartNumber lineStartNumber = lineNumber logicalLine = newbuffer ( ) logicalLine . write ( line ) else : logicalLine = newbuffer ( ) logicalLine . write ( line ) # vCard 2.1 allows parameters to be encoded without a parameter name # False positives are unlikely, but possible. val = logicalLine . getvalue ( ) if val [ - 1 ] == '=' and val . lower ( ) . find ( 'quoted-printable' ) >= 0 : quotedPrintable = True if logicalLine . tell ( ) > 0 : yield logicalLine . getvalue ( ) , lineStartNumber