Browse Source

Renaming,classmethod instead of staticmethod,'' instead of ""

Guénaël Muller 7 years ago
parent
commit
02bd4dd7db

+ 1 - 1
tracim/development.ini.base View File

224
 # Token for communication between mail fetcher and tracim controller
224
 # Token for communication between mail fetcher and tracim controller
225
 email.reply.token = mysecuretoken
225
 email.reply.token = mysecuretoken
226
 # Delay in seconds between each check
226
 # Delay in seconds between each check
227
-email.reply.delay = 60
227
+email.reply.check.heartbeat = 60
228
 
228
 
229
 ## Radical (CalDav server) configuration
229
 ## Radical (CalDav server) configuration
230
 # radicale.server.host = 0.0.0.0
230
 # radicale.server.host = 0.0.0.0

+ 2 - 2
tracim/tracim/config/app_cfg.py View File

373
         self.EMAIL_REPLY_IMAP_FOLDER = tg.config.get(
373
         self.EMAIL_REPLY_IMAP_FOLDER = tg.config.get(
374
             'email.reply.imap.folder',
374
             'email.reply.imap.folder',
375
         )
375
         )
376
-        self.EMAIL_REPLY_DELAY = int(tg.config.get(
377
-            'email.reply.delay',
376
+        self.EMAIL_REPLY_CHECK_HEARTBEAT = int(tg.config.get(
377
+            'email.reply.check.heartbeat',
378
         ))
378
         ))
379
         self.EMAIL_REPLY_TOKEN = tg.config.get(
379
         self.EMAIL_REPLY_TOKEN = tg.config.get(
380
             'email.reply.token',
380
             'email.reply.token',

+ 1 - 1
tracim/tracim/controllers/events.py View File

8
 from tracim.model.data import ContentType
8
 from tracim.model.data import ContentType
9
 
9
 
10
 
10
 
11
-class EventsRestController(RestController):
11
+class EventRestController(RestController):
12
 
12
 
13
     @tg.expose('json')
13
     @tg.expose('json')
14
     def post(self):
14
     def post(self):

+ 2 - 2
tracim/tracim/controllers/root.py View File

22
 from tracim.controllers.previews import PreviewsController
22
 from tracim.controllers.previews import PreviewsController
23
 from tracim.controllers.user import UserRestController
23
 from tracim.controllers.user import UserRestController
24
 from tracim.controllers.workspace import UserWorkspaceRestController
24
 from tracim.controllers.workspace import UserWorkspaceRestController
25
-from tracim.controllers.events import EventsRestController
25
+from tracim.controllers.events import EventRestController
26
 from tracim.lib import CST
26
 from tracim.lib import CST
27
 from tracim.lib.base import logger
27
 from tracim.lib.base import logger
28
 from tracim.lib.content import ContentApi
28
 from tracim.lib.content import ContentApi
62
     previews = PreviewsController()
62
     previews = PreviewsController()
63
 
63
 
64
     content = ContentController()
64
     content = ContentController()
65
-    events = EventsRestController()
65
+    events = EventRestController()
66
     # api
66
     # api
67
     api = APIController()
67
     api = APIController()
68
 
68
 

+ 1 - 1
tracim/tracim/lib/daemons.py View File

172
             user=cfg.EMAIL_REPLY_IMAP_USER,
172
             user=cfg.EMAIL_REPLY_IMAP_USER,
173
             password=cfg.EMAIL_REPLY_IMAP_PASSWORD,
173
             password=cfg.EMAIL_REPLY_IMAP_PASSWORD,
174
             folder=cfg.EMAIL_REPLY_IMAP_FOLDER,
174
             folder=cfg.EMAIL_REPLY_IMAP_FOLDER,
175
-            delay=cfg.EMAIL_REPLY_DELAY,
175
+            delay=cfg.EMAIL_REPLY_CHECK_HEARTBEAT,
176
             # FIXME - G.M - 2017-11-15 - proper tracim url formatting
176
             # FIXME - G.M - 2017-11-15 - proper tracim url formatting
177
             endpoint=cfg.WEBSITE_BASE_URL + "/events",
177
             endpoint=cfg.WEBSITE_BASE_URL + "/events",
178
             token=cfg.EMAIL_REPLY_TOKEN,
178
             token=cfg.EMAIL_REPLY_TOKEN,

+ 39 - 30
tracim/tracim/lib/email_fetcher.py View File

18
 
18
 
19
 from tracim.lib.base import logger
19
 from tracim.lib.base import logger
20
 
20
 
21
-TRACIM_SPECIAL_KEY_HEADER = "X-Tracim-Key"
22
-BS_HTML_BODY_PARSE_CONFIG = {
23
-    'tag_blacklist': ["script", "style", "blockquote"],
21
+TRACIM_SPECIAL_KEY_HEADER = 'X-Tracim-Key'
22
+BEAUTIFULSOUP_HTML_BODY_PARSE_CONFIG = {
23
+    'tag_blacklist': ['script', 'style', 'blockquote'],
24
     'class_blacklist': ['moz-cite-prefix', 'gmail_extra', 'gmail_quote',
24
     'class_blacklist': ['moz-cite-prefix', 'gmail_extra', 'gmail_quote',
25
                         'yahoo_quoted'],
25
                         'yahoo_quoted'],
26
     'id_blacklist': ['reply-intro'],
26
     'id_blacklist': ['reply-intro'],
29
                       'thead', 'tr', 'td', 'tbody', 'table', 'p', 'pre'],
29
                       'thead', 'tr', 'td', 'tbody', 'table', 'p', 'pre'],
30
     'attrs_whitelist': ['href'],
30
     'attrs_whitelist': ['href'],
31
 }
31
 }
