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

(yzsuai) #1

browser = ScrolledText(window)
browser.insert('0.0', fulltext)
browser.pack(expand=YES, fill=BOTH)
else:


3.0/4E: more useful PyEdit text editor


wintitle = ' - raw message text'
browser = textEditor.TextEditorMainPopup(self, winTitle=wintitle)
browser.update()
browser.setAllText(fulltext)
browser.clearModified()


def onViewFormatMail(self):
"""
possibly threaded: view selected messages - pop up formatted display
not threaded if in savefile list, or messages are already loaded
the after action runs only if getMessages prefetch allowed and worked
"""
msgnums = self.verifySelectedMsgs()
if msgnums:
self.getMessages(msgnums, after=lambda: self.contViewFmt(msgnums))


def contViewFmt(self, msgnums):
"""
finish View: extract main text, popup view window(s) to display;
extracts plain text from html text if required, wraps text lines;
html mails: show extracted text, then save in temp file and open
in web browser; part can also be opened manually from view window
Split or part button; if non-multipart, other: part must be opened
manually with Split or part button; verify html open per mailconfig;


3.0: for html-only mails, main text is str here, but save its raw
bytes in binary mode to finesse encodings; worth the effort because
many mails are just html today; this first tried N encoding guesses
(utf-8, latin-1, platform dflt), but now gets and saves raw bytes to
minimize any fidelity loss; if a part is later opened on demand, it
is saved in a binary file as raw bytes in the same way;


caveat: the spawned web browser won't have any original email headers:
it may still have to guess or be told the encoding, unless the html
already has its own encoding headers (these take the form of
html tags within sections if present; none are inserted in the
html here, as some well-formed html parts have them); IE seems to
handle most html part files anyhow; always encoding html parts to
utf-8 may suffice too: this encoding can handle most types of text;
"""
for msgnum in msgnums:
fulltext = self.getMessage(msgnum) # 3.0: str for parser
message = self.parseMessage(fulltext)
type, content = self.findMainText(message) # 3.0: Unicode decoded
if type in ['text/html', 'text/xml']: # 3.0: get plain text
content = html2text.html2text(content)
content = wraplines.wrapText1(content, mailconfig.wrapsz)
ViewWindow(headermap = message,
showtext = content,
origmessage = message) # 3.0: decodes headers


PyMailGUI Implementation| 1071
Free download pdf