From 77fd82eb76fc2052ec629a2d6093919c815c7f6f Mon Sep 17 00:00:00 2001 From: Thomi Richards Date: Mon, 27 Jul 2015 08:05:17 +1200 Subject: [PATCH] Fix a bug where folder creation failed since upgrading to the newest imapclient release. --- gmailfilter/actions.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/gmailfilter/actions.py b/gmailfilter/actions.py index 4982b2b..a19fbab 100644 --- a/gmailfilter/actions.py +++ b/gmailfilter/actions.py @@ -1,4 +1,5 @@ +import imapclient import logging """Classes that manipulate mails.""" @@ -32,15 +33,14 @@ def __init__(self, target_folder): self._target_folder = target_folder def process(self, conn, message): - # TODO: optimise this by trying the copy, and if we get 'NO' with - # 'TRYCREATE' then, and only then try and create the folder. Removes the - # overhead of the existance check for every message, - if not conn.folder_exists(self._target_folder): + try: + conn.copy(message.uid(), self._target_folder) + except imapclient.IMAPClient.Error as e: status = conn.create_folder(self._target_folder) + assert status.lower() == b"success", "Unable to create folder %s" % self._target_folder + conn.copy(message.uid(), self._target_folder) - assert status.lower() == "success", "Unable to create folder %s" % self._target_folder - conn.copy(message.uid(), self._target_folder) # TODO: Maybe provide logging facilities in parent 'Action' class? conn.delete_messages(message.uid()) logging.info("Moving message %r to %s" % (message, self._target_folder))