VirtualMailManager/Transport.py
author Pascal Volk <neverseen@users.sourceforge.net>
Tue, 02 Mar 2010 19:24:36 +0000
branchv0.6.x
changeset 227 87db9f1f95ea
parent 225 a51809f7940b
child 235 9d3405ed08e5
permissions -rw-r--r--
VMM/Account: Adjusted to changes in maillocation.MailLocation. VMM/maillocation: Class MailLocation added missing property `mid`. Added new error to VMM/constants/ERROR and doc/source/vmm_constants_error

# -*- coding: UTF-8 -*-
# Copyright (c) 2008 - 2010, Pascal Volk
# See COPYING for distribution information.

"""
    VirtualMailManager.Transport

    Virtual Mail Manager's Transport class to manage the transport for
    domains and accounts.
"""

from VirtualMailManager.constants.ERROR import UNKNOWN_TRANSPORT_ID
from VirtualMailManager.errors import TransportError
from VirtualMailManager.pycompat import any


class Transport(object):
    """A wrapper class that provides access to the transport table"""
    __slots__ = ('_id', '_transport', '_dbh')

    def __init__(self, dbh, tid=None, transport=None):
        """Creates a new Transport instance.

        Either tid or transport must be specified. When both arguments
        are given, tid will be used.

        Keyword arguments:
        dbh -- a pyPgSQL.PgSQL.connection
        tid -- the id of a transport (int/long)
        transport -- the value of the transport (str)

        """
        self._dbh = dbh
        assert any((tid, transport))
        if tid:
            assert not isinstance(tid, bool) and isinstance(tid, (int, long))
            self._id = tid
            self._loadByID()
        else:
            assert isinstance(transport, basestring)
            self._transport = transport
            self._loadByName()

    @property
    def id(self):
        """The transport's unique ID."""
        return self._id

    @property
    def transport(self):
        """The transport's value, ex: 'dovecot:'"""
        return self._transport

    def __eq__(self, other):
        if isinstance(other, self.__class__):
            return self._id == other.id
        return NotImplemented

    def __ne__(self, other):
        if isinstance(other, self.__class__):
            return self._id != other.id
        return NotImplemented

    def __str__(self):
        return self._transport

    def _loadByID(self):
        dbc = self._dbh.cursor()
        dbc.execute('SELECT transport FROM transport WHERE tid = %s', self._id)
        result = dbc.fetchone()
        dbc.close()
        if result:
            self._transport = result[0]
        else:
            raise TransportError(_('Unknown tid specified.'),
                                 UNKNOWN_TRANSPORT_ID)

    def _loadByName(self):
        dbc = self._dbh.cursor()
        dbc.execute('SELECT tid FROM transport WHERE transport = %s',
                    self._transport)
        result = dbc.fetchone()
        dbc.close()
        if result:
            self._id = result[0]
        else:
            self._save()

    def _save(self):
        dbc = self._dbh.cursor()
        dbc.execute("SELECT nextval('transport_id')")
        self._id = dbc.fetchone()[0]
        dbc.execute('INSERT INTO transport VALUES (%s, %s)', self._id,
                    self._transport)
        self._dbh.commit()
        dbc.close()