<s> """<STR_LIT>""" <EOL> __all__ = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> import re <EOL> from string import hexdigits <EOL> from email . utils import fix_eols <EOL> CRLF = '<STR_LIT:\r\n>' <EOL> NL = '<STR_LIT:\n>' <EOL> MISC_LEN = <NUM_LIT:7> <EOL> hqre = re . compile ( r'<STR_LIT>' ) <EOL> bqre = re . compile ( r'<STR_LIT>' ) <EOL> def header_quopri_check ( c ) : <EOL> """<STR_LIT>""" <EOL> return bool ( hqre . match ( c ) ) <EOL> def body_quopri_check ( c ) : <EOL> """<STR_LIT>""" <EOL> return bool ( bqre . match ( c ) ) <EOL> def header_quopri_len ( s ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> for c in s : <EOL> if hqre . match ( c ) : <EOL> count += <NUM_LIT:3> <EOL> else : <EOL> count += <NUM_LIT:1> <EOL> return count <EOL> def body_quopri_len ( str ) : <EOL> """<STR_LIT>""" <EOL> count = <NUM_LIT:0> <EOL> for c in str : <EOL> if bqre . match ( c ) : <EOL> count += <NUM_LIT:3> <EOL> else : <EOL> count += <NUM_LIT:1> <EOL> return count <EOL> def _max_append ( L , s , maxlen , extra = '<STR_LIT>' ) : <EOL> if not L : <EOL> L . append ( s . lstrip ( ) ) <EOL> elif len ( L [ - <NUM_LIT:1> ] ) + len ( s ) <= maxlen : <EOL> L [ - <NUM_LIT:1> ] += extra + s <EOL> else : <EOL> L . append ( s . lstrip ( ) ) <EOL> def unquote ( s ) : <EOL> """<STR_LIT>""" <EOL> return chr ( int ( s [ <NUM_LIT:1> : <NUM_LIT:3> ] , <NUM_LIT:16> ) ) <EOL> def quote ( c ) : <EOL> return "<STR_LIT>" % ord ( c ) <EOL> def header_encode ( header , charset = "<STR_LIT>" , keep_eols = False , <EOL> maxlinelen = <NUM_LIT> , eol = NL ) : <EOL> """<STR_LIT>""" <EOL> if not header : <EOL> return header <EOL> if not keep_eols : <EOL> header = fix_eols ( header ) <EOL> quoted = [ ] <EOL> if maxlinelen is None : <EOL> max_encoded = <NUM_LIT> <EOL> else : <EOL> max_encoded = maxlinelen - len ( charset ) - MISC_LEN - <NUM_LIT:1> <EOL> for c in header : <EOL> if c == '<STR_LIT:U+0020>' : <EOL> _max_append ( quoted , '<STR_LIT:_>' , max_encoded ) <EOL> elif not hqre . match ( c ) : <EOL> _max_append ( quoted , c , max_encoded ) <EOL> else : <EOL> _max_append ( quoted , "<STR_LIT>" % ord ( c ) , max_encoded ) <EOL> joiner = eol + '<STR_LIT:U+0020>' <EOL> return joiner . join ( [ '<STR_LIT>' % ( charset , line ) for line in quoted ] ) <EOL> def encode ( body , binary = False , maxlinelen = <NUM_LIT> , eol = NL ) : <EOL> """<STR_LIT>""" <EOL> if not body : <EOL> return body <EOL> if not binary : <EOL> body = fix_eols ( body ) <EOL> encoded_body = '<STR_LIT>' <EOL> lineno = - <NUM_LIT:1> <EOL> lines = body . splitlines ( <NUM_LIT:1> ) <EOL> for line in lines : <EOL> if line . endswith ( CRLF ) : <EOL> line = line [ : - <NUM_LIT:2> ] <EOL> elif line [ - <NUM_LIT:1> ] in CRLF : <EOL> line = line [ : - <NUM_LIT:1> ] <EOL> lineno += <NUM_LIT:1> <EOL> encoded_line = '<STR_LIT>' <EOL> prev = None <EOL> linelen = len ( line ) <EOL> for j in range ( linelen ) : <EOL> c = line [ j ] <EOL> prev = c <EOL> if bqre . match ( c ) : <EOL> c = quote ( c ) <EOL> elif j + <NUM_LIT:1> == linelen : <EOL> if c not in '<STR_LIT>' : <EOL> encoded_line += c <EOL> prev = c <EOL> continue <EOL> if len ( encoded_line ) + len ( c ) >= maxlinelen : <EOL> encoded_body += encoded_line + '<STR_LIT:=>' + eol <EOL> encoded_line = '<STR_LIT>' <EOL> encoded_line += c <EOL> if prev and prev in '<STR_LIT>' : <EOL> if lineno + <NUM_LIT:1> == len ( lines ) : <EOL> prev = quote ( prev ) <EOL> if len ( encoded_line ) + len ( prev ) > maxlinelen : <EOL> encoded_body += encoded_line + '<STR_LIT:=>' + eol + prev <EOL> else : <EOL> encoded_body += encoded_line + prev <EOL> else : <EOL> encoded_body += encoded_line + prev + '<STR_LIT:=>' + eol <EOL> encoded_line = '<STR_LIT>' <EOL> if lines [ lineno ] . endswith ( CRLF ) or lines [ lineno ] [ - <NUM_LIT:1> ] in CRLF : <EOL> encoded_body += encoded_line + eol <EOL> else : <EOL> encoded_body += encoded_line <EOL> encoded_line = '<STR_LIT>' <EOL> return encoded_body <EOL> body_encode = encode <EOL> encodestring = encode <EOL> def decode ( encoded , eol = NL ) : <EOL> """<STR_LIT>""" <EOL> if not encoded : <EOL> return encoded <EOL> decoded = '<STR_LIT>' <EOL> for line in encoded . splitlines ( ) : <EOL> line = line . rstrip ( ) <EOL> if not line : <EOL> decoded += eol <EOL> continue <EOL> i = <NUM_LIT:0> <EOL> n = len ( line ) <EOL> while i < n : <EOL> c = line [ i ] <EOL> if c != '<STR_LIT:=>' : <EOL> decoded += c <EOL> i += <NUM_LIT:1> <EOL> elif i + <NUM_LIT:1> == n : <EOL> i += <NUM_LIT:1> <EOL> continue <EOL> elif i + <NUM_LIT:2> < n and line [ i + <NUM_LIT:1> ] in hexdigits and line [ i + <NUM_LIT:2> ] in hexdigits : <EOL> decoded += unquote ( line [ i : i + <NUM_LIT:3> ] ) <EOL> i += <NUM_LIT:3> <EOL> else : <EOL> decoded += c <EOL> i += <NUM_LIT:1> <EOL> if i == n : <EOL> decoded += eol <EOL> if not encoded . endswith ( eol ) and decoded . endswith ( eol ) : <EOL> decoded = decoded [ : - <NUM_LIT:1> ] <EOL> return decoded <EOL> body_decode = decode <EOL> decodestring = decode <EOL> def _unquote_match ( match ) : <EOL> """<STR_LIT>""" <EOL> s = match . group ( <NUM_LIT:0> ) <EOL> return unquote ( s ) <EOL> def header_decode ( s ) : <EOL> """<STR_LIT>""" <EOL> s = s . replace ( '<STR_LIT:_>' , '<STR_LIT:U+0020>' ) <EOL> return re . sub ( r'<STR_LIT>' , _unquote_match , s ) </s>
