(outbuf, input, lineLength = 75)
Folding line procedure that ensures multi-byte utf-8 sequences are not
    broken across lines

    TO-DO: This all seems odd. Is it still needed, especially in python3?
Folding line procedure that ensures multi-byte utf-8 sequences are not
    broken across lines
Folding line procedure that ensures multi - byte utf - 8 sequences are not broken across lines
def foldOneLine(outbuf, input, lineLength = 75):
    """
    Folding line procedure that ensures multi-byte utf-8 sequences are not
    broken across lines

    TO-DO: This all seems odd. Is it still needed, especially in python3?
    """
    if len(input) < lineLength:
        # Optimize for unfolded line case
        try:
            outbuf.write(bytes(input, 'UTF-8'))
        except Exception:
            # fall back on py2 syntax
            outbuf.write(str_(input))

    else:
        # Look for valid utf8 range and write that out
        start = 0
        written = 0
        counter = 0  # counts line size in bytes
        decoded = to_unicode(input)
        length = len(to_basestring(input))
        while written < length:
            s = decoded[start]  # take one char
            size = len(to_basestring(s))  # calculate it's size in bytes
            if counter + size > lineLength:
                try:
                    outbuf.write(bytes("\r\n ", 'UTF-8'))
                except Exception:
                    # fall back on py2 syntax
                    outbuf.write("\r\n ")

                counter = 1  # one for space

            if str is unicode_type:
                outbuf.write(to_unicode(s))
            else:
                # fall back on py2 syntax
                outbuf.write(s.encode('utf-8'))

            written += size
            counter += size
            start += 1
    try:
        outbuf.write(bytes("\r\n", 'UTF-8'))
    except Exception:
        # fall back on py2 syntax
        outbuf.write("\r\n")
def foldOneLine ( outbuf , input , lineLength = 75 ) : if len ( input ) < lineLength : # Optimize for unfolded line case try : outbuf . write ( bytes ( input , 'UTF-8' ) ) except Exception : # fall back on py2 syntax outbuf . write ( str_ ( input ) ) else : # Look for valid utf8 range and write that out start = 0 written = 0 counter = 0 # counts line size in bytes decoded = to_unicode ( input ) length = len ( to_basestring ( input ) ) while written < length : s = decoded [ start ] # take one char size = len ( to_basestring ( s ) ) # calculate it's size in bytes if counter + size > lineLength : try : outbuf . write ( bytes ( "\r\n " , 'UTF-8' ) ) except Exception : # fall back on py2 syntax outbuf . write ( "\r\n " ) counter = 1 # one for space if str is unicode_type : outbuf . write ( to_unicode ( s ) ) else : # fall back on py2 syntax outbuf . write ( s . encode ( 'utf-8' ) ) written += size counter += size start += 1 try : outbuf . write ( bytes ( "\r\n" , 'UTF-8' ) ) except Exception : # fall back on py2 syntax outbuf . write ( "\r\n" )