Browse Source

Refactoring email_fetcher class

Guénaël Muller 7 years ago
parent
commit
f4ffbd29f5
1 changed files with 23 additions and 16 deletions
  1. 23 16
      tracim/tracim/lib/email_fetcher.py

+ 23 - 16
tracim/tracim/lib/email_fetcher.py View File

196
         :param token: token to authenticate http connexion
196
         :param token: token to authenticate http connexion
197
         """
197
         """
198
         self._connection = None
198
         self._connection = None
199
-        self._mails = []
200
         self.host = host
199
         self.host = host
201
         self.port = port
200
         self.port = port
202
         self.user = user
201
         self.user = user
212
         while self._is_active:
211
         while self._is_active:
213
             time.sleep(self.delay)
212
             time.sleep(self.delay)
214
             self._connect()
213
             self._connect()
215
-            self._fetch()
216
-            self._notify_tracim()
214
+            mails = self._fetch()
215
+            # TODO - G.M -  2017-11-22 retry sending unsended mail
216
+            # These mails are return by _notify_tracim, flag them with "unseen"
217
+            # or store them until new _notify_tracim call
218
+            self._notify_tracim(mails)
217
             self._disconnect()
219
             self._disconnect()
218
 
220
 
219
     def stop(self) -> None:
221
     def stop(self) -> None:
220
         self._is_active = False
222
         self._is_active = False
221
 
223
 
222
     def _connect(self) -> None:
224
     def _connect(self) -> None:
223
-        # FIXME - G.M - 2017-11-15 Verify connection/disconnection
225
+        # TODO - G.M - 2017-11-15 Verify connection/disconnection
224
         # Are old connexion properly close this way ?
226
         # Are old connexion properly close this way ?
225
         if self._connection:
227
         if self._connection:
226
             self._disconnect()
228
             self._disconnect()
239
             self._connection.logout()
241
             self._connection.logout()
240
             self._connection = None
242
             self._connection = None
241
 
243
 
242
-    def _fetch(self) -> None:
244
+    def _fetch(self) -> list:
243
         """
245
         """
244
         Get news message from mailbox
246
         Get news message from mailbox
247
+        :return: list of new mails
245
         """
248
         """
246
-
249
+        mails = []
247
         # select mailbox
250
         # select mailbox
248
         rv, data = self._connection.select(self.folder)
251
         rv, data = self._connection.select(self.folder)
249
         if rv == 'OK':
252
         if rv == 'OK':
250
             # get mails
253
             # get mails
251
-            # FIXME - G.M -  2017-11-15 Which files to select as new file ?
254
+            # TODO - G.M -  2017-11-15 Which files to select as new file ?
252
             # Unseen file or All file from a directory (old one should be moved/
255
             # Unseen file or All file from a directory (old one should be moved/
253
             # deleted from mailbox during this process) ?
256
             # deleted from mailbox during this process) ?
254
             rv, data = self._connection.search(None, "(UNSEEN)")
257
             rv, data = self._connection.search(None, "(UNSEEN)")
259
                     if rv == 'OK':
262
                     if rv == 'OK':
260
                         msg = message_from_bytes(data[0][1])
263
                         msg = message_from_bytes(data[0][1])
261
                         decodedmsg = DecodedMail(msg)
264
                         decodedmsg = DecodedMail(msg)
262
-                        self._mails.append(decodedmsg)
265
+                        mails.append(decodedmsg)
263
                     else:
266
                     else:
264
                         log = 'IMAP : Unable to get mail : {}'
267
                         log = 'IMAP : Unable to get mail : {}'
265
                         logger.debug(self, log.format(str(rv)))
268
                         logger.debug(self, log.format(str(rv)))
269
         else:
272
         else:
270
             log = 'IMAP : Unable to open mailbox : {}'
273
             log = 'IMAP : Unable to open mailbox : {}'
271
             logger.debug(self, log.format(str(rv)))
274
             logger.debug(self, log.format(str(rv)))
275
+        return mails
272
 
276
 
273
-    def _notify_tracim(self) -> None:
274
-        unsended_mail = []
275
-        while self._mails:
276
-            mail = self._mails.pop()
277
+    def _notify_tracim(self, mails: list) -> list:
278
+        """
279
+        Send http request to tracim endpoint
280
+        :param mails: list of mails to send
281
+        :return: unsended mails
282
+        """
283
+        unsended_mails = []
284
+        while mails:
285
+            mail = mails.pop()
277
             msg = {'token': self.token,
286
             msg = {'token': self.token,
278
                    'user_mail': mail.get_from_address(),
287
                    'user_mail': mail.get_from_address(),
279
                    'content_id': mail.get_key(),
288
                    'content_id': mail.get_key(),
283
             try:
292
             try:
284
                 r = requests.post(self.endpoint, json=msg)
293
                 r = requests.post(self.endpoint, json=msg)
285
                 if r.status_code not in [200, 204]:
294
                 if r.status_code not in [200, 204]:
286
-                    log = 'bad status code response when sending mail to tracim: {}' # nopep8
295
+                    log = 'bad status code response when sending mail to tracim: {}'  # nopep8
287
                     logger.error(self, log.format(str(r.status_code)))
296
                     logger.error(self, log.format(str(r.status_code)))
288
             # TODO - G.M - Verify exception correctly works
297
             # TODO - G.M - Verify exception correctly works
289
             except requests.exceptions.Timeout:
298
             except requests.exceptions.Timeout:
295
                 log = 'Fail to transmit fetched mail to tracim : {}'
304
                 log = 'Fail to transmit fetched mail to tracim : {}'
296
                 logger.error(self, log.format(str(e)))
305
                 logger.error(self, log.format(str(e)))
297
                 break
306
                 break
298
-        # FIXME - G.M - 2017-11-17 Avoid too short-timed infinite retry ?
299
-        # retry later to send those mail
300
-        self._mails = unsended_mail
307
+        return unsended_mails