[PATCH v2 4/6] __syncmessagesto_flags: refactor for readability
Igor Almeida
igor.contato at gmail.com
Fri Nov 20 19:09:12 GMT 2015
Extract the flag/keyword translation and combination logic to a
function.
Signed-off-by: Igor Almeida <igor.contato at gmail.com>
---
offlineimap/folder/Base.py | 64 +++++++++++++++++++++++++++-------------------
1 file changed, 37 insertions(+), 27 deletions(-)
diff --git a/offlineimap/folder/Base.py b/offlineimap/folder/Base.py
index b48afbe..d77a16c 100644
--- a/offlineimap/folder/Base.py
+++ b/offlineimap/folder/Base.py
@@ -908,6 +908,42 @@ class BaseFolder(object):
return #don't delete messages in dry-run mode
dstfolder.deletemessages(deletelist)
+ def combine_flags_and_keywords(self, uid, dstfolder):
+ """Combine the message's flags and keywords using the mapping for the
+ destination folder."""
+
+ # Take a copy of the message flag set, otherwise
+ # __syncmessagesto_flags() will fail because statusflags is actually a
+ # reference to selfflags (which it should not, but I don't have time to
+ # debug THAT).
+ selfflags = set(self.getmessageflags(uid))
+
+ try:
+ keywordmap = dstfolder.getrepository().getkeywordmap()
+ knownkeywords = set(keywordmap.keys())
+
+ selfkeywords = self.getmessagekeywords(uid)
+
+ if not knownkeywords >= selfkeywords:
+ #some of the message's keywords are not in the mapping, so
+ #skip them
+
+ skipped_keywords = list(selfkeywords - knownkeywords)
+ selfkeywords &= knownkeywords
+
+ self.ui.warn("Unknown keywords skipped: %s\n"
+ "You may want to change your configuration to include "
+ "those\n" % (skipped_keywords))
+
+ keywordletterset = set([keywordmap[keyw] for keyw in selfkeywords])
+
+ #add the mapped keywords to the list of message flags
+ selfflags |= keywordletterset
+ except NotImplementedError:
+ pass
+
+ return selfflags
+
def __syncmessagesto_flags(self, dstfolder, statusfolder):
"""Pass 3: Flag synchronization.
@@ -930,38 +966,12 @@ class BaseFolder(object):
if uid < 0 or not dstfolder.uidexists(uid):
continue
- selfflags = self.getmessageflags(uid)
-
if statusfolder.uidexists(uid):
statusflags = statusfolder.getmessageflags(uid)
else:
statusflags = set()
- #keywords: if there is a keyword map, use it to figure out what
- #other 'flags' we should add
- try:
- keywordmap = dstfolder.getrepository().getkeywordmap()
- knownkeywords = set(keywordmap.keys())
-
- selfkeywords = self.getmessagekeywords(uid)
-
- if not knownkeywords >= selfkeywords:
- #some of the message's keywords are not in the mapping, so
- #skip them
-
- skipped_keywords = list(selfkeywords - knownkeywords)
- selfkeywords &= knownkeywords
-
- self.ui.warn("Unknown keywords skipped: %s\n"
- "You may want to change your configuration to include "
- "those\n" % (skipped_keywords))
-
- keywordletterset = set([keywordmap[keyw] for keyw in selfkeywords])
-
- #add the lower-case letters to the list of message flags
- selfflags |= keywordletterset
- except NotImplementedError:
- pass
+ selfflags = self.combine_flags_and_keywords(uid, dstfolder)
addflags = selfflags - statusflags
delflags = statusflags - selfflags
--
2.5.3
More information about the OfflineIMAP-project
mailing list