create sendmails method on cwconfig, use it in SendMailOp stable
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 01 Jul 2009 16:44:37 +0200
branchstable
changeset 2221 d9b85a7b0bdd
parent 2220 64aace08ae2f
child 2222 81130047390d
create sendmails method on cwconfig, use it in SendMailOp
cwconfig.py
devtools/apptest.py
server/hookhelper.py
--- a/cwconfig.py	Wed Jul 01 16:41:30 2009 +0200
+++ b/cwconfig.py	Wed Jul 01 16:44:37 2009 +0200
@@ -17,6 +17,8 @@
 import sys
 import os
 import logging
+from smtplib import SMTP
+from threading import Lock
 from os.path import exists, join, expanduser, abspath, normpath, basename, isdir
 
 from logilab.common.decorators import cached
@@ -29,6 +31,8 @@
 
 CONFIGURATIONS = []
 
+SMTP_LOCK = Lock()
+
 
 class metaconfiguration(type):
     """metaclass to automaticaly register configuration"""
@@ -827,6 +831,28 @@
         sourcedirs.append(self.i18n_lib_dir())
         return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs)
 
+    def sendmails(self, msgs):
+        """msgs: list of 2-uple (message object, recipients)"""
+        server, port = self['smtp-host'], self['smtp-port']
+        SMTP_LOCK.acquire()
+        try:
+            try:
+                smtp = SMTP(server, port)
+            except Exception, ex:
+                self.exception("can't connect to smtp server %s:%s (%s)",
+                               server, port, ex)
+                return
+            heloaddr = '%s <%s>' % (self['sender-name'], self['sender-addr'])
+            for msg, recipients in msgs:
+                try:
+                    smtp.sendmail(heloaddr, recipients, msg.as_string())
+                except Exception, ex:
+                    self.exception("error sending mail to %s (%s)",
+                                   recipients, ex)
+            smtp.close()
+        finally:
+            SMTP_LOCK.release()
+
 set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration'))
 
 # alias to get a configuration instance from an application id
--- a/devtools/apptest.py	Wed Jul 01 16:41:30 2009 +0200
+++ b/devtools/apptest.py	Wed Jul 01 16:44:37 2009 +0200
@@ -46,8 +46,8 @@
     def sendmail(self, helo_addr, recipients, msg):
         MAILBOX.append(Email(recipients, msg))
 
-from cubicweb.server import hookhelper
-hookhelper.SMTP = MockSMTP
+from cubicweb import cwconfig
+cwconfig.SMTP = MockSMTP
 
 
 def get_versions(self, checkversions=False):
--- a/server/hookhelper.py	Wed Jul 01 16:41:30 2009 +0200
+++ b/server/hookhelper.py	Wed Jul 01 16:44:37 2009 +0200
@@ -7,9 +7,6 @@
 """
 __docformat__ = "restructuredtext en"
 
-from smtplib import SMTP
-from threading import Lock
-
 from cubicweb import RepositoryError
 from cubicweb.server.pool import SingleLastOperation
 
@@ -47,8 +44,6 @@
 
 # mail related ################################################################
 
-SMTP_LOCK = Lock()
-
 class SendMailOp(SingleLastOperation):
     def __init__(self, session, msg=None, recipients=None, **kwargs):
         # may not specify msg yet, as
@@ -70,26 +65,7 @@
         self.repo.threaded_task(self.sendmails)
 
     def sendmails(self):
-        server, port = self.config['smtp-host'], self.config['smtp-port']
-        SMTP_LOCK.acquire()
-        try:
-            try:
-                smtp = SMTP(server, port)
-            except Exception, ex:
-                self.exception("can't connect to smtp server %s:%s (%s)",
-                               server, port, ex)
-                return
-            heloaddr = '%s <%s>' % (self.config['sender-name'],
-                                    self.config['sender-addr'])
-            for msg, recipients in self.to_send:
-                try:
-                    smtp.sendmail(heloaddr, recipients, msg.as_string())
-                except Exception, ex:
-                    self.exception("error sending mail to %s (%s)",
-                                   recipients, ex)
-            smtp.close()
-        finally:
-            SMTP_LOCK.release()
+        self.config.sendmails(self.to_send)
 
 
 # state related ###############################################################