Browse Source

Pep8+coding rules for email_fetcher

Guénaël Muller 7 years ago
parent
commit
9ebfd8d527
1 changed files with 54 additions and 49 deletions
  1. 54 49
      tracim/tracim/lib/email_fetcher.py

+ 54 - 49
tracim/tracim/lib/email_fetcher.py View File

1
+# -*- coding: utf-8 -*-
2
+
1
 import sys
3
 import sys
2
 import time
4
 import time
3
 import imaplib
5
 import imaplib
6
 from typing import Union
8
 from typing import Union
7
 from email.message import Message
9
 from email.message import Message
8
 from email.header import Header, decode_header, make_header
10
 from email.header import Header, decode_header, make_header
9
-from email.utils import parseaddr,parsedate_tz,mktime_tz
11
+from email.utils import parseaddr, parsedate_tz, mktime_tz
10
 from email import message_from_bytes
12
 from email import message_from_bytes
11
 
13
 
12
 import requests
14
 import requests
14
 from tracim.controllers.events import VALID_TOKEN_VALUE
16
 from tracim.controllers.events import VALID_TOKEN_VALUE
15
 
17
 
16
 
18
 
17
-TRACIM_SPECIAL_KEY_HEADER="X-Tracim-Key"
19
+TRACIM_SPECIAL_KEY_HEADER = "X-Tracim-Key"
20
+
18
 
21
 
19
-def str_header(header:Header):
22
+def str_header(header: Header) -> str:
20
     return str(make_header(decode_header(header)))
23
     return str(make_header(decode_header(header)))
21
 
24
 
22
-def decode_mail(msg:Message)-> dict:
25
+
26
+def decode_mail(msg: Message)-> dict:
23
     """
27
     """
24
     Get useful header and body content and decode from Message
28
     Get useful header and body content and decode from Message
25
     :param msg:
29
     :param msg:
26
     :return:
30
     :return:
27
     """
31
     """
28
-    mailData = {}
32
+    mail_data = {}
29
 
33
 
30
     try:
34
     try:
31
-        mailData['subject'] = str_header(msg['subject'])
32
-        mailData['msg_id'] = str_header(msg['Message-ID'])
33
-        mailData['from'] = parseaddr(msg['From'])[1]
35
+        mail_data['subject'] = str_header(msg['subject'])
36
+        mail_data['msg_id'] = str_header(msg['Message-ID'])
37
+        mail_data['from'] = parseaddr(msg['From'])[1]
34
         # Reply key
38
         # Reply key
35
-        mailData['to'] = parseaddr(msg['To'])[1]
39
+        mail_data['to'] = parseaddr(msg['To'])[1]
36
 
40
 
37
-        mailData['references'] = parseaddr(msg['References'])[1]
41
+        mail_data['references'] = parseaddr(msg['References'])[1]
38
         if TRACIM_SPECIAL_KEY_HEADER in msg:
42
         if TRACIM_SPECIAL_KEY_HEADER in msg:
39
-            mailData[TRACIM_SPECIAL_KEY_HEADER] = str_header(msg[TRACIM_SPECIAL_KEY_HEADER])
43
+            mail_data[TRACIM_SPECIAL_KEY_HEADER] = str_header(msg[TRACIM_SPECIAL_KEY_HEADER])  # nopep8
40
         # date
44
         # date
41
         date_h = str_header(msg['Date'])
45
         date_h = str_header(msg['Date'])
42
         date_tuple = parsedate_tz(date_h)
46
         date_tuple = parsedate_tz(date_h)
43
 
47
 
44
-        mailData['date'] = datetime.datetime.fromtimestamp(mktime_tz(date_tuple))
48
+        mail_data['date'] = datetime.datetime.fromtimestamp(
49
+            mktime_tz(date_tuple)
50
+        )
45
 
51
 
46
-    except Exception as e:
52
+    except Exception:
47
         # TODO: exception -> mail not correctly formatted