32
+CONTENT_TYPE_TEXT_PLAIN = 'text/plain'
33
+CONTENT_TYPE_TEXT_HTML = 'text/html'
32
 
34
 
33
 
35
 
34
 class DecodedMail(object):
36
 class DecodedMail(object):
63
         body = None
65
         body = None
64
         if body_part:
66
         if body_part:
65
             charset = body_part.get_content_charset('iso-8859-1')
67
             charset = body_part.get_content_charset('iso-8859-1')
66
-            ctype = body_part.get_content_type()
67
-            if ctype == "text/plain":
68
+            content_type = body_part.get_content_type()
69
+            if content_type == CONTENT_TYPE_TEXT_PLAIN:
68
                 txt_body = body_part.get_payload(decode=True).decode(
70
                 txt_body = body_part.get_payload(decode=True).decode(
69
                     charset)
71
                     charset)
70
                 body = DecodedMail._parse_txt_body(txt_body)
72
                 body = DecodedMail._parse_txt_body(txt_body)
71
 
73
 
72
-            elif ctype == "text/html":
74
+            elif content_type == CONTENT_TYPE_TEXT_HTML:
73
                 html_body = body_part.get_payload(decode=True).decode(
75
                 html_body = body_part.get_payload(decode=True).decode(
74
                     charset)
76
                     charset)
75
                 body = DecodedMail._parse_html_body(html_body)
77
                 body = DecodedMail._parse_html_body(html_body)
76
 
78
 
77
         return body
79
         return body
78
 
80
 
79
-    @staticmethod
80
-    def _parse_txt_body(txt_body: str):
81
+    @classmethod
82
+    def _parse_txt_body(cls, txt_body: str):
81
         txt_body = EmailReplyParser.parse_reply(txt_body)
83
         txt_body = EmailReplyParser.parse_reply(txt_body)
82
         html_body = markdown.markdown(txt_body)
84
         html_body = markdown.markdown(txt_body)
83
         body = DecodedMail._parse_html_body(html_body)
85
         body = DecodedMail._parse_html_body(html_body)
84
         return body
86
         return body
85
 
87
 
86
-    @staticmethod
87
-    def _parse_html_body(html_body: str):
88
+    @classmethod
89
+    def _parse_html_body(cls, html_body: str):
88
         soup = BeautifulSoup(html_body)
90
         soup = BeautifulSoup(html_body)
89
-        config = BS_HTML_BODY_PARSE_CONFIG
91
+        config = BEAUTIFULSOUP_HTML_BODY_PARSE_CONFIG
90
         for tag in soup.findAll():
92
         for tag in soup.findAll():
91
             if DecodedMail._tag_to_extract(tag):
93
             if DecodedMail._tag_to_extract(tag):
92
                 tag.extract()
94
                 tag.extract()
99
                 tag.unwrap()
101
                 tag.unwrap()
100
         return str(soup)
102
         return str(soup)
101
 
103
 
