VMM/EmailAddress: reworked once more.
- moved EmailAddress.__chkLocalpart() -> __module__.check_localpart()
- renamed EmailAddress.__chkAddress() -> EmailAddress._chk_address()
- attributes domainname and localpart are no longer protected
- added missing doc strings.
# -*- coding: UTF-8 -*-# Copyright (c) 2008 - 2010, Pascal Volk# See COPYING for distribution information.""" VirtualMailManager.EmailAddress Virtual Mail Manager's EmailAddress class to handle e-mail addresses."""importrefromVirtualMailManagerimportchk_domainnamefromVirtualMailManager.constants.ERRORimport \DOMAIN_NO_NAME,INVALID_ADDRESS,LOCALPART_INVALID,LOCALPART_TOO_LONGfromVirtualMailManager.ExceptionsimportVMMEmailAddressExceptionasVMMEAERE_LOCALPART="""[^\w!#$%&'\*\+-\.\/=?^_`{\|}~]"""classEmailAddress(object):"""Simple class for validated e-mail addresses."""__slots__=('localpart','domainname')def__init__(self,address):"""Creates a new instance from the string/unicode ``address``."""ifnotisinstance(address,basestring):raiseTypeError('address is not a str/unicode object: %r'%address)self.localpart=Noneself.domainname=Noneself._chk_address(address)def__eq__(self,other):ifisinstance(other,self.__class__):returnself.localpart==other.localpartand \self.domainname==other.domainnamereturnNotImplementeddef__ne__(self,other):ifisinstance(other,self.__class__):returnself.localpart!=other.localpartor \self.domainname!=other.domainnamereturnNotImplementeddef__repr__(self):return"EmailAddress('%s@%s')"%(self.localpart,self.domainname)def__str__(self):return"%s@%s"%(self.localpart,self.domainname)def_chk_address(self,address):"""Checks if the string ``address`` could be used for an e-mail address."""parts=address.split('@')p_len=len(parts)ifp_lenis2:self.localpart=check_localpart(parts[0])iflen(parts[1])>0:self.domainname=chk_domainname(parts[1])else:raiseVMMEAE(_(u"Missing domain name after “%s@”.")%self.localpart,DOMAIN_NO_NAME)elifp_len<2:raiseVMMEAE(_(u"Missing '@' sign in e-mail address “%s”.")%address,INVALID_ADDRESS)elifp_len>2:raiseVMMEAE(_(u"Too many '@' signs in e-mail address “%s”.")%address,INVALID_ADDRESS)_=lambdamsg:msgdefcheck_localpart(localpart):"""Validates the local-part of an e-mail address. Argument: localpart -- local-part of the e-mail address that should be validated """iflen(localpart)<1:raiseVMMEAE(_(u'No local-part specified.'),LOCALPART_INVALID)iflen(localpart)>64:raiseVMMEAE(_(u'The local-part “%s” is too long')%localpart,LOCALPART_TOO_LONG)invalid_chars=set(re.findall(RE_LOCALPART,localpart))ifinvalid_chars:i_chrs=u''.join((u'“%s” '%cforcininvalid_chars))raiseVMMEAE(_(u"The local-part “%(l_part)s” contains invalid\ characters: %(i_chrs)s")%{'l_part':localpart,'i_chrs':i_chrs},LOCALPART_INVALID)returnlocalpartdel_