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

(yzsuai) #1

cache). Messages may still be composed during a deletion, however, and offline save
files may be processed.


POP Message Numbers and Synchronization


By now, we’ve seen all the basic functionality of PyMailGUI—enough to get you started
sending and receiving simple but typical text-based messages. In the rest of this demo,
we’re going to turn our attention to some of the deeper concepts in this system, in-
cluding inbox synchronization, HTML mails, Internationalization, and multiple ac-
count configuration. Since the first of these is related to the preceding section’s tour of
mail deletions, let’s begin here.


Though they might seem simple from an end-user perspective, it turns out that deletions
are complicated by POP’s message-numbering scheme. We learned about the potential
for synchronization errors between the server’s inbox and the fetched email list in
Chapter 13, when studying the mailtools package PyMailGUI uses (near Exam-
ple 13-24). In brief, POP assigns each message a relative sequential number, starting
from one, and these numbers are passed to the server to fetch and delete messages. The
server’s inbox is normally locked while a connection is held so that a series of deletions
can be run as an atomic operation; no other inbox changes occur until the connection
is closed.


However, message number changes also have some implications for the GUI itself. It’s
never an issue if new mail arrives while we’re displaying the result of a prior download—
the new mail is assigned higher numbers, beyond what is displayed on the client. But
if we delete a message in the middle of a mailbox after the index has been loaded from
the mail server, the numbers of all messages after the one deleted change (they are
decremented by one). As a result, some message numbers might no longer be valid if
deletions are made while viewing previously loaded email.


To work around this, PyMailGUI adjusts all the displayed numbers after a Delete by
simply removing the entries for deleted mails from its index list and mail cache. How-
ever, this adjustment is not enough to keep the GUI in sync with the server’s inbox if
the inbox is modified at a position other than after the end, by deletions in another
email client (even in another PyMailGUI session), or by deletions performed by the
mail server itself (e.g., messages determined to be undeliverable and automatically re-
moved from the inbox). Such modifications outside PyMailGUI’s scope are uncom-
mon, but not impossible.


To handle these cases, PyMailGUI uses the safe deletion and synchronization tests in
mailtools. That module uses mail header matching to detect mail list and server inbox
synchronization errors. For instance, if another email client has deleted a message prior
to the one to be deleted by PyMailGUI, mailtools catches the problem and cancels the
deletion, and an error pop up like the one in Figure 14-36 is displayed.


A PyMailGUI Demo| 1051
Free download pdf