102
-    @staticmethod
103
-    def _tag_to_extract(tag) -> bool:
104
-        config = BS_HTML_BODY_PARSE_CONFIG
104
+    @classmethod
105
+    def _tag_to_extract(cls, tag) -> bool:
106
+        config = BEAUTIFULSOUP_HTML_BODY_PARSE_CONFIG
105
         if tag.name.lower() in config['tag_blacklist']:
107
         if tag.name.lower() in config['tag_blacklist']:
106
             return True
108
             return True
107
         if 'class' in tag.attrs:
109
         if 'class' in tag.attrs:
114
                     return True
116
                     return True
115
         return False
117
         return False
116
 
118
 
117
-
118
     def _get_mime_body_message(self) -> typing.Optional[Message]:
119
     def _get_mime_body_message(self) -> typing.Optional[Message]:
119
         # FIXME - G.M - 2017-11-16 - Use stdlib msg.get_body feature for py3.6+
120
         # FIXME - G.M - 2017-11-16 - Use stdlib msg.get_body feature for py3.6+
120
         # FIXME - G.M - 2017-11-16 - Check support for non-multipart mail
121
         # FIXME - G.M - 2017-11-16 - Check support for non-multipart mail
121
         part = None
122
         part = None
122
         # Check for html
123
         # Check for html
123
         for part in self._message.walk():
124
         for part in self._message.walk():
124
-            ctype = part.get_content_type()
125
-            cdispo = str(part.get('Content-Disposition'))
126
-            if ctype == 'text/html' and 'attachment' not in cdispo:
125
+            content_type = part.get_content_type()
126
+            content_dispo = str(part.get('Content-Disposition'))
127
+            if content_type == CONTENT_TYPE_TEXT_HTML \
128
+                    and 'attachment' not in content_dispo:
127
                 return part
129
                 return part
128
         # check for plain text
130
         # check for plain text
129
         for part in self._message.walk():
131
         for part in self._message.walk():
130
-            ctype = part.get_content_type()
131
-            cdispo = str(part.get('Content-Disposition'))
132
-            if ctype == 'text/plain' and 'attachment' not in cdispo:
132
+            content_type = part.get_content_type()
133
+            content_dispo = str(part.get('Content-Disposition'))
134
+            if content_type == CONTENT_TYPE_TEXT_PLAIN and 'attachment' \
135
+                    not in content_dispo:
133
                 return part
136
                 return part
134
         return part
137
         return part
135
 
138
 
153
 
156
 
154
         return key
157
         return key
155
 
158
 
156
-    @staticmethod
157
-    def find_key_from_mail_address(mail_address: str) \
159
+    @classmethod
160
+    def find_key_from_mail_address(cls, mail_address: str) \
158
             -> typing.Optional[str]:
161
             -> typing.Optional[str]:
159
         """ Parse mail_adress-like string
162
         """ Parse mail_adress-like string
160
         to retrieve key.
163
         to retrieve key.
174
 class MailFetcher(object):
177
 class MailFetcher(object):
175
 
178
 
176
     def __init__(self,
179
     def __init__(self,
177
-                 host: str, port: str, user: str, password: str, folder: str,
178
-                 delay: int, endpoint: str, token:str) \
180
+                 host: str,
181
+                 port: str,
182
+                 user: str,
183
+                 password: str,
184
+                 folder: str,
185
+                 delay: int,
186
+                 endpoint: str,
187
+                 token: str) \
179
             -> None:
188
             -> None:
180
         """
189
         """
181
         Fetch mail from a mailbox folder through IMAP and add their content to
190
         Fetch mail from a mailbox folder through IMAP and add their content to
269
         unsended_mail = []
278
         unsended_mail = []
270
         while self._mails:
279
         while self._mails:
271
             mail = self._mails.pop()
280
             mail = self._mails.pop()
272
-            msg = {"token": self.token,
273
-                   "user_mail": mail.get_from_address(),
274
-                   "content_id": mail.get_key(),
275
-                   "payload": {
276
-                       "content": mail.get_body(),
281
+            msg = {'token': self.token,
282
+                   'user_mail': mail.get_from_address(),
283
+                   'content_id': mail.get_key(),
284
+                   'payload': {
285
+                       'content': mail.get_body(),
277
                    }}
286
                    }}
278
             try:
287
             try:
279
                 r = requests.post(self.endpoint, json=msg)
288
                 r = requests.post(self.endpoint, json=msg)