VMM/account: Check the account's transport only when we have a transport.
# coding: utf-8# Copyright (c) 2011, Pascal Volk# See COPYING for distribution information.""" VirtualMailManager.serviceset ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Virtual Mail Manager's ServiceSet class for simplified database access to the service_set table."""SERVICES=('smtp','pop3','imap','sieve')cfg_dget=lambdaoption:NoneclassServiceSet(object):"""A wrapper class that provides access to the service_set table. Each ServiceSet object provides following - read only - attributes: `ssid` : long The id of the service set `smtp` : bool Boolean flag for service smtp `pop3` : bool Boolean flag for service pop3 `imap` : bool Boolean flag for service imap `sieve` : bool Boolean flag for service sieve `services` : dict The four services above with boolean values """__slots__=('_ssid','_services','_sieve_col','_dbh')_kwargs=(('ssid',)+SERVICES)def__init__(self,dbh,**kwargs):"""Creates a new ServiceSet instance. Either the 'ssid' keyword argument or one or more of the service arguments ('smtp', 'pop3', 'imap', 'sieve') must be provided. Arguments: `dbh` : pyPgSQL.PgSQL.Connection or psycopg2.extensions.connection A database connection for the database access. Keyword arguments: `ssid` : int The id of the service set (>0) `smtp` : bool Boolean flag for service smtp - default `True` `pop3` : bool Boolean flag for service pop3 - default `True` `imap` : bool Boolean flag for service imap - default `True` `sieve` : bool Boolean flag for service sieve - default `True` """self._dbh=dbhself._ssid=0self._services=dict.fromkeys(SERVICES,True)ifcfg_dget('misc.dovecot_version')<0x10200b02:self._sieve_col='managesieve'else:self._sieve_col='sieve'forkeyinkwargs.iterkeys():ifkeynotinself.__class__._kwargs:raiseValueError('unrecognized keyword: %r'%key)ifkey=='ssid':assertnotisinstance(kwargs[key],bool)and \isinstance(kwargs[key],(int,long))andkwargs[key]>0self._load_by_ssid(kwargs[key])breakelse:assertisinstance(kwargs[key],bool)ifnotkwargs[key]:self._services[key]=kwargs[key]ifnotself._ssid:self._load_by_services()def__eq__(self,other):ifisinstance(other,self.__class__):returnself._ssid==other._ssidreturnNotImplementeddef__ne__(self,other):ifisinstance(other,self.__class__):returnself._ssid!=other._ssidreturnNotImplementeddef__getattr__(self,name):ifnamenotinself.__class__._kwargs:raiseAttributeError('%r object has no attribute %r'%(self.__class__.__name__,name))ifname=='ssid':returnself._ssidelse:returnself._services[name]def__repr__(self):return'%s(%s, %s)'%(self.__class__.__name__,self._dbh,', '.join('%s=%r'%sforsinself._services.iteritems()))def_load_by_services(self):"""Try to load the service_set by it's service combination."""sql=('SELECT ssid FROM service_set WHERE %s'%' AND '.join('%s = %s'%(k,str(v).upper())fork,vinself._services.iteritems()))ifself._sieve_col=='managesieve':sql.replace('sieve',self._sieve_col)dbc=self._dbh.cursor()dbc.execute(sql)result=dbc.fetchone()dbc.close()ifresult:self._ssid=result[0]else:self._save()def_load_by_ssid(self,ssid):"""Try to load the service_set by it's primary key."""dbc=self._dbh.cursor()dbc.execute('SELECT ssid, smtp, pop3, imap, %s'%(self._sieve_col,)+' FROM service_set WHERE ssid = %s',(ssid,))result=dbc.fetchone()dbc.close()ifnotresult:raiseValueError('Unknown service_set id specified: %r'%ssid)self._ssid=result[0]#self._services.update(zip(SERVICES, result[1:]))forkey,valueinzip(SERVICES,result[1:]):# pyPgSQL compatibleifvalue:self._services[key]=Trueelse:self._services[key]=Falsedef_save(self):"""Store a new service_set in the database."""sql=('INSERT INTO service_set (ssid, smtp, pop3, imap, %s) '%(self._sieve_col,)+'VALUES (%(ssid)s, %(smtp)s, %(pop3)s, %(imap)s, %(sieve)s)')ifself._sieve_col=='managesieve':sql.replace('sieve',self._sieve_col)self._set_ssid()values={'ssid':self._ssid}values.update(self._services)dbc=self._dbh.cursor()dbc.execute(sql,values)self._dbh.commit()dbc.close()def_set_ssid(self):"""Set the unique ID for the new service_set."""assertself._ssid==0dbc=self._dbh.cursor()dbc.execute("SELECT nextval('service_set_id')")self._ssid=dbc.fetchone()[0]dbc.close()@propertydefservices(self):"""A dictionary: Keys: `smtp`, `pop3`, `imap` and `sieve` with boolean values."""returnself._services.copy()delcfg_dget