VirtualMailManager/Transport.py
author Pascal Volk <neverseen@users.sourceforge.net>
Fri, 21 Aug 2009 17:42:22 +0000
changeset 128 cf8116625866
parent 110 cb8b2f6a5fca
child 138 617f27715b01
permissions -rw-r--r--
Converted VirtualMailManager and Postconf to new-style classes. A few small cleanups.
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 -*-
102
485d3f7d6981 Removed svn keywords, more POSIX compliant shell scripts
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
     2
# Copyright (c) 2008 - 2009, VEB IT
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
102
485d3f7d6981 Removed svn keywords, more POSIX compliant shell scripts
Pascal Volk <neverseen@users.sourceforge.net>
parents: 76
diff changeset
     8
from constants.VERSION import *
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     9
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
from Exceptions import VMMTransportException
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    11
import constants.ERROR as ERR
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    12
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    13
class Transport(object):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    14
    """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
    15
    __slots__ = ('__id', '__transport', '_dbh')
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
    def __init__(self, dbh, tid=None, transport=None):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
        """Creates a new Transport instance.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    18
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    19
        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
    20
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
        Keyword arguments:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
        dbh -- a pyPgSQL.PgSQL.connection
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
        tid -- the id of a transport (long)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
        transport -- the value of the transport (str)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    25
        """
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    26
        self._dbh = dbh
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
        if tid is None and transport is None:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    28
            raise VMMTransportException(
34
6d74e20c5b3b * 'VirtualMailManager/Account.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 32
diff changeset
    29
                _('Either tid or transport must be specified.'),
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    30
                ERR.TRANSPORT_INIT)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        elif tid is not None:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
            try:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
                self.__id = long(tid)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
            except ValueError:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    35
                raise VMMTransportException(_('tid must be an int/long.'),
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    36
                    ERR.TRANSPORT_INIT)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    37
            self._loadByID()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
        else:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
            self.__transport = transport
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
            self._loadByName()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    41
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    42
    def __eq__(self, other):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    43
        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
    44
            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
    45
        return NotImplemented
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    46
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    47
    def __ne__(self, other):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    48
        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
    49
            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
    50
        return NotImplemented
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    51
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    52
    def __str__(self):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    53
        return self.__transport
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    54
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
    def _loadByID(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    56
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    57
        dbc.execute('SELECT transport FROM transport WHERE tid = %s', self.__id)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    58
        result = dbc.fetchone()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    59
        dbc.close()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    60
        if result is not None:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    61
            self.__transport = result[0]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    62
        else:
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    63
            raise VMMTransportException(_('Unknown tid specified.'),
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    64
                ERR.UNKNOWN_TRANSPORT_ID)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    65
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    66
    def _loadByName(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    67
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
        dbc.execute('SELECT tid FROM transport WHERE transport = %s',
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
                self.__transport)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    70
        result = dbc.fetchone()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
        dbc.close()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
        if result is not None:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    73
            self.__id = result[0]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    74
        else:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
            self._save()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    76
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
    def _save(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
        dbc.execute("SELECT nextval('transport_id')")
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        self.__id = dbc.fetchone()[0]
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
        dbc.execute('INSERT INTO transport (tid, transport) VALUES (%s, %s)',
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    82
                self.__id, self.__transport)
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        self._dbh.commit()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        dbc.close()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    85
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    86
    def getID(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        """Returns the unique ID of the transport."""
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
        return self.__id
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    89
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    90
    def getTransport(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        """Returns the value of transport, ex: 'dovecot:'"""
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
        return self.__transport