Package turbomail :: Module api
[hide private]
[frames] | no frames]

Source Code for Module turbomail.api

  1  # encoding: utf-8 
  2   
  3  """TurboMail extension API.""" 
  4   
  5   
  6  import logging 
  7  import warnings 
  8   
  9  from turbomail.control import interface 
 10  from turbomail.exceptions import ManagerException 
 11   
 12   
 13  __all__ = ['Extension', 'TransportFactory', 'Transport', 'Manager'] 
 14   
 15   
 16   
17 -class Extension(object):
18 """Basic extension API that allows for startup and shutdown hooks.""" 19
20 - def __init__(self):
21 super(Extension, self).__init__() 22 self.ready = False
23
24 - def start(self):
25 self.ready = True 26 return True
27
28 - def stop(self):
29 if not self.ready: 30 return False 31 32 self.ready = False 33 return True
34 35
36 -class TransportFactory(Extension):
37 """An extension that creates new Transport instances. 38 39 This is useful to perform configuration or startup tasks outside the Transport's initializer. 40 """ 41 42 transport = None 43
44 - def __init__(self):
45 super(TransportFactory, self).__init__()
46
47 - def new(self):
48 if not self.ready: return None 49 return self.transport()
50 51
52 -class Transport(object):
53 """Message delivery subsystem API. 54 55 A Transport can deliver messages towards their recipients with a specific 56 method, e.g. SMTP. They don't care about delivery strategies like queing or 57 batch submission.""" 58
59 - def __init__(self):
60 super(Transport, self).__init__()
61
62 - def deliver(self, message):
63 raise NotImplementedError, "Transport plugin must override this method without inheritance."
64
65 - def config_get(self, key, default=None, tm2_key=None):
66 """Returns the value for the given key from the configuration. If the 67 value was not found, this method looks if old configuration option 68 (specified in tm2_key) is used. If tm2_key was ommitted, it tries to 69 calculate the old key from the new one by cutting out the 'smtp.' in the 70 middle. If an old configuration key is used, a DeprecationWarning is 71 issued. 72 As a final fallback, the default value (default None) is 73 returned.""" 74 # We can not use 'key in interface.config' because TurboGears' 75 # configuration (ConfigObj) does not support this (it does provide any 76 # possibility to detect if a value is present or not. 77 value = interface.config.get(key, None) 78 if value == None: 79 if tm2_key != None and not tm2_key.startswith('mail.'): 80 tm2_key = 'mail.' + tm2_key 81 elif tm2_key == None: 82 tm2_key = key.replace('.smtp.', '.') 83 value = interface.config.get(tm2_key, None) 84 if value is not None: 85 basemsg = 'Configuration key "%s" is deprecated, please use "%s" instead' 86 warn_text = basemsg % (tm2_key, key) 87 warnings.warn(warn_text, category=DeprecationWarning) 88 if value == None: 89 value = default 90 return value
91
92 - def stop(self):
93 """Called by the manager before the transport instance is destroyed. The 94 transport can do some final cleanups (like releasing external resources) 95 here.""" 96 pass
97 98
99 -class Manager(Extension):
100 """Manager instances orchestrate the delivery of messages.""" 101
102 - def __init__(self):
103 super(Manager, self).__init__() 104 self.transport_factory = None
105
106 - def get_new_transport(self):
107 if self.transport_factory == None: 108 self.transport_factory = interface.transport 109 transport = self.transport_factory.new() 110 if transport is None: 111 raise ManagerException('Unable to allocate new transport.') 112 return transport
113
114 - def deliver(self, message):
115 return self.ready
116