VirtualMailManager/transport.py
author Pascal Volk <user@localhost.localdomain.org>
Thu, 12 Apr 2012 18:18:02 +0000
branchv0.6.x
changeset 519 b1fd6f08f369
parent 449 6826acb9b0a7
child 568 14abdd04ddf5
permissions -rw-r--r--
README: reStructuredText fix: ERROR/3 and WARNING/2.
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 -*-
366
d6573da35b5f Updated copyright notices to include the year 2011.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 360
diff changeset
     2
# Copyright (c) 2008 - 2011, Pascal Volk
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     4
"""
320
011066435e6f VMM/*: Made all modules names lowercase, adjusted imports.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 316
diff changeset
     5
    VirtualMailManager.transport
308
aa4a9fc31e1b VMM/Transport: Small cosmetics.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 235
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
     8
    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
     9
    domains and accounts.
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    10
"""
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
from VirtualMailManager.pycompat import any
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    13
321
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    14
_ = lambda msg: msg
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    15
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    16
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    17
class Transport(object):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    18
    """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
    19
    __slots__ = ('_tid', '_transport', '_dbh')
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    20
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
    def __init__(self, dbh, tid=None, transport=None):
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    22
        """Creates a new Transport instance.
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    24
        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
    25
        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
    26
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    27
        Keyword arguments:
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    28
        dbh -- a pyPgSQL.PgSQL.connection
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    29
        tid -- the id of a transport (int/long)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    30
        transport -- the value of the transport (str)
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    31
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    32
        """
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    33
        self._dbh = dbh
449
6826acb9b0a7 VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    34
        self._tid = 0
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))
449
6826acb9b0a7 VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    38
            self._load_by_id(tid)
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
        else:
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    40
            assert isinstance(transport, basestring)
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    41
            self._transport = transport
321
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    42
            self._load_by_name()
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    44
    @property
235
9d3405ed08e5 VMM/Transport: Renamed attribute/property {,_}id to {,_}tid.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 225
diff changeset
    45
    def tid(self):
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    46
        """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
    47
        return self._tid
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    48
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    49
    @property
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    50
    def transport(self):
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    51
        """The transport's value, ex: 'dovecot:'"""
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    52
        return self._transport
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    53
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    54
    def __eq__(self, other):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    55
        if isinstance(other, self.__class__):
321
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    56
            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
    57
        return NotImplemented
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    58
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    59
    def __ne__(self, other):
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    60
        if isinstance(other, self.__class__):
321
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    61
            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
    62
        return NotImplemented
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    63
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    64
    def __str__(self):
225
a51809f7940b VMM/Transport: reworked Transport class.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 216
diff changeset
    65
        return self._transport
110
cb8b2f6a5fca Transport: converted to new-style class; Domain: query reduction
Pascal Volk <neverseen@users.sourceforge.net>
parents: 102
diff changeset
    66
449
6826acb9b0a7 VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    67
    def _load_by_id(self, tid):
321
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    68
        """load a transport by its id from the database"""
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    69
        dbc = self._dbh.cursor()
449
6826acb9b0a7 VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    70
        dbc.execute('SELECT transport FROM transport WHERE tid = %s', (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()
449
6826acb9b0a7 VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    73
        if not result:
6826acb9b0a7 VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    74
            raise ValueError('Unknown transport id specified: %r' % tid)
6826acb9b0a7 VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    75
        self._transport = result[0]
6826acb9b0a7 VMM/transport: Reworked Transport initialization.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 379
diff changeset
    76
        self._tid = tid
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    77
321
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    78
    def _load_by_name(self):
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    79
        """Load a transport by its transport name from the database."""
8
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',
352
22d115376e4d VMM/…: Provide parameters as tuple to cursor.execute().
Pascal Volk <neverseen@users.sourceforge.net>
parents: 321
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):
321
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    91
        """Save the new transport in the database."""
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    92
        dbc = self._dbh.cursor()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    93
        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
    94
        self._tid = dbc.fetchone()[0]
379
7518d927d443 VMM/*: Use target column names in all INSERT statements.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    95
        dbc.execute('INSERT INTO transport (tid, transport) VALUES (%s, %s)',
7518d927d443 VMM/*: Use target column names in all INSERT statements.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    96
                    (self._tid, self._transport))
8
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    97
        self._dbh.commit()
7e3ce56f49e6 * 'create_tables.pgsql'
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    98
        dbc.close()
321
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
    99
883d5cd66498 VMM/transport: Converted _mixedCase method names to
Pascal Volk <neverseen@users.sourceforge.net>
parents: 320
diff changeset
   100
del _