[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

file = open(self.filename, 'r', encoding=mailconfig.fetchEncoding) # 3.0
allmsgs = file.read()
self.msglist = allmsgs.split(saveMailSeparator)[1:] # full text
self.hdrlist = list(map(self.parseHeaders, self.msglist)) # msg objects


def onLoadMailFileExit(self, savetitle):


on thread success


self.title(savetitle) # reset window title to filename
self.fillIndex() # updates GUI: do in main thread
self.lift() # raise my window
self.openFileBusy.decr()


def onLoadMailFileFail(self, exc_info, savetitle):


on thread exception


showerror(appname, 'Error opening "%s"\n%s\n%s' %
((self.filename,) + exc_info[:2]))
printStack(exc_info)
self.destroy() # always close my window?
self.openFileBusy.decr() # not needed if destroy


def addSavedMails(self, fulltextlist):
"""
optimization: extend loaded file lists for mails
newly saved to this window's file; in past called
loadMailThread to reload entire file on save - slow;
must be called in main GUI thread only: updates GUI;
sends still reloads sent file if open: no msg text;
"""
self.msglist.extend(fulltextlist)
self.hdrlist.extend(map(self.parseHeaders, fulltextlist)) # 3.x iter ok
self.fillIndex()
self.lift()


def doDelete(self, msgnums):
"""
simple-minded, but sufficient: rewrite all
nondeleted mails to file; can't just delete
from self.msglist in-place: changes item indexes;
Py2.3 enumerate(L) same as zip(range(len(L)), L)
2.1: now threaded, else N sec pause for large files
"""
if self.openFileBusy:


dont allow parallel open/delete changes


errmsg = 'Cannot delete, file is busy:\n"%s"' % self.filename
showerror(appname, errmsg)
else:
savetitle = self.title()
self.title(appname + ' - ' + 'Deleting...')
self.openFileBusy.incr()
threadtools.startThread(
action = self.deleteMailFile,
args = (msgnums,),
context = (savetitle,),
onExit = self.onDeleteMailFileExit,
onFail = self.onDeleteMailFileFail)


PyMailGUI Implementation| 1079
Free download pdf