53
         # TODO: exception -> mail not correctly formatted
48
-        return None
49
-    #email.utils.mktime_tz(date_tuple))
50
-    #print( "Local Date:", local_date.strftime("%a, %d %b %Y %H:%M:%S"))
51
-    ## TODO : msg.get_body look like the best way to get body but it's a new feature now (08112017).
54
+        return {}
55
+    # TODO : msg.get_body look like the best way to get body
56
+    # but it's a new feature now (08112017).
52
     for part in msg.walk():
57
     for part in msg.walk():
53
         if not part.get_content_type() == "text/plain":
58
         if not part.get_content_type() == "text/plain":
54
             continue
59
             continue
55
         else:
60
         else:
56
             # TODO: check if decoding is working correctly
61
             # TODO: check if decoding is working correctly
57
             charset = part.get_content_charset('iso-8859-1')
62
             charset = part.get_content_charset('iso-8859-1')
58
-            mailData['body']= part.get_payload(decode=True).decode(charset)
63
+            mail_data['body'] = part.get_payload(decode=True).decode(charset)
59
             break
64
             break
60
-    return mailData
65
+    return mail_data
66
+
61
 
67
 
62
-def get_tracim_content_key(mailData:dict) -> Union[str,None]:
68
+def get_tracim_content_key(mail_data: dict) -> Union[str, None]:
63
 
69
 
64
-    """ Link mailData dict to tracim content
70
+    """ Link mail_data dict to tracim content
65
     First try checking special header, them check 'to' header
71
     First try checking special header, them check 'to' header
66
     and finally check first(oldest) mail-id of 'references' header
72
     and finally check first(oldest) mail-id of 'references' header
67
     """
73
     """
68
     key = None
74
     key = None
69
-    if TRACIM_SPECIAL_KEY_HEADER in mailData:
70
-        key = mailData[TRACIM_SPECIAL_KEY_HEADER]
71
-    if key is None and 'to' in mailData:
72
-        key = find_key_from_mail_adress(mailData['to'])
73
-    if key is None and 'references' in mailData:
74
-        mail_adress = mailData['references']
75
+    if TRACIM_SPECIAL_KEY_HEADER in mail_data:
76
+        key = mail_data[TRACIM_SPECIAL_KEY_HEADER]
77
+    if key is None and 'to' in mail_data:
78
+        key = find_key_from_mail_adress(mail_data['to'])
79
+    if key is None and 'references' in mail_data:
80
+        mail_adress = mail_data['references']
75
         key = find_key_from_mail_adress(mail_adress)
81
         key = find_key_from_mail_adress(mail_adress)
76
     return key
82
     return key
77
 
83
 
78
-def find_key_from_mail_adress(mail_address:str) -> Union[str,None]:
84
+
85
+def find_key_from_mail_adress(mail_address: str) -> Union[str, None]:
79
     """ Parse mail_adress-like string
86
     """ Parse mail_adress-like string
80
     to retrieve key.
87
     to retrieve key.
81
 
88
 
82
     :param mail_address: user+key@something like string
89
     :param mail_address: user+key@something like string
83
     :return: key
90
     :return: key
84
     """
91
     """
85
-    username= mail_address.split('@')[0]
92
+    username = mail_address.split('@')[0]
86
     username_data = username.split('+')
93
     username_data = username.split('+')
87
     if len(username_data) == 2:
94
     if len(username_data) == 2:
88
         key = username_data[1]
95
         key = username_data[1]
106
 
113
 
107
         self._is_active = True
114
         self._is_active = True
108
 
115
 
109
-
110
-    def run(self):
116
+    def run(self) -> None:
111
         while self._is_active:
117
         while self._is_active:
112
             time.sleep(self.delay)
118
             time.sleep(self.delay)
113
             self._connect()
119
             self._connect()
115
             self._notify_tracim()
121
             self._notify_tracim()
116
             self._disconnect()
122
             self._disconnect()
117
 
123
 
