VMM/cli/subcommands: Code cleanup in list_pwschemes().
# -*- coding: UTF-8 -*-# Copyright (c) 2008 - 2012, Pascal Volk# See COPYING for distribution information.""" VirtualMailManager.maillocation ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Virtual Mail Manager's maillocation module to handle Dovecot's mail_location setting for accounts."""fromVirtualMailManager.constantsimportMAILLOCATION_INITfromVirtualMailManager.errorsimportMailLocationErrorasMLErr__all__=('MailLocation','known_format')_=lambdamsg:msg_format_info={'maildir':dict(dovecot_version=0x10000f00,postfix=True),'mdbox':dict(dovecot_version=0x20000b05,postfix=False),'sdbox':dict(dovecot_version=0x20000c03,postfix=False),}classMailLocation(object):"""Class to handle mail_location relevant information."""__slots__=('_directory','_mbfmt','_mid','_dbh')_kwargs=('mid','mbfmt','directory')def__init__(self,dbh,**kwargs):"""Creates a new MailLocation instance. Either the mid keyword or the mbfmt and directory keywords must be specified. Arguments: `dbh` : pyPgSQL.PgSQL.Connection A database connection for the database access. Keyword arguments: `mid` : int the id of a mail_location `mbfmt` : str the mailbox format of the mail_location. One out of: ``maildir``, ``sdbox`` and ``mdbox``. `directory` : str name of the mailbox root directory. """self._dbh=dbhself._directory=Noneself._mbfmt=Noneself._mid=0forkeyinkwargs.iterkeys():ifkeynotinself.__class__._kwargs:raiseValueError('unrecognized keyword: %r'%key)mid=kwargs.get('mid')ifmid:assertisinstance(mid,(int,long))self._load_by_mid(mid)else:args=kwargs.get('mbfmt'),kwargs.get('directory')assertall(isinstance(arg,basestring)forarginargs)ifargs[0].lower()notin_format_info:raiseMLErr(_(u"Unsupported mailbox format: '%s'")%args[0],MAILLOCATION_INIT)directory=args[1].strip()ifnotdirectory:raiseMLErr(_(u"Empty directory name"),MAILLOCATION_INIT)iflen(directory)>20:raiseMLErr(_(u"Directory name is too long: '%s'")%directory,MAILLOCATION_INIT)self._load_by_names(args[0].lower(),directory)def__str__(self):returnu'%s:~/%s'%(self._mbfmt,self._directory)@propertydefdirectory(self):"""The mail_location's directory name."""returnself._directory@propertydefdovecot_version(self):"""The required Dovecot version for this mailbox format."""return_format_info[self._mbfmt]['dovecot_version']@propertydefpostfix(self):"""`True` if Postfix supports this mailbox format, else `False`."""return_format_info[self._mbfmt]['postfix']@propertydefmbformat(self):"""The mail_location's mailbox format."""returnself._mbfmt@propertydefmail_location(self):"""The mail_location, e.g. ``maildir:~/Maildir``"""returnself.__str__()@propertydefmid(self):"""The mail_location's unique ID."""returnself._middef_load_by_mid(self,mid):"""Load mail_location relevant information by *mid*"""dbc=self._dbh.cursor()dbc.execute('SELECT format, directory FROM mailboxformat, ''maillocation WHERE mid = %u AND ''maillocation.fid = mailboxformat.fid'%mid)result=dbc.fetchone()dbc.close()ifnotresult:raiseValueError('Unknown mail_location id specified: %r'%mid)self._mid=midself._mbfmt,self._directory=resultdef_load_by_names(self,mbfmt,directory):"""Try to load mail_location relevant information by *mbfmt* and *directory* name. If it fails goto _save()."""dbc=self._dbh.cursor()dbc.execute("SELECT mid FROM maillocation WHERE fid = (SELECT fid ""FROM mailboxformat WHERE format = %s) AND directory = %s",(mbfmt,directory))result=dbc.fetchone()dbc.close()ifnotresult:self._save(mbfmt,directory)else:self._mid=result[0]self._mbfmt=mbfmtself._directory=directorydef_save(self,mbfmt,directory):"""Save a new mail_location in the database."""dbc=self._dbh.cursor()dbc.execute("SELECT nextval('maillocation_id')")mid=dbc.fetchone()[0]dbc.execute("INSERT INTO maillocation (fid, mid, directory) VALUES (""(SELECT fid FROM mailboxformat WHERE format = %s), %s, ""%s)",(mbfmt,mid,directory))self._dbh.commit()dbc.close()self._mid=midself._mbfmt=mbfmtself._directory=directorydefknown_format(mbfmt):"""Checks if the mailbox format *mbfmt* is known, returns bool."""returnmbfmt.lower()in_format_infodel_