root/imapclient/imap_utf7.py

Revision 148:efee6e971771, 2.8 KB (checked in by Menno Smits <menno@…>, 21 months ago)

Always return folder names as unicode for consistency.

Otherwise sometimes the folder names are unicode and sometimes they
are strs. This is good practice for Python 3.

Line 
1# The contents of this file has been derived code from the Twisted project
2# (http://twistedmatrix.com/). The original author is Jp Calderone.
3
4# Twisted project license follows:
5
6# Permission is hereby granted, free of charge, to any person obtaining
7# a copy of this software and associated documentation files (the
8# "Software"), to deal in the Software without restriction, including
9# without limitation the rights to use, copy, modify, merge, publish,
10# distribute, sublicense, and/or sell copies of the Software, and to
11# permit persons to whom the Software is furnished to do so, subject to
12# the following conditions:
13#
14# The above copyright notice and this permission notice shall be
15# included in all copies or substantial portions of the Software.
16#
17# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25
26class FolderNameError(ValueError):
27    pass
28
29
30def encode(s):
31    if isinstance(s, str) and sum(n for n in (ord(c) for c in s) if n > 127):
32        raise FolderNameError("%r contains characters not valid in a str folder name. "
33                              "Convert to unicode first?" % s)
34
35    r = []
36    _in = []
37    for c in s:
38        if ord(c) in (range(0x20, 0x26) + range(0x27, 0x7f)):
39            if _in:
40                r.extend(['&', modified_base64(''.join(_in)), '-'])
41                del _in[:]
42            r.append(str(c))
43        elif c == '&':
44            if _in:
45                r.extend(['&', modified_base64(''.join(_in)), '-'])
46                del _in[:]
47            r.append('&-')
48        else:
49            _in.append(c)
50    if _in:
51        r.extend(['&', modified_base64(''.join(_in)), '-'])
52    return ''.join(r)
53
54
55def decode(s):
56    r = []
57    decode = []
58    for c in s:
59        if c == '&' and not decode:
60            decode.append('&')
61        elif c == '-' and decode:
62            if len(decode) == 1:
63                r.append('&')
64            else:
65                r.append(modified_unbase64(''.join(decode[1:])))
66            decode = []
67        elif decode:
68            decode.append(c)
69        else:
70            r.append(c)
71    if decode:
72        r.append(modified_unbase64(''.join(decode[1:])))
73    out = ''.join(r)
74
75    if not isinstance(out, unicode):
76        out = unicode(out, 'latin-1')
77    return out
78
79
80def modified_base64(s):
81    s_utf7 = s.encode('utf-7')
82    return s_utf7[1:-1].replace('/', ',')
83
84
85def modified_unbase64(s):
86    s_utf7 = '+' + s.replace(',', '/') + '-'
87    return s_utf7.decode('utf-7')
Note: See TracBrowser for help on using the browser.