VirtualMailManager/Transport.py
author Pascal Volk <neverseen@users.sourceforge.net>
Sat, 27 Feb 2010 20:58:49 +0000
branchv0.6.x
changeset 220 8b8d632f0ef3
parent 216 0c8c053b451c
child 225 a51809f7940b
permissions -rw-r--r--
doc: Added the alias documentation.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 138
diff changeset
     2
# Copyright (c) 2008 - 2010, Pascal Volk
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     4
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     5
"""Virtual Mail Manager's Transport class to manage the transport for
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     6
domains and accounts."""
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
185
6e1ef32fbd82 VMM/*: Moved some methods from classes to modules __init__.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 162
diff changeset
     8
import VirtualMailManager.constants.ERROR as ERR
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
     9
from VirtualMailManager.errors import TransportError
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    11
class Transport(object):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    12
    """A wrapper class that provides access to the transport table"""
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    13
    __slots__ = ('__id', '__transport', '_dbh')
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    14
    def __init__(self, dbh, tid=None, transport=None):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    15
        """Creates a new Transport instance.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
        Either tid or transport must be specified.
128
cf8116625866 Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
    18
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
        Keyword arguments:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
        dbh -- a pyPgSQL.PgSQL.connection
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
        tid -- the id of a transport (long)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
        transport -- the value of the transport (str)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
        """
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
        self._dbh = dbh
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
        if tid is None and transport is None:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    26
            raise TransportError(
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
    27
                _('Either tid or transport must be specified.'),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    28
                ERR.TRANSPORT_INIT)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
        elif tid is not None:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
            try:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
                self.__id = long(tid)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
            except ValueError:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    33
                raise TransportError(_('tid must be an int/long.'),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    34
                    ERR.TRANSPORT_INIT)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
            self._loadByID()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
        else:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
            self.__transport = transport
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
            self._loadByName()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    40
    def __eq__(self, other):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    41
        if isinstance(other, self.__class__):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    42
            return self.__id == other.getID()
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    43
        return NotImplemented
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    44
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    45
    def __ne__(self, other):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    46
        if isinstance(other, self.__class__):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    47
            return self.__id != other.getID()
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    48
        return NotImplemented
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    49
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    50
    def __str__(self):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    51
        return self.__transport
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    52
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    53
    def _loadByID(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    54
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
        dbc.execute('SELECT transport FROM transport WHERE tid = %s', self.__id)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
        result = dbc.fetchone()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
        dbc.close()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        if result is not None:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
            self.__transport = result[0]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        else:
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    61
            raise TransportError(_('Unknown tid specified.'),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    62
                ERR.UNKNOWN_TRANSPORT_ID)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    63
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    64
    def _loadByName(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
        dbc.execute('SELECT tid FROM transport WHERE transport = %s',
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
                self.__transport)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
        result = dbc.fetchone()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        dbc.close()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
        if result is not None:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
            self.__id = result[0]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
        else:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
            self._save()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
    def _save(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
        dbc.execute("SELECT nextval('transport_id')")
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
        self.__id = dbc.fetchone()[0]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
        dbc.execute('INSERT INTO transport (tid, transport) VALUES (%s, %s)',
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
                self.__id, self.__transport)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
        self._dbh.commit()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
        dbc.close()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
    def getID(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
        """Returns the unique ID of the transport."""
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
        return self.__id
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
    def getTransport(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
        """Returns the value of transport, ex: 'dovecot:'"""
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
        return self.__transport