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