Преглед изворни кода

Cleans up following PEP8 rules

Adrien Panay пре 7 година
родитељ
комит
ebd2aebd4f
1 измењених фајлова са 34 додато и 19 уклоњено
  1. 34 19
      tracim/tracim/lib/email.py

+ 34 - 19
tracim/tracim/lib/email.py Прегледај датотеку

@@ -19,6 +19,7 @@ def send_email_through(
19 19
 ) -> None:
20 20
     """
21 21
     Send mail encapsulation to send it in async or sync mode.
22
+
22 23
     TODO BS 20170126: A global mail/sender management should be a good
23 24
                       thing. Actually, this method is an fast solution.
24 25
     :param sendmail_callable: A callable who get message on first parameter
@@ -41,9 +42,7 @@ def send_email_through(
41 42
 
42 43
 
43 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 47
     def __init__(self, server: str, port: int, login: str, password: str):
49 48
         self.server = server
@@ -54,9 +53,12 @@ class SmtpConfiguration(object):
54 53
 
55 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 62
     def __init__(self, config: SmtpConfiguration, really_send_messages):
61 63
         self._smtp_config = config
62 64
         self._smtp_connection = None
@@ -64,36 +66,47 @@ class EmailSender(object):
64 66
 
65 67
     def connect(self):
66 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 75
             self._smtp_connection.ehlo()
70 76
 
71 77
             if self._smtp_config.login:
72 78
                 try:
73 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 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 86
             if self._smtp_config.login:
79 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 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 97
             logger.info(self, 'Connection OK')
85 98
 
86
-
87
-
88 99
     def disconnect(self):
89 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 103
             self._smtp_connection.quit()
92 104
             logger.info(self, 'Connection closed.')
93 105
 
94 106
     def send_mail(self, message: MIMEMultipart):
95 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 110
         else:
98 111
             self.connect()  # Actually, this connects to SMTP only if required
99 112
             logger.info(self, 'Sending email to {}'.format(message['To']))
@@ -117,7 +130,8 @@ class EmailManager(object):
117 130
             password: str,
118 131
     ) -> None:
119 132
         """
120
-        Send created account email to given user
133
+        Send created account email to given user.
134
+
121 135
         :param password: choosed password
122 136
         :param user: user to notify
123 137
         """
@@ -182,7 +196,8 @@ class EmailManager(object):
182 196
 
183 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 201
         :param mako_template_filepath: file path of mako template
187 202
         :param context: dict with template context
188 203
         :return: template rendered string