VirtualMailManager/Transport.py
branchv0.6.x
changeset 320 011066435e6f
parent 319 f4956b4ceba1
child 321 883d5cd66498
equal deleted inserted replaced
319:f4956b4ceba1 320:011066435e6f
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright (c) 2008 - 2010, Pascal Volk
       
     3 # See COPYING for distribution information.
       
     4 
       
     5 """
       
     6     VirtualMailManager.Transport
       
     7     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
     8 
       
     9     Virtual Mail Manager's Transport class to manage the transport for
       
    10     domains and accounts.
       
    11 """
       
    12 
       
    13 from VirtualMailManager.constants import UNKNOWN_TRANSPORT_ID
       
    14 from VirtualMailManager.errors import TransportError
       
    15 from VirtualMailManager.pycompat import any
       
    16 
       
    17 
       
    18 class Transport(object):
       
    19     """A wrapper class that provides access to the transport table"""
       
    20     __slots__ = ('_tid', '_transport', '_dbh')
       
    21 
       
    22     def __init__(self, dbh, tid=None, transport=None):
       
    23         """Creates a new Transport instance.
       
    24 
       
    25         Either tid or transport must be specified. When both arguments
       
    26         are given, tid will be used.
       
    27 
       
    28         Keyword arguments:
       
    29         dbh -- a pyPgSQL.PgSQL.connection
       
    30         tid -- the id of a transport (int/long)
       
    31         transport -- the value of the transport (str)
       
    32 
       
    33         """
       
    34         self._dbh = dbh
       
    35         assert any((tid, transport))
       
    36         if tid:
       
    37             assert not isinstance(tid, bool) and isinstance(tid, (int, long))
       
    38             self._tid = tid
       
    39             self._loadByID()
       
    40         else:
       
    41             assert isinstance(transport, basestring)
       
    42             self._transport = transport
       
    43             self._loadByName()
       
    44 
       
    45     @property
       
    46     def tid(self):
       
    47         """The transport's unique ID."""
       
    48         return self._tid
       
    49 
       
    50     @property
       
    51     def transport(self):
       
    52         """The transport's value, ex: 'dovecot:'"""
       
    53         return self._transport
       
    54 
       
    55     def __eq__(self, other):
       
    56         if isinstance(other, self.__class__):
       
    57             return self._tid == other.tid
       
    58         return NotImplemented
       
    59 
       
    60     def __ne__(self, other):
       
    61         if isinstance(other, self.__class__):
       
    62             return self._tid != other.tid
       
    63         return NotImplemented
       
    64 
       
    65     def __str__(self):
       
    66         return self._transport
       
    67 
       
    68     def _loadByID(self):
       
    69         dbc = self._dbh.cursor()
       
    70         dbc.execute('SELECT transport FROM transport WHERE tid=%s', self._tid)
       
    71         result = dbc.fetchone()
       
    72         dbc.close()
       
    73         if result:
       
    74             self._transport = result[0]
       
    75         else:
       
    76             raise TransportError(_(u'Unknown tid specified.'),
       
    77                                  UNKNOWN_TRANSPORT_ID)
       
    78 
       
    79     def _loadByName(self):
       
    80         dbc = self._dbh.cursor()
       
    81         dbc.execute('SELECT tid FROM transport WHERE transport = %s',
       
    82                     self._transport)
       
    83         result = dbc.fetchone()
       
    84         dbc.close()
       
    85         if result:
       
    86             self._tid = result[0]
       
    87         else:
       
    88             self._save()
       
    89 
       
    90     def _save(self):
       
    91         dbc = self._dbh.cursor()
       
    92         dbc.execute("SELECT nextval('transport_id')")
       
    93         self._tid = dbc.fetchone()[0]
       
    94         dbc.execute('INSERT INTO transport VALUES (%s, %s)', self._tid,
       
    95                     self._transport)
       
    96         self._dbh.commit()
       
    97         dbc.close()