VirtualMailManager/Transport.py
author Pascal Volk <neverseen@users.sourceforge.net>
Mon, 26 Jul 2010 03:08:57 +0000
branchv0.6.x
changeset 313 c17c46d9e440
parent 308 aa4a9fc31e1b
child 316 31d8931dc535
permissions -rw-r--r--
VMM/cli/Config: Write warnings to stderr. Renamed method __saveChanges to __save_changes.
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
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     5
"""
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     6
    VirtualMailManager.Transport
308
aa4a9fc31e1b VMM/Transport: Small cosmetics.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 235
diff changeset
     7
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     8
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     9
    Virtual Mail Manager's Transport class to manage the transport for
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    10
    domains and accounts.
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    11
"""
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    12
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    13
from VirtualMailManager.constants.ERROR import UNKNOWN_TRANSPORT_ID
216
0c8c053b451c Moved VirtualMailManager/Exceptions to VirtualMailManager/errors.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 185
diff changeset
    14
from VirtualMailManager.errors import TransportError
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    15
from VirtualMailManager.pycompat import any
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    16
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    17
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    18
class Transport(object):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    19
    """A wrapper class that provides access to the transport table"""
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    20
    __slots__ = ('_tid', '_transport', '_dbh')
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    21
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
    def __init__(self, dbh, tid=None, transport=None):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
        """Creates a new Transport instance.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    25
        Either tid or transport must be specified. When both arguments
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    26
        are given, tid will be used.
128
cf8116625866 Converted VirtualMailManager and Postconf to new-style classes.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 110
diff changeset
    27
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
        Keyword arguments:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    29
        dbh -- a pyPgSQL.PgSQL.connection
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    30
        tid -- the id of a transport (int/long)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    31
        transport -- the value of the transport (str)
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    32
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        """
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    34
        self._dbh = dbh
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    35
        assert any((tid, transport))
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    36
        if tid:
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    37
            assert not isinstance(tid, bool) and isinstance(tid, (int, long))
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    38
            self._tid = tid
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
            self._loadByID()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    40
        else:
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    41
            assert isinstance(transport, basestring)
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    42
            self._transport = transport
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
            self._loadByName()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    44
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    45
    @property
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    46
    def tid(self):
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    47
        """The transport's unique ID."""
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    48
        return self._tid
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    49
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    50
    @property
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    51
    def transport(self):
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    52
        """The transport's value, ex: 'dovecot:'"""
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    53
        return self._transport
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    54
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    55
    def __eq__(self, other):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    56
        if isinstance(other, self.__class__):
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    57
            return self._tid == other.tid
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    58
        return NotImplemented
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    59
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    60
    def __ne__(self, other):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    61
        if isinstance(other, self.__class__):
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    62
            return self._tid != other.tid
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    63
        return NotImplemented
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    64
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    65
    def __str__(self):
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    66
        return self._transport
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    67
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    68
    def _loadByID(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        dbc = self._dbh.cursor()
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    70
        dbc.execute('SELECT transport FROM transport WHERE tid=%s', self._tid)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    71
        result = dbc.fetchone()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    72
        dbc.close()
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    73
        if result:
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    74
            self._transport = result[0]
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    75
        else:
308
aa4a9fc31e1b VMM/Transport: Small cosmetics.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 235
diff changeset
    76
            raise TransportError(_(u'Unknown tid specified.'),
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    77
                                 UNKNOWN_TRANSPORT_ID)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    78
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    79
    def _loadByName(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    80
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    81
        dbc.execute('SELECT tid FROM transport WHERE transport = %s',
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    82
                    self._transport)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    83
        result = dbc.fetchone()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    84
        dbc.close()
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    85
        if result:
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    86
            self._tid = result[0]
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    87
        else:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    88
            self._save()
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 _save(self):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    91
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
        dbc.execute("SELECT nextval('transport_id')")
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    93
        self._tid = dbc.fetchone()[0]
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    94
        dbc.execute('INSERT INTO transport VALUES (%s, %s)', self._tid,
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    95
                    self._transport)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    96
        self._dbh.commit()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
        dbc.close()