diff --git a/cyruslib.py b/cyruslib.py --- a/cyruslib.py +++ b/cyruslib.py @@ -87,6 +87,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') @@ -172,7 +192,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')) @@ -260,7 +280,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')) @@ -541,7 +561,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 @@ -555,8 +575,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)) @@ -596,7 +616,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)) @@ -699,14 +720,14 @@ """Get Annotation""" self.__prepare('GETANNOTATION') res, data = self.__docommand('getannotation', self.decode(mailbox), pattern) - + data = ensure_str(data) if (len(data) == 1) and data[0] is None: self.__verbose( '[GETANNOTATION %s] No results' % (mailbox) ) return {} ann = {} annotations = [] - empty_values = [ "NIL", '" "', None, '', ' ' ] + empty_values = ["NIL", '" "', None, '', ' '] concat_items = [] for item in data: @@ -816,10 +837,10 @@ if value_shared in empty_values: value_shared = None - if not value_priv == None: + if value_priv is not None: ann[folder]['/private' + key] = value_priv - if not value_shared == None: + if value_shared is not None: ann[folder]['/shared' + key] = value_shared return ann @@ -853,7 +874,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 []