VirtualMailManager/Transport.py
changeset 8 7e3ce56f49e6
child 32 ceb700bc4a80
equal deleted inserted replaced
7:96761c442dcf 8:7e3ce56f49e6
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: UTF-8 -*-
       
     3 # Copyright 2008 VEB IT
       
     4 # See COPYING for distribution information.
       
     5 # $Id$
       
     6 
       
     7 """Virtual Mail Manager's Transport class to manage the transport for
       
     8 domains and accounts."""
       
     9 
       
    10 from constants.VERSION import VERSION
       
    11 
       
    12 __author__ = 'Pascal Volk <p.volk@veb-it.de>'
       
    13 __version__ = VERSION
       
    14 __revision__ = 'rev '+'$Rev$'.split()[1]
       
    15 __date__ = '$Date$'.split()[1]
       
    16 
       
    17 from Exceptions import VMMTransportException
       
    18 import constants.ERROR as ERR
       
    19 
       
    20 class Transport:
       
    21     """A wrapper class thats provide access to the transport table"""
       
    22     def __init__(self, dbh, tid=None, transport=None):
       
    23         """Creates a new Transport instance.
       
    24 
       
    25         Either tid or transport must be specified.
       
    26         
       
    27         Keyword arguments:
       
    28         dbh -- a pyPgSQL.PgSQL.connection
       
    29         tid -- the id of a transport (long)
       
    30         transport -- the value of the transport (str)
       
    31         """
       
    32         self._dbh = dbh
       
    33         if tid is None and transport is None:
       
    34             raise VMMTransportException(
       
    35                     ('Either tid or transport must be specified.',
       
    36                         ERR.TRANSPORT_INIT))
       
    37         elif tid is not None:
       
    38             try:
       
    39                 self.__id = long(tid)
       
    40             except ValueError:
       
    41                 raise VMMTransportException(('tid must be an int/long.',
       
    42                     ERR.TRANSPORT_INIT))
       
    43             self._loadByID()
       
    44         else:
       
    45             self.__transport = transport
       
    46             self._loadByName()
       
    47 
       
    48     def _loadByID(self):
       
    49         dbc = self._dbh.cursor()
       
    50         dbc.execute('SELECT transport FROM transport WHERE tid = %s', self.__id)
       
    51         result = dbc.fetchone()
       
    52         dbc.close()
       
    53         if result is not None:
       
    54             self.__transport = result[0]
       
    55         else:
       
    56             raise VMMTransportException(('Unknown tid specified.',
       
    57                 ERR.UNKNOWN_TRANSPORT_ID))
       
    58 
       
    59     def _loadByName(self):
       
    60         dbc = self._dbh.cursor()
       
    61         dbc.execute('SELECT tid FROM transport WHERE transport = %s',
       
    62                 self.__transport)
       
    63         result = dbc.fetchone()
       
    64         dbc.close()
       
    65         if result is not None:
       
    66             self.__id = result[0]
       
    67         else:
       
    68             self._save()
       
    69 
       
    70     def _save(self):
       
    71         dbc = self._dbh.cursor()
       
    72         dbc.execute("SELECT nextval('transport_id')")
       
    73         self.__id = dbc.fetchone()[0]
       
    74         dbc.execute('INSERT INTO transport (tid, transport) VALUES (%s, %s)',
       
    75                 self.__id, self.__transport)
       
    76         self._dbh.commit()
       
    77         dbc.close()
       
    78 
       
    79     def getID(self):
       
    80         """Returns the unique ID of the transport."""
       
    81         return self.__id
       
    82 
       
    83     def getTransport(self):
       
    84         """Returns the value of transport, ex: 'dovecot:'"""
       
    85         return self.__transport