118
-    def stop(self):
124
+    def stop(self) -> None:
119
         self._is_active = False
125
         self._is_active = False
120
 
126
 
121
-    def _connect(self):
122
-        ## verify if connected ?
127
+    def _connect(self) -> None:
128
+        # verify if connected ?
123
         if self._connection:
129
         if self._connection:
124
             self._disconnect()
130
             self._disconnect()
125
         # TODO: Support unencrypted connection ?
131
         # TODO: Support unencrypted connection ?
126
         # TODO: Support keyfile,certfile ?
132
         # TODO: Support keyfile,certfile ?
127
-        self._connection = imaplib.IMAP4_SSL(self.host,self.port)
133
+        self._connection = imaplib.IMAP4_SSL(self.host, self.port)
128
         try:
134
         try:
129
-            rv, data = self._connection.login(self.user,self.password)
135
+            self._connection.login(self.user, self.password)
130
         except Exception as e:
136
         except Exception as e:
131
             log = 'IMAP login error: {}'
137
             log = 'IMAP login error: {}'
132
             logger.debug(self, log.format(e.__str__()))
138
             logger.debug(self, log.format(e.__str__()))
133
 
139
 
134
-    def _disconnect(self):
140
+    def _disconnect(self) -> None:
135
         if self._connection:
141
         if self._connection:
136
             self._connection.close()
142
             self._connection.close()
137
             self._connection.logout()
143
             self._connection.logout()
138
             self._connection = None
144
             self._connection = None
139
 
145
 
140
-    def _fetch(self):
146
+    def _fetch(self) -> None:
141
         """
147
         """
142
         Get news message from mailbox
148
         Get news message from mailbox
143
         """
149
         """
155
                     if rv == 'OK':
161
                     if rv == 'OK':
156
                         msg = message_from_bytes(data[0][1])
162
                         msg = message_from_bytes(data[0][1])
157
                         self._mails.append(msg)
163
                         self._mails.append(msg)
158
-                        ret = True
159
                     else:
164
                     else:
160
                         # TODO : Check best debug value
165
                         # TODO : Check best debug value
161
                         log = 'IMAP : Unable to get mail : {}'
166
                         log = 'IMAP : Unable to get mail : {}'
162
-                        logger.debug(self,log.format(str(rv)))
167
+                        logger.debug(self, log.format(str(rv)))
163
             else:
168
             else:
164
-                #TODO : Distinct error from empty mailbox ?
169
+                # TODO : Distinct error from empty mailbox ?
165
                 pass
170
                 pass
166
-        else :
171
+        else:
167
             # TODO : Check best debug value
172
             # TODO : Check best debug value
168
             log = 'IMAP : Unable to open mailbox : {}'
173
             log = 'IMAP : Unable to open mailbox : {}'
169
-            logger.debug(self,log.format(str(rv)))
174
+            logger.debug(self, log.format(str(rv)))
170
 
175
 
171
-    def _notify_tracim(self):
176
+    def _notify_tracim(self) -> None:
172
         while self._mails:
177
         while self._mails:
173
             mail = self._mails.pop()
178
             mail = self._mails.pop()
174
             decoded_mail = decode_mail(mail)
179
             decoded_mail = decode_mail(mail)
175
-            msg = {"token" : VALID_TOKEN_VALUE,
176
-                   "user_mail" : decoded_mail['from'],
177
-                   "content_id" : get_tracim_content_key(decoded_mail),
180
+            msg = {"token": VALID_TOKEN_VALUE,
181
+                   "user_mail": decoded_mail['from'],
182
+                   "content_id": get_tracim_content_key(decoded_mail),
178
                    "payload": {
183
                    "payload": {
179
-                       "content": decoded_mail['body']
184
+                       "content": decoded_mail['body'],
180
                    }}
185
                    }}
181
 
186
 
182
-            requests.post(self.endpoint,json=msg)
187
+            requests.post(self.endpoint, json=msg)
183
             pass
188
             pass