[PATCH 2/2] Don't fail mysteriously if we cannot get a password
Sebastian
Sebastian at SSpaeth.de
Mon Dec 13 15:33:18 GMT 2010
Rework repository.IMAP.getpassword() to not return None on failure which
leads to mysterious Exceptions in the bowels of imaplib. Rather fail with a
clear exception explaining what went wrong.
Some extended documentation and cleanups in the functino en-passent.
Signed-Off-By: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
offlineimap/repository/IMAP.py | 30 ++++++++++++++++++++++--------
1 files changed, 22 insertions(+), 8 deletions(-)
diff --git a/offlineimap/repository/IMAP.py b/offlineimap/repository/IMAP.py
index 789a64c..1076591 100644
--- a/offlineimap/repository/IMAP.py
+++ b/offlineimap/repository/IMAP.py
@@ -152,24 +152,35 @@ class IMAPRepository(BaseRepository):
return self.getconfboolean('expunge', 1)
def getpassword(self):
- passwd = None
- localeval = self.localeval
+ """Return the IMAP password for this repository.
- if self.config.has_option(self.getsection(), 'remotepasseval'):
- passwd = self.getconf('remotepasseval')
- if passwd != None:
- return localeval.eval(passwd)
+ It tries to get passwords in the following order:
+
+ 1. evaluate Repository 'remotepasseval'
+ 2. read password from Repository 'remotepass'
+ 3. read password from file specified in Repository 'remotepassfile'
+ 4. read password from ~/.netrc
+ 5. read password from /etc/netrc
+ On success we return the password.
+ If all strategies fail, we raise an Exception.
+ """
+ # 1. evaluate Repository 'remotepasseval'
+ passwd = self.getconf('remotepasseval', None)
+ if passwd != None:
+ return self.localeval.eval(passwd)
+ # 2. read password from Repository 'remotepass'
password = self.getconf('remotepass', None)
if password != None:
return password
+ # 3. read password from file specified in Repository 'remotepassfile'
passfile = self.getconf('remotepassfile', None)
if passfile != None:
fd = open(os.path.expanduser(passfile))
password = fd.readline().strip()
fd.close()
return password
-
+ # 4. read password from ~/.netrc
try:
netrcentry = netrc.netrc().authenticators(self.gethost())
except IOError, inst:
@@ -180,6 +191,7 @@ class IMAPRepository(BaseRepository):
user = self.getconf('remoteuser')
if user == None or user == netrcentry[0]:
return netrcentry[2]
+ # 5. read password from /etc/netrc
try:
netrcentry = netrc.netrc('/etc/netrc').authenticators(self.gethost())
except IOError, inst:
@@ -190,7 +202,9 @@ class IMAPRepository(BaseRepository):
user = self.getconf('remoteuser')
if user == None or user == netrcentry[0]:
return netrcentry[2]
- return None
+ # no strategy yielded a password!
+ raise Exception("No password could be retrieved for repository '%s'" % self.getname())
+
def getfolder(self, foldername):
return self.getfoldertype()(self.imapserver, foldername,
--
1.7.1
More information about the OfflineIMAP-project
mailing list