diff --git a/cyruslib.py b/cyruslib.py --- a/cyruslib.py +++ b/cyruslib.py @@ -89,6 +89,26 @@ elif not isinstance(s, (six.text_type, six.binary_type)): raise TypeError("not expecting type '%s'" % type(s)) return s +def ensure_binary(s, encoding='utf-8', errors='strict'): + """Coerce **s** to six.binary_type. + For Python 2: + - `unicode` -> encoded to `str` + - `str` -> `str` + For Python 3: + - `str` -> encoded to `bytes` + - `bytes` -> `bytes` + + Copied from six (not available < 1.12), and extended with list support. + """ + if isinstance(s, list): + + #FIXME pass encoding + return list(map(ensure_binary, s)) + if isinstance(s, six.binary_type): + return s + if isinstance(s, six.text_type): + return s.encode(encoding, errors) + raise TypeError("not expecting type '%s'" % type(s)) def ok(res): return res.upper().startswith('OK') @@ -231,7 +251,7 @@ return ok(res), dat[0] def getannotation(self, mailbox, pattern='*', shared=None): - if shared == None: + if shared is None: typ, dat = self._simple_command('GETANNOTATION', mailbox, quote(pattern), quote('*')) elif shared: typ, dat = self._simple_command('GETANNOTATION', mailbox, quote(pattern), quote('value.shared')) @@ -335,7 +355,7 @@ return ok(res), dat[0] def getannotation(self, mailbox, pattern='*', shared=None): - if shared == None: + if shared is None: typ, dat = self._simple_command('GETANNOTATION', mailbox, quote(pattern), quote('*')) elif shared: typ, dat = self._simple_command('GETANNOTATION', mailbox, quote(pattern), quote('value.shared')) @@ -651,7 +671,7 @@ elif self.ENCODING in self.ENCODING_LIST: return self.__decode(text) - def lm(self, pattern="*"): + def lm(self, pattern=b"*"): """ List mailboxes, returns dict with list of mailboxes @@ -665,8 +685,8 @@ """ self.__prepare('LIST') - if pattern == '': pattern = "*" - if pattern == '%': + if pattern == b'': pattern = b"*" + if pattern == b'%': res, ml = self.__docommand('list', '', '%') else: res, ml = self.__docommand('list', '""', self.decode(pattern)) @@ -706,7 +726,8 @@ def dm(self, mailbox, recursive=True): """Delete mailbox""" self.__prepare('DELETE', mailbox) - mbxTmp = mailbox.split(self.SEP) + SEPARATOR = ensure_binary(self.SEP) + mbxTmp = mailbox.split(SEPARATOR) # Cyrus is not recursive for user subfolders and global folders if (recursive and mbxTmp[0] != "user") or (len(mbxTmp) > 2): mbxList = self.lm("%s%s*" % (mailbox, self.SEP)) @@ -816,7 +837,6 @@ # calling programs should be fixed instead. res, data = self.__docommand("getmetadata", self.decode(mailbox), pattern, shared) - if (len(data) == 1) and data[0] is None: self.__verbose( '[GETMETADATA %s] No results' % (mailbox) ) return {} @@ -901,7 +921,7 @@ self.__prepare('LSUB') if pattern == '': pattern = "*" res, ml = self.__docommand('lsub', '*', pattern) - + ml = ensure_str(ml) if not ok(res): self.__verbose( '[LIST] %s: %s' % (res, ml) ) return []