cwconfig.py
branchstable
changeset 2221 d9b85a7b0bdd
parent 2220 64aace08ae2f
child 2267 e1d2df3f1091
equal deleted inserted replaced
2220:64aace08ae2f 2221:d9b85a7b0bdd
    15 _ = unicode
    15 _ = unicode
    16 
    16 
    17 import sys
    17 import sys
    18 import os
    18 import os
    19 import logging
    19 import logging
       
    20 from smtplib import SMTP
       
    21 from threading import Lock
    20 from os.path import exists, join, expanduser, abspath, normpath, basename, isdir
    22 from os.path import exists, join, expanduser, abspath, normpath, basename, isdir
    21 
    23 
    22 from logilab.common.decorators import cached
    24 from logilab.common.decorators import cached
    23 from logilab.common.logging_ext import set_log_methods, init_log
    25 from logilab.common.logging_ext import set_log_methods, init_log
    24 from logilab.common.configuration import (Configuration, Method,
    26 from logilab.common.configuration import (Configuration, Method,
    26 
    28 
    27 from cubicweb import CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, ConfigurationError
    29 from cubicweb import CW_SOFTWARE_ROOT, CW_MIGRATION_MAP, ConfigurationError
    28 from cubicweb.toolsutils import env_path, create_dir
    30 from cubicweb.toolsutils import env_path, create_dir
    29 
    31 
    30 CONFIGURATIONS = []
    32 CONFIGURATIONS = []
       
    33 
       
    34 SMTP_LOCK = Lock()
    31 
    35 
    32 
    36 
    33 class metaconfiguration(type):
    37 class metaconfiguration(type):
    34     """metaclass to automaticaly register configuration"""
    38     """metaclass to automaticaly register configuration"""
    35     def __new__(mcs, name, bases, classdict):
    39     def __new__(mcs, name, bases, classdict):
   825             create_dir(i18ndir)
   829             create_dir(i18ndir)
   826         sourcedirs = [join(path, 'i18n') for path in self.cubes_path()]
   830         sourcedirs = [join(path, 'i18n') for path in self.cubes_path()]
   827         sourcedirs.append(self.i18n_lib_dir())
   831         sourcedirs.append(self.i18n_lib_dir())
   828         return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs)
   832         return i18n.compile_i18n_catalogs(sourcedirs, i18ndir, langs)
   829 
   833 
       
   834     def sendmails(self, msgs):
       
   835         """msgs: list of 2-uple (message object, recipients)"""
       
   836         server, port = self['smtp-host'], self['smtp-port']
       
   837         SMTP_LOCK.acquire()
       
   838         try:
       
   839             try:
       
   840                 smtp = SMTP(server, port)
       
   841             except Exception, ex:
       
   842                 self.exception("can't connect to smtp server %s:%s (%s)",
       
   843                                server, port, ex)
       
   844                 return
       
   845             heloaddr = '%s <%s>' % (self['sender-name'], self['sender-addr'])
       
   846             for msg, recipients in msgs:
       
   847                 try:
       
   848                     smtp.sendmail(heloaddr, recipients, msg.as_string())
       
   849                 except Exception, ex:
       
   850                     self.exception("error sending mail to %s (%s)",
       
   851                                    recipients, ex)
       
   852             smtp.close()
       
   853         finally:
       
   854             SMTP_LOCK.release()
       
   855 
   830 set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration'))
   856 set_log_methods(CubicWebConfiguration, logging.getLogger('cubicweb.configuration'))
   831 
   857 
   832 # alias to get a configuration instance from an application id
   858 # alias to get a configuration instance from an application id
   833 application_configuration = CubicWebConfiguration.config_for
   859 application_configuration = CubicWebConfiguration.config_for
   834 
   860