Browse Source

Cleans up following PEP8 rules

Adrien Panay 7 years ago
parent
commit
ebd2aebd4f
1 changed files with 34 additions and 19 deletions
  1. 34 19
      tracim/tracim/lib/email.py

+ 34 - 19
tracim/tracim/lib/email.py View File

19
 ) -> None:
19
 ) -> None:
20
     """
20
     """
21
     Send mail encapsulation to send it in async or sync mode.
21
     Send mail encapsulation to send it in async or sync mode.
22
+
22
     TODO BS 20170126: A global mail/sender management should be a good
23
     TODO BS 20170126: A global mail/sender management should be a good
23
                       thing. Actually, this method is an fast solution.
24
                       thing. Actually, this method is an fast solution.
24
     :param sendmail_callable: A callable who get message on first parameter
25
     :param sendmail_callable: A callable who get message on first parameter
41
 
42
 
42
 
43
 
43
 class SmtpConfiguration(object):
44
 class SmtpConfiguration(object):
44
-    """
45
-    Container class for SMTP configuration used in Tracim
46
-    """
45
+    """Container class for SMTP configuration used in Tracim."""
47
 
46
 
48
     def __init__(self, server: str, port: int, login: str, password: str):
47
     def __init__(self, server: str, port: int, login: str, password: str):
49
         self.server = server
48
         self.server = server
54
 
53
 
55
 class EmailSender(object):
54
 class EmailSender(object):
56
     """
55
     """
57
-    this class allow to send emails and has no relations with SQLAlchemy and other tg HTTP request environment
58
-    This means that it can be used in any thread (even through a asyncjob_perform() call
56
+    Independent email sender class.
57
+
58
+    To allow its use in any thread, as an asyncjob_perform() call for
59
+    example, it has no dependencies on SQLAlchemy nor tg HTTP request.
59
     """
60
     """
61
+
60
     def __init__(self, config: SmtpConfiguration, really_send_messages):
62
     def __init__(self, config: SmtpConfiguration, really_send_messages):
61
         self._smtp_config = config
63
         self._smtp_config = config
62
         self._smtp_connection = None
64
         self._smtp_connection = None
64
 
66
 
65
     def connect(self):
67
     def connect(self):
66
         if not self._smtp_connection:
68
         if not self._smtp_connection:
67
-            logger.info(self, 'Connecting from SMTP server {}'.format(self._smtp_config.server))
68
-            self._smtp_connection = smtplib.SMTP(self._smtp_config.server, self._smtp_config.port)
69
+            log = 'Connecting from SMTP server {}'
70
+            logger.info(self, log.format(self._smtp_config.server))
71
+            self._smtp_connection = smtplib.SMTP(
72
+                self._smtp_config.server,
73
+                self._smtp_config.port
74
+            )
69
             self._smtp_connection.ehlo()
75
             self._smtp_connection.ehlo()
70
 
76
 
71
             if self._smtp_config.login:
77
             if self._smtp_config.login:
72
                 try:
78
                 try:
73
                     starttls_result = self._smtp_connection.starttls()
79
                     starttls_result = self._smtp_connection.starttls()
74
-                    logger.debug(self, 'SMTP start TLS result: {}'.format(starttls_result))
80
+                    log = 'SMTP start TLS result: {}'
81
+                    logger.debug(self, log.format(starttls_result))
75
                 except Exception as e:
82
                 except Exception as e:
76
-                    logger.debug(self, 'SMTP start TLS error: {}'.format(e.__str__()))
77
-                    
83
+                    log = 'SMTP start TLS error: {}'
84
+                    logger.debug(self, log.format(e.__str__()))
85
+
78
             if self._smtp_config.login:
86
             if self._smtp_config.login:
79
                 try:
87
                 try:
80
-                    login_res = self._smtp_connection.login(self._smtp_config.login, self._smtp_config.password)
81
-                    logger.debug(self, 'SMTP login result: {}'.format(login_res))
88
+                    login_res = self._smtp_connection.login(
89
+                        self._smtp_config.login,
90
+                        self._smtp_config.password
91
+                    )
92
+                    log = 'SMTP login result: {}'
93
+                    logger.debug(self, log.format(login_res))
82
                 except Exception as e:
94
                 except Exception as e:
83
-                    logger.debug(self, 'SMTP login error: {}'.format(e.__str__()))
95
+                    log = 'SMTP login error: {}'
96
+                    logger.debug(self, log.format(e.__str__()))
84
             logger.info(self, 'Connection OK')
97
             logger.info(self, 'Connection OK')
85
 
98
 
86
-
87
-
88
     def disconnect(self):
99
     def disconnect(self):
89
         if self._smtp_connection:
100
         if self._smtp_connection:
90
-            logger.info(self, 'Disconnecting from SMTP server {}'.format(self._smtp_config.server))
101
+            log = 'Disconnecting from SMTP server {}'
102
+            logger.info(self, log.format(self._smtp_config.server))
91
             self._smtp_connection.quit()
103
             self._smtp_connection.quit()
92
             logger.info(self, 'Connection closed.')
104
             logger.info(self, 'Connection closed.')
93
 
105
 
94
     def send_mail(self, message: MIMEMultipart):
106
     def send_mail(self, message: MIMEMultipart):
95
         if not self._is_active:
107
         if not self._is_active:
96
-            logger.info(self, 'Not sending email to {} (service desactivated)'.format(message['To']))
108
+            log = 'Not sending email to {} (service disabled)'
109
+            logger.info(self, log.format(message['To']))
97
         else:
110
         else:
98
             self.connect()  # Actually, this connects to SMTP only if required
111
             self.connect()  # Actually, this connects to SMTP only if required
99
             logger.info(self, 'Sending email to {}'.format(message['To']))
112
             logger.info(self, 'Sending email to {}'.format(message['To']))
117
             password: str,
130
             password: str,
118
     ) -> None:
131
     ) -> None:
119
         """
132
         """
120
-        Send created account email to given user
133
+        Send created account email to given user.
134
+
121
         :param password: choosed password
135
         :param password: choosed password
122
         :param user: user to notify
136
         :param user: user to notify
123
         """
137
         """
182
 
196
 
183
     def _render(self, mako_template_filepath: str, context: dict):
197
     def _render(self, mako_template_filepath: str, context: dict):
184
         """
198
         """
185
-        Render mako template with all needed current variables
199
+        Render mako template with all needed current variables.
200
+
186
         :param mako_template_filepath: file path of mako template
201
         :param mako_template_filepath: file path of mako template
187
         :param context: dict with template context
202
         :param context: dict with template context
188
         :return: template rendered string
203
         :return: template rendered string