VirtualMailManager/Transport.py
branchv0.6.x
changeset 235 9d3405ed08e5
parent 225 a51809f7940b
child 308 aa4a9fc31e1b
equal deleted inserted replaced
234:e88ba0fb1281 235:9d3405ed08e5
    14 from VirtualMailManager.pycompat import any
    14 from VirtualMailManager.pycompat import any
    15 
    15 
    16 
    16 
    17 class Transport(object):
    17 class Transport(object):
    18     """A wrapper class that provides access to the transport table"""
    18     """A wrapper class that provides access to the transport table"""
    19     __slots__ = ('_id', '_transport', '_dbh')
    19     __slots__ = ('_tid', '_transport', '_dbh')
    20 
    20 
    21     def __init__(self, dbh, tid=None, transport=None):
    21     def __init__(self, dbh, tid=None, transport=None):
    22         """Creates a new Transport instance.
    22         """Creates a new Transport instance.
    23 
    23 
    24         Either tid or transport must be specified. When both arguments
    24         Either tid or transport must be specified. When both arguments
    32         """
    32         """
    33         self._dbh = dbh
    33         self._dbh = dbh
    34         assert any((tid, transport))
    34         assert any((tid, transport))
    35         if tid:
    35         if tid:
    36             assert not isinstance(tid, bool) and isinstance(tid, (int, long))
    36             assert not isinstance(tid, bool) and isinstance(tid, (int, long))
    37             self._id = tid
    37             self._tid = tid
    38             self._loadByID()
    38             self._loadByID()
    39         else:
    39         else:
    40             assert isinstance(transport, basestring)
    40             assert isinstance(transport, basestring)
    41             self._transport = transport
    41             self._transport = transport
    42             self._loadByName()
    42             self._loadByName()
    43 
    43 
    44     @property
    44     @property
    45     def id(self):
    45     def tid(self):
    46         """The transport's unique ID."""
    46         """The transport's unique ID."""
    47         return self._id
    47         return self._tid
    48 
    48 
    49     @property
    49     @property
    50     def transport(self):
    50     def transport(self):
    51         """The transport's value, ex: 'dovecot:'"""
    51         """The transport's value, ex: 'dovecot:'"""
    52         return self._transport
    52         return self._transport
    53 
    53 
    54     def __eq__(self, other):
    54     def __eq__(self, other):
    55         if isinstance(other, self.__class__):
    55         if isinstance(other, self.__class__):
    56             return self._id == other.id
    56             return self._tid == other.tid
    57         return NotImplemented
    57         return NotImplemented
    58 
    58 
    59     def __ne__(self, other):
    59     def __ne__(self, other):
    60         if isinstance(other, self.__class__):
    60         if isinstance(other, self.__class__):
    61             return self._id != other.id
    61             return self._tid != other.tid
    62         return NotImplemented
    62         return NotImplemented
    63 
    63 
    64     def __str__(self):
    64     def __str__(self):
    65         return self._transport
    65         return self._transport
    66 
    66 
    67     def _loadByID(self):
    67     def _loadByID(self):
    68         dbc = self._dbh.cursor()
    68         dbc = self._dbh.cursor()
    69         dbc.execute('SELECT transport FROM transport WHERE tid = %s', self._id)
    69         dbc.execute('SELECT transport FROM transport WHERE tid=%s', self._tid)
    70         result = dbc.fetchone()
    70         result = dbc.fetchone()
    71         dbc.close()
    71         dbc.close()
    72         if result:
    72         if result:
    73             self._transport = result[0]
    73             self._transport = result[0]
    74         else:
    74         else:
    80         dbc.execute('SELECT tid FROM transport WHERE transport = %s',
    80         dbc.execute('SELECT tid FROM transport WHERE transport = %s',
    81                     self._transport)
    81                     self._transport)
    82         result = dbc.fetchone()
    82         result = dbc.fetchone()
    83         dbc.close()
    83         dbc.close()
    84         if result:
    84         if result:
    85             self._id = result[0]
    85             self._tid = result[0]
    86         else:
    86         else:
    87             self._save()
    87             self._save()
    88 
    88 
    89     def _save(self):
    89     def _save(self):
    90         dbc = self._dbh.cursor()
    90         dbc = self._dbh.cursor()
    91         dbc.execute("SELECT nextval('transport_id')")
    91         dbc.execute("SELECT nextval('transport_id')")
    92         self._id = dbc.fetchone()[0]
    92         self._tid = dbc.fetchone()[0]
    93         dbc.execute('INSERT INTO transport VALUES (%s, %s)', self._id,
    93         dbc.execute('INSERT INTO transport VALUES (%s, %s)', self._tid,
    94                     self._transport)
    94                     self._transport)
    95         self._dbh.commit()
    95         self._dbh.commit()
    96         dbc.close()
    96         dbc.close()