function.imap-utf7-encode

function.imap-utf7-encode

def modified_base64(s):
s_utf7 = s.encode('utf-7') ¶
return s_utf7[1:-1].replace('/', ',') ¶

def modified_unbase64(s): ¶
s_utf7 = '+' + s.replace(',', '/') + '-' ¶
return s_utf7.decode('utf-7') ¶

def encode_imap4_utf7(s, errors=None): ¶
""" ¶
Encode the given C{unicode} string using the IMAP4 specific variation of ¶
UTF-7. ¶

@type s: C{unicode} ¶
@param s: The text to encode. ¶

@param errors: Policy for handling encoding errors. Currently ignored. ¶

@return: C{tuple} of a C{str} giving the encoded bytes and an C{int} ¶
giving the number of code units consumed from the input. ¶
""" ¶
r = [] ¶
_in = [] ¶
for c in s: ¶
if ord(c) in (range(0x20, 0x26) + range(0x27, 0x7f)): ¶
if _in: ¶
r.extend(['&', modified_base64(''.join(_in)), '-']) ¶
del _in[:] ¶
r.append(str(c)) ¶
elif c == '&': ¶
if _in: ¶
r.extend(['&', modified_base64(''.join(_in)), '-']) ¶
del _in[:] ¶
r.append('&-') ¶
else: ¶
_in.append(c) ¶
if _in: ¶
r.extend(['&', modified_base64(''.join(_in)), '-']) ¶
return (''.join(r), len(s)) ¶
<br>&nbsp;&nbsp;&nbsp;&nbsp;s_utf7 = s.encode(\'utf-7\')<br>&nbsp;&nbsp;&nbsp;&nbsp;return s_utf7[1:-1].replace(\'/\', \',\')<br><br>def modified_unbase64(s):<br>&nbsp;&nbsp;&nbsp;&nbsp;s_utf7 = \'+\' + s.replace(\',\', \'/\') + \'-\'<br>&nbsp;&nbsp;&nbsp;&nbsp;return s_utf7.decode(\'utf-7\')<br><br>def encode_imap4_utf7(s, errors=None):<br>&nbsp;&nbsp;&nbsp;&nbsp;"""<br>&nbsp;&nbsp;&nbsp;&nbsp;Encode the given C{unicode} string using the IMAP4 specific variation of<br>&nbsp;&nbsp;&nbsp;&nbsp;UTF-7.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;@type s: C{unicode}<br>&nbsp;&nbsp;&nbsp;&nbsp;@param s: The text to encode.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;@param errors: Policy for handling encoding errors. Currently ignored.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;@return: C{tuple} of a C{str} giving the encoded bytes and an C{int}<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;giving the number of code units consumed from the input.<br>&nbsp;&nbsp;&nbsp;&nbsp;"""<br>&nbsp;&nbsp;&nbsp;&nbsp;r = []<br>&nbsp;&nbsp;&nbsp;&nbsp;_in = []<br>&nbsp;&nbsp;&nbsp;&nbsp;for c in s:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ord(c) in (range(0x20, 0x26) + range(0x27, 0x7f)):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if _in:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.extend([\'&amp;\', modified_base64(\'\'.join(_in)), \'-\'])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;del _in[:]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.append(str(c))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elif c == \'&amp;\':<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if _in:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.extend([\'&amp;\', modified_base64(\'\'.join(_in)), \'-\'])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;del _in[:]<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.append(\'&amp;-\')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_in.append(c)<br>&nbsp;&nbsp;&nbsp;&nbsp;if _in:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r.extend([\'&amp;\', modified_base64(\'\'.join(_in)), \'-\'])<br>&nbsp;&nbsp;&nbsp;&nbsp;return (\'\'.join(r), len(s))