def encode(input, output, quotetabs, header = <NUM_LIT:0>):
if b2a_qp is not None:<EOL><INDENT>data = input.read()<EOL>odata = b2a_qp(data, quotetabs = quotetabs, header = header)<EOL>output.write(odata)<EOL>return<EOL><DEDENT>def write(s, output=output, lineEnd='<STR_LIT:\n>'):<EOL><INDENT>if s and s[-<NUM_LIT:1>:] in '<STR_LIT>':<EOL><INDENT>output.write(s[:-<NUM_LIT:1>] + quote(s[-<NUM_LIT:1>]) + lineEnd)<EOL><DEDENT>elif s == '<STR_LIT:.>':<EOL><INDENT>output.write(quote(s) + lineEnd)<EOL><DEDENT>else:<EOL><INDENT>output.write(s + lineEnd)<EOL><DEDENT><DEDENT>prevline = None<EOL>while <NUM_LIT:1>:<EOL><INDENT>line = input.readline()<EOL>if not line:<EOL><INDENT>break<EOL><DEDENT>outline = []<EOL>stripped = '<STR_LIT>'<EOL>if line[-<NUM_LIT:1>:] == '<STR_LIT:\n>':<EOL><INDENT>line = line[:-<NUM_LIT:1>]<EOL>stripped = '<STR_LIT:\n>'<EOL><DEDENT>for c in line:<EOL><INDENT>if needsquoting(c, quotetabs, header):<EOL><INDENT>c = quote(c)<EOL><DEDENT>if header and c == '<STR_LIT:U+0020>':<EOL><INDENT>outline.append('<STR_LIT:_>')<EOL><DEDENT>else:<EOL><INDENT>outline.append(c)<EOL><DEDENT><DEDENT>if prevline is not None:<EOL><INDENT>write(prevline)<EOL><DEDENT>thisline = EMPTYSTRING.join(outline)<EOL>while len(thisline) > MAXLINESIZE:<EOL><INDENT>write(thisline[:MAXLINESIZE-<NUM_LIT:1>], lineEnd='<STR_LIT>')<EOL>thisline = thisline[MAXLINESIZE-<NUM_LIT:1>:]<EOL><DEDENT>prevline = thisline<EOL><DEDENT>if prevline is not None:<EOL><INDENT>write(prevline, lineEnd=stripped)<EOL><DEDENT>
Read 'input', apply quoted-printable encoding, and write to 'output'.

    'input' and 'output' are files with readline() and write() methods.
    The 'quotetabs' flag indicates whether embedded tabs and spaces should be
    quoted.  Note that line-ending tabs and spaces are always encoded, as per
    RFC 1521.
    The 'header' flag indicates whether we are encoding spaces as _ as per
    RFC 1522.