Commits

Gregory Petukhov committed 4f37c6d

Improve gmail email service

Comments (0)

Files changed (1)

tools/email/gmail.py

         except imaplib.IMAP4.error, ex:
             raise AuthError(str(ex))
 
-    def find_message(self, anchor):
-        typ, ids = self.box.search(None, 'BODY', '"%s"' % anchor)
+    def find_message(self, content_anchor):
+        typ, ids = self.box.search(None, 'BODY', '"%s"' % content_anchor)
         ids = ids[0].split()
         if not ids:
             raise EmailNotFound('Imap search command did not find anything')
             raise EmailNotFound('Could not find text/html or text/plain content')
 
 
-def find_message(username, password, anchor):
+def find_message(username, password, content_anchor, timeouts=[]):
     "Shortcut to Gmail functions"
+    timeouts = list(timeouts) + [0]
 
-    box = Gmail(username, password)
-    return box.find_message(anchor)
+    while timeouts:
+        box = Gmail(username, password)
+        try:
+            return box.find_message(content_anchor)
+        except EmailNotFound, ex:
+            timeout = timeouts.pop(0)
+            if timeouts:
+                time.sleep(timeout)
+                logging.debug('NO MAIL: sleeping for %d' % timeout)
+            else:
+                raise
 
 
 if __name__ == '__main__':