mailbox --- 以各種格式操作郵件信箱¶
原始碼:Lib/mailbox.py
This module defines two classes, Mailbox and Message, for
accessing and manipulating on-disk mailboxes and the messages they contain.
Mailbox offers a dictionary-like mapping from keys to messages.
Message extends the email.message module's
Message class with format-specific state and behavior.
Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF.
也參考
email模組Represent and manipulate messages.
Mailbox 物件¶
- class mailbox.Mailbox¶
A mailbox, which may be inspected and modified.
The
Mailboxclass defines an interface and is not intended to be instantiated. Instead, format-specific subclasses should inherit fromMailboxand your code should instantiate a particular subclass.The
Mailboxinterface is dictionary-like, with small keys corresponding to messages. Keys are issued by theMailboxinstance with which they will be used and are only meaningful to thatMailboxinstance. A key continues to identify a message even if the corresponding message is modified, such as by replacing it with another message.Messages may be added to a
Mailboxinstance using the set-like methodadd()and removed using adelstatement or the set-like methodsremove()anddiscard().Mailboxinterface semantics differ from dictionary semantics in some noteworthy ways. Each time a message is requested, a new representation (typically aMessageinstance) is generated based upon the current state of the mailbox. Similarly, when a message is added to aMailboxinstance, the provided message representation's contents are copied. In neither case is a reference to the message representation kept by theMailboxinstance.The default
Mailboxiterator iterates over message representations, not keys as the defaultdictionaryiterator does. Moreover, modification of a mailbox during iteration is safe and well-defined. Messages added to the mailbox after an iterator is created will not be seen by the iterator. Messages removed from the mailbox before the iterator yields them will be silently skipped, though using a key from an iterator may result in aKeyErrorexception if the corresponding message is subsequently removed.警告
Be very cautious when modifying mailboxes that might be simultaneously changed by some other process. The safest mailbox format to use for such tasks is
Maildir; try to avoid using single-file formats such asmboxfor concurrent writing. If you're modifying a mailbox, you must lock it by calling thelock()andunlock()methods before reading any messages in the file or making any changes by adding or deleting a message. Failing to lock the mailbox runs the risk of losing messages or corrupting the entire mailbox.Mailboxinstances have the following methods:- add(message)¶
Add message to the mailbox and return the key that has been assigned to it.
Parameter message may be a
Messageinstance, anemail.message.Messageinstance, a string, a byte string, or a file-like object (which should be open in binary mode). If message is an instance of the appropriate format-specificMessagesubclass (e.g., if it's anmboxMessageinstance and this is anmboxinstance), its format-specific information is used. Otherwise, reasonable defaults for format-specific information are used.在 3.2 版的變更: Support for binary input was added.
- remove(key)¶
- __delitem__(key)¶
- discard(key)¶
Delete the message corresponding to key from the mailbox.
If no such message exists, a
KeyErrorexception is raised if the method was called asremove()or__delitem__()but no exception is raised if the method was called asdiscard(). The behavior ofdiscard()may be preferred if the underlying mailbox format supports concurrent modification by other processes.
- __setitem__(key, message)¶
Replace the message corresponding to key with message. Raise a
KeyErrorexception if no message already corresponds to key.As with
add(), parameter message may be aMessageinstance, anemail.message.Messageinstance, a string, a byte string, or a file-like object (which should be open in binary mode). If message is an instance of the appropriate format-specificMessagesubclass (e.g., if it's anmboxMessageinstance and this is anmboxinstance), its format-specific information is used. Otherwise, the format-specific information of the message that currently corresponds to key is left unchanged.
- keys()¶
The same as
iterkeys(), except that alistis returned rather than an iterator
- itervalues()¶
- __iter__()¶
Return an iterator over representations of all messages. The messages are represented as instances of the appropriate format-specific
Messagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.備註
The behavior of
__iter__()is unlike that of dictionaries, which iterate over keys.
- values()¶
The same as
itervalues(), except that alistis returned rather than an iterator
- iteritems()¶
Return an iterator over (key, message) pairs, where key is a key and message is a message representation. The messages are represented as instances of the appropriate format-specific
Messagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.
- items()¶
The same as
iteritems(), except that alistof pairs is returned rather than an iterator of pairs.
- get(key, default=None)¶
- __getitem__(key)¶
Return a representation of the message corresponding to key. If no such message exists, default is returned if the method was called as
get()and aKeyErrorexception is raised if the method was called as__getitem__(). The message is represented as an instance of the appropriate format-specificMessagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.
- get_message(key)¶
Return a representation of the message corresponding to key as an instance of the appropriate format-specific
Messagesubclass, or raise aKeyErrorexception if no such message exists.
- get_bytes(key)¶
Return a byte representation of the message corresponding to key, or raise a
KeyErrorexception if no such message exists.在 3.2 版被加入.
- get_string(key)¶
Return a string representation of the message corresponding to key, or raise a
KeyErrorexception if no such message exists. The message is processed throughemail.message.Messageto convert it to a 7bit clean representation.
- get_file(key)¶
Return a file-like representation of the message corresponding to key, or raise a
KeyErrorexception if no such message exists. The file-like object behaves as if open in binary mode. This file should be closed once it is no longer needed.在 3.2 版的變更: The file object really is a binary file; previously it was incorrectly returned in text mode. Also, the file-like object now supports the context manager protocol: you can use a
withstatement to automatically close it.備註
Unlike other representations of messages, file-like representations are not necessarily independent of the
Mailboxinstance that created them or of the underlying mailbox. More specific documentation is provided by each subclass.
- __contains__(key)¶
Return
Trueif key corresponds to a message,Falseotherwise.
- __len__()¶
Return a count of messages in the mailbox.
- clear()¶
Delete all messages from the mailbox.
- pop(key, default=None)¶
Return a representation of the message corresponding to key and delete the message. If no such message exists, return default. The message is represented as an instance of the appropriate format-specific
Messagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.
- popitem()¶
Return an arbitrary (key, message) pair, where key is a key and message is a message representation, and delete the corresponding message. If the mailbox is empty, raise a
KeyErrorexception. The message is represented as an instance of the appropriate format-specificMessagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.
- update(arg)¶
Parameter arg should be a key-to-message mapping or an iterable of (key, message) pairs. Updates the mailbox so that, for each given key and message, the message corresponding to key is set to message as if by using
__setitem__(). As with__setitem__(), each key must already correspond to a message in the mailbox or else aKeyErrorexception will be raised, so in general it is incorrect for arg to be aMailboxinstance.備註
Unlike with dictionaries, keyword arguments are not supported.
- flush()¶
Write any pending changes to the filesystem. For some
Mailboxsubclasses, changes are always written immediately andflush()does nothing, but you should still make a habit of calling this method.
- lock()¶
Acquire an exclusive advisory lock on the mailbox so that other processes know not to modify it. An
ExternalClashErroris raised if the lock is not available. The particular locking mechanisms used depend upon the mailbox format. You should always lock the mailbox before making any modifications to its contents.
- unlock()¶
Release the lock on the mailbox, if any.
- close()¶
Flush the mailbox, unlock it if necessary, and close any open files. For some
Mailboxsubclasses, this method does nothing.
Mailbox 物件¶
- class mailbox.Maildir(dirname, factory=