diff options
author | Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com> | 2013-05-03 17:57:41 +0200 |
---|---|---|
committer | Jeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com> | 2013-05-03 18:00:22 +0200 |
commit | 4c89c7a579c9afbd860dacae1087cd1c489da645 (patch) | |
tree | 8a4430fdcf8741ea6e24d3811ed666269902e215 /cyruslib.py | |
parent | 3c760fc298ca610e81119b0427b5b5ecb8fd116f (diff) | |
download | pykolab-4c89c7a579c9afbd860dacae1087cd1c489da645.tar.gz |
Rewrite getannotation to be compatible with 2.5
Diffstat (limited to 'cyruslib.py')
-rw-r--r-- | cyruslib.py | 153 |
1 files changed, 114 insertions, 39 deletions
diff --git a/cyruslib.py b/cyruslib.py index 75b636f..460cf82 100644 --- a/cyruslib.py +++ b/cyruslib.py @@ -642,49 +642,124 @@ class CYRUS: """Get Annotation""" self.__prepare('GETANNOTATION') res, data = self.__docommand('getannotation', self.decode(mailbox), pattern) + if (len(data) == 1) and data[0] is None: self.__verbose( '[GETANNOTATION %s] No results' % (mailbox) ) return {} + ann = {} - for annotation in data: - if isinstance(annotation, tuple): - annotation = annotation[0] - self.__verbose( '[GETANNOTATION] RAW %r (length %d)' % (annotation,len(annotation))) - annotation = annotation.split('"') - self.__verbose( '[GETANNOTATION] SPLIT %r (length %d)' % (annotation,len(annotation))) - if not len(annotation) in [ 9, 17, 21, 37 ]: - self.__verbose( '[GETANNOTATION] Invalid annotation entry' ) - continue - mbx = self.encode(annotation[1]) - _key = annotation[3] - - if annotation[5] == "value.shared": - key = "/shared%s" % (_key) - elif annotation[5] == "value.priv": - key = "/private%s" % (_key) - - value = annotation[7] - - self.__verbose( '[GETANNOTATION %s] %s: %s' % (mbx, key, value) ) - if not ann.has_key(mbx): - ann[mbx] = {} - if not ann[mbx].has_key(key): - ann[mbx][key] = value - - if len(annotation) > 21: - # There's another one hidden in here. - if annotation[21] == "value.shared": - key = "/shared%s" % (_key) - elif annotation[21] == "value.priv": - key = "/private%s" % (_key) - - value = annotation[23] - - self.__verbose( '[GETANNOTATION %s] %s: %s' % (mbx, key, value) ) - if not ann.has_key(mbx): - ann[mbx] = {} - if not ann[mbx].has_key(key): - ann[mbx][key] = value + annotations = [] + empty_values = [ "NIL", '" "', None, '', ' ' ] + + concat_items = [] + for item in data: + if isinstance(item, tuple): + item = ' '.join([str(x) for x in item]) + + if len(concat_items) > 0: + concat_items.append(item) + + if ''.join(concat_items).count('(') == ''.join(concat_items).count(')'): + annotations.append(''.join(concat_items)) + concat_items = [] + continue + else: + + if item.count('(') == item.count(')'): + annotations.append(item) + continue + else: + concat_items.append(item) + continue + + for annotation in annotations: + + folder = annotation.split()[0].replace('"','') + + if not ann.has_key(folder): + ann[folder] = {} + + key = annotation.split()[1].replace('"','').replace("'","") + + _annot = annotation.split('(')[1].split(')')[0] + + try: + value_priv = _annot[(_annot.index('"value.priv"')+len('"value.priv"')):_annot.index('"size.priv"')].strip() + except ValueError, errmsg: + value_priv = None + + try: + size_priv = _annot[(_annot.index('"size.priv"')+len('"size.priv"')):].strip().split('"')[1].strip() + try: + value_priv = value_priv[value_priv.index('{%s}' % (size_priv))+len('{%s}' % (size_priv)):].strip() + except Exception, errmsg: + pass + except Exception, errmsg: + pass + + if value_priv in empty_values: + value_priv = None + else: + try: + value_priv = value_priv[:value_priv.index('"content-type.priv"')].strip() + except: + pass + + try: + value_priv = value_priv[:value_priv.index('"modifiedsince.priv"')].strip() + except: + pass + + if value_priv.startswith('"'): + value_priv = value_priv[1:] + + if value_priv.endswith('"'): + value_priv = value_priv[:-1] + + if value_priv in empty_values: + value_priv = None + + try: + value_shared = _annot[(_annot.index('"value.shared"')+len('"value.shared"')):_annot.index('"size.shared"')].strip() + except ValueError, errmsg: + value_shared = None + + try: + size_shared = _annot[(_annot.index('"size.shared"')+len('"size.shared"')):].strip().split('"')[1].strip() + try: + value_shared = value_shared[value_shared.index('{%s}' % (size_shared))+len('{%s}' % (size_shared)):].strip() + except Exception, errmsg: + pass + except Exception, errmsg: + pass + + if value_shared in empty_values: + value_shared = None + else: + try: + value_shared = value_shared[:value_shared.index('"content-type.shared"')].strip() + except: + pass + + try: + value_shared = value_shared[:value_shared.index('"modifiedsince.shared"')].strip() + except: + pass + + if value_shared.startswith('"'): + value_shared = value_shared[1:] + + if value_shared.endswith('"'): + value_shared = value_shared[:-1] + + if value_shared in empty_values: + value_shared = None + + if not value_priv == None: + ann[folder]['/private' + key] = value_priv + + if not value_shared == None: + ann[folder]['/shared' + key] = value_shared return ann |