UIDMaps: code cleanups and improvements

- Use more 'with' statements.
- Remove dead code: 'dolock' was never used.

Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Nicolas Sebrecht 2016-06-28 03:03:11 +02:00
parent c32b409413
commit cd43cd5b3b

View File

@ -49,15 +49,14 @@ class MappedIMAPFolder(IMAPFolder):
self.getfolderbasename()) self.getfolderbasename())
def _loadmaps(self): def _loadmaps(self):
self.maplock.acquire() with self.maplock:
try:
mapfilename = self._getmapfilename() mapfilename = self._getmapfilename()
if not os.path.exists(mapfilename): if not os.path.exists(mapfilename):
return ({}, {}) return ({}, {})
file = open(mapfilename, 'rt') file = open(mapfilename, 'rt')
r2l = {} r2l = {}
l2r = {} l2r = {}
while 1: while True:
line = file.readline() line = file.readline()
if not len(line): if not len(line):
break break
@ -75,20 +74,13 @@ class MappedIMAPFolder(IMAPFolder):
r2l[rem] = loc r2l[rem] = loc
l2r[loc] = rem l2r[loc] = rem
return (r2l, l2r) return (r2l, l2r)
finally:
self.maplock.release()
def _savemaps(self, dolock = 1): def _savemaps(self):
mapfilename = self._getmapfilename() mapfilename = self._getmapfilename()
if dolock: self.maplock.acquire() with open(mapfilename + ".tmp", 'wt') as mapfilefd:
try:
file = open(mapfilename + ".tmp", 'wt')
for (key, value) in self.diskl2r.items(): for (key, value) in self.diskl2r.items():
file.write("%d:%d\n"% (key, value)) mapfilefd.write("%d:%d\n"% (key, value))
file.close() os.rename(mapfilename + '.tmp', mapfilename)
os.rename(mapfilename + '.tmp', mapfilename)
finally:
if dolock: self.maplock.release()
def _uidlist(self, mapping, items): def _uidlist(self, mapping, items):
try: try:
@ -109,8 +101,7 @@ class MappedIMAPFolder(IMAPFolder):
reallist = self._mb.getmessagelist() reallist = self._mb.getmessagelist()
self.messagelist = self._mb.messagelist self.messagelist = self._mb.messagelist
self.maplock.acquire() with self.maplock:
try:
# OK. Now we've got a nice list. First, delete things from the # OK. Now we've got a nice list. First, delete things from the
# summary that have been deleted from the folder. # summary that have been deleted from the folder.
@ -121,7 +112,7 @@ class MappedIMAPFolder(IMAPFolder):
del self.diskl2r[luid] del self.diskl2r[luid]
# Now, assign negative UIDs to local items. # Now, assign negative UIDs to local items.
self._savemaps(dolock = 0) self._savemaps()
nextneg = -1 nextneg = -1
self.r2l = self.diskr2l.copy() self.r2l = self.diskr2l.copy()
@ -133,8 +124,6 @@ class MappedIMAPFolder(IMAPFolder):
nextneg -= 1 nextneg -= 1
self.l2r[luid] = ruid self.l2r[luid] = ruid
self.r2l[ruid] = luid self.r2l[ruid] = luid
finally:
self.maplock.release()
def dropmessagelistcache(self): def dropmessagelistcache(self):
self._mb.dropmessagelistcache() self._mb.dropmessagelistcache()
@ -170,8 +159,7 @@ class MappedIMAPFolder(IMAPFolder):
retval = {} retval = {}
localhash = self._mb.getmessagelist() localhash = self._mb.getmessagelist()
self.maplock.acquire() with self.maplock:
try:
for key, value in list(localhash.items()): for key, value in list(localhash.items()):
try: try:
key = self.l2r[key] key = self.l2r[key]
@ -185,8 +173,6 @@ class MappedIMAPFolder(IMAPFolder):
value['uid'] = self.l2r[value['uid']] value['uid'] = self.l2r[value['uid']]
retval[key] = value retval[key] = value
return retval return retval
finally:
self.maplock.release()
# Interface from BaseFolder # Interface from BaseFolder
def getmessage(self, uid): def getmessage(self, uid):
@ -228,15 +214,12 @@ class MappedIMAPFolder(IMAPFolder):
if newluid < 1: if newluid < 1:
raise ValueError("Backend could not find uid for message, " raise ValueError("Backend could not find uid for message, "
"returned %s"% newluid) "returned %s"% newluid)
self.maplock.acquire() with self.maplock:
try:
self.diskl2r[newluid] = uid self.diskl2r[newluid] = uid
self.diskr2l[uid] = newluid self.diskr2l[uid] = newluid
self.l2r[newluid] = uid self.l2r[newluid] = uid
self.r2l[uid] = newluid self.r2l[uid] = newluid
self._savemaps(dolock = 0) self._savemaps()
finally:
self.maplock.release()
return uid return uid
# Interface from BaseFolder # Interface from BaseFolder
@ -275,8 +258,7 @@ class MappedIMAPFolder(IMAPFolder):
raise OfflineImapError("Cannot change unknown Maildir UID %s"% raise OfflineImapError("Cannot change unknown Maildir UID %s"%
ruid, OfflineImapError.ERROR.MESSAGE) ruid, OfflineImapError.ERROR.MESSAGE)
if ruid == new_ruid: return # sanity check shortcut if ruid == new_ruid: return # sanity check shortcut
self.maplock.acquire() with self.maplock:
try:
luid = self.r2l[ruid] luid = self.r2l[ruid]
self.l2r[luid] = new_ruid self.l2r[luid] = new_ruid
del self.r2l[ruid] del self.r2l[ruid]
@ -286,13 +268,10 @@ class MappedIMAPFolder(IMAPFolder):
if luid > 0: self.diskl2r[luid] = new_ruid if luid > 0: self.diskl2r[luid] = new_ruid
if ruid > 0: del self.diskr2l[ruid] if ruid > 0: del self.diskr2l[ruid]
if new_ruid > 0: self.diskr2l[new_ruid] = luid if new_ruid > 0: self.diskr2l[new_ruid] = luid
self._savemaps(dolock = 0) self._savemaps()
finally:
self.maplock.release()
def _mapped_delete(self, uidlist): def _mapped_delete(self, uidlist):
self.maplock.acquire() with self.maplock:
try:
needssave = 0 needssave = 0
for ruid in uidlist: for ruid in uidlist:
luid = self.r2l[ruid] luid = self.r2l[ruid]
@ -303,9 +282,7 @@ class MappedIMAPFolder(IMAPFolder):
del self.diskl2r[luid] del self.diskl2r[luid]
needssave = 1 needssave = 1
if needssave: if needssave:
self._savemaps(dolock = 0) self._savemaps()
finally:
self.maplock.release()
# Interface from BaseFolder # Interface from BaseFolder
def deletemessageflags(self, uid, flags): def deletemessageflags(self, uid, flags):