root / trunk / turbomail / api.py

Revision 128, 3.2 kB (checked in by fs, 2 weeks ago)

fixes for the TG adapter (TurboGears's ConfigObj? does not behave like a dict)

  • Property svn:keywords set to Revision
Line 
1# encoding: utf-8
2
3"""TurboMail extension API."""
4
5__version__ = "$Revision$"
6
7import warnings
8
9from exceptions import *
10from turbomail.control import interface
11
12__all__ = ['Extension', 'TransportFactory', 'Transport', 'Manager']
13
14
15class Extension(object):
16    name = "GenericExtension"
17    version = "0.1"
18    url = "http://www.python-turbomail.org/wiki/3.0/Extensions"
19   
20    def __init__(self):
21        super(Extension, self).__init__()
22        self.ready = False
23   
24    def start(self):
25        self.ready = True
26       
27        return True
28   
29    def stop(self):
30        if not self.ready: return False
31       
32        self.ready = False
33       
34        return True
35
36
37class TransportFactory(Extension):
38    """docstring for TransportFactory"""
39    transport = None
40   
41    def __init__(self):
42        super(TransportFactory, self).__init__()
43   
44    def new(self):
45        if not self.ready: return None
46        return self.transport()
47
48
49class Transport(object):
50    """A Transport can deliver messages towards their recipients with a specific
51    method, e.g. SMTP. They don't care about delivery strategies like queing or
52    batch submission."""
53   
54    def __init__(self):
55        super(Transport, self).__init__()
56   
57    def deliver(self, message):
58        raise NotImplementedError, "Transport plugin must override this method without inheritance."
59   
60   
61    def config_get(self, key, default=None, tm2_key=None):
62        """Returns the value for the given key from the configuration. If the
63        value was not found, this method looks if old configuration option
64        (specified in tm2_key) is used. If tm2_key was ommitted, it tries to
65        calculate the old key from the new one by cutting out the 'smtp.' in the
66        middle. If an old configuration key is used, a DeprecationWarning is
67        issued.
68        As a final fallback, the default value (default None) is
69        returned."""
70        # We can not use 'key in interface.config' because TurboGears'
71        # configuration (ConfigObj) does not support this (it does provide any
72        # possibility to detect if a value is present or not.
73        value = interface.config.get(key, None)
74        if value == None:
75            if tm2_key != None and not tm2_key.startswith('mail.'):
76                tm2_key = 'mail.' + tm2_key
77            elif tm2_key == None:
78                tm2_key = key.replace('.smtp.', '.')
79            value = interface.config.get(key, None)
80            if value != None:
81                value = interface.config.get(tm2_key)
82                basemsg = 'Configuration key "%s" is deprecated, please use "%s" instead'
83                warn_text = basemsg % (tm2_key, key)
84                warnings.warn(warn_text, category=DeprecationWarning)
85        if value == None:
86            value = default
87        return value
88   
89   
90    def stop(self):
91        """Called by the manager before the transport instance is destroyed. The
92        transport can do some final cleanups (like releasing external resources)
93        here."""
94        pass
95
96
97class Manager(Extension):
98    """docstring for Manager"""
99    def __init__(self):
100        super(Manager, self).__init__()
101   
102    def deliver(self, message):
103        if not self.ready: return False
104        return True
Note: See TracBrowser for help on using the browser.