Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 2 | :mod:`mailbox` --- Manipulate mailboxes in various formats |
| 3 | ========================================================== |
| 4 | |
| 5 | .. module:: mailbox |
| 6 | :synopsis: Manipulate mailboxes in various formats |
| 7 | .. moduleauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com> |
| 8 | .. sectionauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com> |
| 9 | |
| 10 | |
| 11 | This module defines two classes, :class:`Mailbox` and :class:`Message`, for |
| 12 | accessing and manipulating on-disk mailboxes and the messages they contain. |
| 13 | :class:`Mailbox` offers a dictionary-like mapping from keys to messages. |
R David Murray | 9faaf1b | 2012-09-28 15:21:32 -0400 | [diff] [blame] | 14 | :class:`Message` extends the :mod:`email.message` module's |
| 15 | :class:`~email.message.Message` class with format-specific state and behavior. |
| 16 | Supported mailbox formats are |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 17 | Maildir, mbox, MH, Babyl, and MMDF. |
| 18 | |
| 19 | |
| 20 | .. seealso:: |
| 21 | |
| 22 | Module :mod:`email` |
| 23 | Represent and manipulate messages. |
| 24 | |
| 25 | |
| 26 | .. _mailbox-objects: |
| 27 | |
| 28 | :class:`Mailbox` objects |
| 29 | ------------------------ |
| 30 | |
| 31 | |
| 32 | .. class:: Mailbox |
| 33 | |
| 34 | A mailbox, which may be inspected and modified. |
| 35 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 36 | The :class:`Mailbox` class defines an interface and is not intended to be |
| 37 | instantiated. Instead, format-specific subclasses should inherit from |
| 38 | :class:`Mailbox` and your code should instantiate a particular subclass. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 39 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 40 | The :class:`Mailbox` interface is dictionary-like, with small keys |
| 41 | corresponding to messages. Keys are issued by the :class:`Mailbox` instance |
| 42 | with which they will be used and are only meaningful to that :class:`Mailbox` |
| 43 | instance. A key continues to identify a message even if the corresponding |
| 44 | message is modified, such as by replacing it with another message. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 45 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 46 | Messages may be added to a :class:`Mailbox` instance using the set-like |
| 47 | method :meth:`add` and removed using a ``del`` statement or the set-like |
| 48 | methods :meth:`remove` and :meth:`discard`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 49 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 50 | :class:`Mailbox` interface semantics differ from dictionary semantics in some |
| 51 | noteworthy ways. Each time a message is requested, a new representation |
| 52 | (typically a :class:`Message` instance) is generated based upon the current |
| 53 | state of the mailbox. Similarly, when a message is added to a |
| 54 | :class:`Mailbox` instance, the provided message representation's contents are |
| 55 | copied. In neither case is a reference to the message representation kept by |
| 56 | the :class:`Mailbox` instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 57 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 58 | The default :class:`Mailbox` iterator iterates over message representations, |
| 59 | not keys as the default dictionary iterator does. Moreover, modification of a |
| 60 | mailbox during iteration is safe and well-defined. Messages added to the |
| 61 | mailbox after an iterator is created will not be seen by the |
| 62 | iterator. Messages removed from the mailbox before the iterator yields them |
| 63 | will be silently skipped, though using a key from an iterator may result in a |
| 64 | :exc:`KeyError` exception if the corresponding message is subsequently |
| 65 | removed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 66 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 67 | .. warning:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 68 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 69 | Be very cautious when modifying mailboxes that might be simultaneously |
| 70 | changed by some other process. The safest mailbox format to use for such |
| 71 | tasks is Maildir; try to avoid using single-file formats such as mbox for |
| 72 | concurrent writing. If you're modifying a mailbox, you *must* lock it by |
| 73 | calling the :meth:`lock` and :meth:`unlock` methods *before* reading any |
| 74 | messages in the file or making any changes by adding or deleting a |
| 75 | message. Failing to lock the mailbox runs the risk of losing messages or |
| 76 | corrupting the entire mailbox. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 77 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 78 | :class:`Mailbox` instances have the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 79 | |
| 80 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 81 | .. method:: add(message) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 82 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 83 | Add *message* to the mailbox and return the key that has been assigned to |
| 84 | it. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 86 | Parameter *message* may be a :class:`Message` instance, an |
R David Murray | 9faaf1b | 2012-09-28 15:21:32 -0400 | [diff] [blame] | 87 | :class:`email.message.Message` instance, a string, or a file-like object |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 88 | (which should be open in text mode). If *message* is an instance of the |
| 89 | appropriate format-specific :class:`Message` subclass (e.g., if it's an |
| 90 | :class:`mboxMessage` instance and this is an :class:`mbox` instance), its |
| 91 | format-specific information is used. Otherwise, reasonable defaults for |
| 92 | format-specific information are used. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 93 | |
| 94 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 95 | .. method:: remove(key) |
| 96 | __delitem__(key) |
| 97 | discard(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 98 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 99 | Delete the message corresponding to *key* from the mailbox. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 100 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 101 | If no such message exists, a :exc:`KeyError` exception is raised if the |
| 102 | method was called as :meth:`remove` or :meth:`__delitem__` but no |
| 103 | exception is raised if the method was called as :meth:`discard`. The |
| 104 | behavior of :meth:`discard` may be preferred if the underlying mailbox |
| 105 | format supports concurrent modification by other processes. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 106 | |
| 107 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 108 | .. method:: __setitem__(key, message) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 109 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 110 | Replace the message corresponding to *key* with *message*. Raise a |
| 111 | :exc:`KeyError` exception if no message already corresponds to *key*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 112 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 113 | As with :meth:`add`, parameter *message* may be a :class:`Message` |
R David Murray | 9faaf1b | 2012-09-28 15:21:32 -0400 | [diff] [blame] | 114 | instance, an :class:`email.message.Message` instance, a string, or a |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 115 | file-like object (which should be open in text mode). If *message* is an |
| 116 | instance of the appropriate format-specific :class:`Message` subclass |
| 117 | (e.g., if it's an :class:`mboxMessage` instance and this is an |
| 118 | :class:`mbox` instance), its format-specific information is |
| 119 | used. Otherwise, the format-specific information of the message that |
| 120 | currently corresponds to *key* is left unchanged. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 121 | |
| 122 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 123 | .. method:: iterkeys() |
| 124 | keys() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 126 | Return an iterator over all keys if called as :meth:`iterkeys` or return a |
| 127 | list of keys if called as :meth:`keys`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 128 | |
| 129 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 130 | .. method:: itervalues() |
| 131 | __iter__() |
| 132 | values() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 133 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 134 | Return an iterator over representations of all messages if called as |
| 135 | :meth:`itervalues` or :meth:`__iter__` or return a list of such |
| 136 | representations if called as :meth:`values`. The messages are represented |
| 137 | as instances of the appropriate format-specific :class:`Message` subclass |
| 138 | unless a custom message factory was specified when the :class:`Mailbox` |
| 139 | instance was initialized. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 140 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 141 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 142 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 143 | The behavior of :meth:`__iter__` is unlike that of dictionaries, which |
| 144 | iterate over keys. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 145 | |
| 146 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 147 | .. method:: iteritems() |
| 148 | items() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 149 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 150 | Return an iterator over (*key*, *message*) pairs, where *key* is a key and |
| 151 | *message* is a message representation, if called as :meth:`iteritems` or |
| 152 | return a list of such pairs if called as :meth:`items`. The messages are |
| 153 | represented as instances of the appropriate format-specific |
| 154 | :class:`Message` subclass unless a custom message factory was specified |
| 155 | when the :class:`Mailbox` instance was initialized. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 156 | |
| 157 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 158 | .. method:: get(key, default=None) |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 159 | __getitem__(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 160 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 161 | Return a representation of the message corresponding to *key*. If no such |
| 162 | message exists, *default* is returned if the method was called as |
| 163 | :meth:`get` and a :exc:`KeyError` exception is raised if the method was |
| 164 | called as :meth:`__getitem__`. The message is represented as an instance |
| 165 | of the appropriate format-specific :class:`Message` subclass unless a |
| 166 | custom message factory was specified when the :class:`Mailbox` instance |
| 167 | was initialized. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 168 | |
| 169 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 170 | .. method:: get_message(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 171 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 172 | Return a representation of the message corresponding to *key* as an |
| 173 | instance of the appropriate format-specific :class:`Message` subclass, or |
| 174 | raise a :exc:`KeyError` exception if no such message exists. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 175 | |
| 176 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 177 | .. method:: get_string(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 178 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 179 | Return a string representation of the message corresponding to *key*, or |
| 180 | raise a :exc:`KeyError` exception if no such message exists. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 181 | |
| 182 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 183 | .. method:: get_file(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 184 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 185 | Return a file-like representation of the message corresponding to *key*, |
| 186 | or raise a :exc:`KeyError` exception if no such message exists. The |
| 187 | file-like object behaves as if open in binary mode. This file should be |
| 188 | closed once it is no longer needed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 189 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 190 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 191 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 192 | Unlike other representations of messages, file-like representations are |
| 193 | not necessarily independent of the :class:`Mailbox` instance that |
| 194 | created them or of the underlying mailbox. More specific documentation |
| 195 | is provided by each subclass. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 196 | |
| 197 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 198 | .. method:: has_key(key) |
| 199 | __contains__(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 200 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 201 | Return ``True`` if *key* corresponds to a message, ``False`` otherwise. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 202 | |
| 203 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 204 | .. method:: __len__() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 205 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 206 | Return a count of messages in the mailbox. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 207 | |
| 208 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 209 | .. method:: clear() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 210 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 211 | Delete all messages from the mailbox. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 212 | |
| 213 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 214 | .. method:: pop(key[, default]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 215 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 216 | Return a representation of the message corresponding to *key* and delete |
| 217 | the message. If no such message exists, return *default* if it was |
| 218 | supplied or else raise a :exc:`KeyError` exception. The message is |
| 219 | represented as an instance of the appropriate format-specific |
| 220 | :class:`Message` subclass unless a custom message factory was specified |
| 221 | when the :class:`Mailbox` instance was initialized. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 222 | |
| 223 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 224 | .. method:: popitem() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 225 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 226 | Return an arbitrary (*key*, *message*) pair, where *key* is a key and |
| 227 | *message* is a message representation, and delete the corresponding |
| 228 | message. If the mailbox is empty, raise a :exc:`KeyError` exception. The |
| 229 | message is represented as an instance of the appropriate format-specific |
| 230 | :class:`Message` subclass unless a custom message factory was specified |
| 231 | when the :class:`Mailbox` instance was initialized. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 232 | |
| 233 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 234 | .. method:: update(arg) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 235 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 236 | Parameter *arg* should be a *key*-to-*message* mapping or an iterable of |
| 237 | (*key*, *message*) pairs. Updates the mailbox so that, for each given |
| 238 | *key* and *message*, the message corresponding to *key* is set to |
| 239 | *message* as if by using :meth:`__setitem__`. As with :meth:`__setitem__`, |
| 240 | each *key* must already correspond to a message in the mailbox or else a |
| 241 | :exc:`KeyError` exception will be raised, so in general it is incorrect |
| 242 | for *arg* to be a :class:`Mailbox` instance. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 243 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 244 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 245 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 246 | Unlike with dictionaries, keyword arguments are not supported. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 247 | |
| 248 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 249 | .. method:: flush() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 250 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 251 | Write any pending changes to the filesystem. For some :class:`Mailbox` |
| 252 | subclasses, changes are always written immediately and :meth:`flush` does |
| 253 | nothing, but you should still make a habit of calling this method. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 254 | |
| 255 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 256 | .. method:: lock() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 257 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 258 | Acquire an exclusive advisory lock on the mailbox so that other processes |
| 259 | know not to modify it. An :exc:`ExternalClashError` is raised if the lock |
| 260 | is not available. The particular locking mechanisms used depend upon the |
| 261 | mailbox format. You should *always* lock the mailbox before making any |
| 262 | modifications to its contents. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 263 | |
| 264 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 265 | .. method:: unlock() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 266 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 267 | Release the lock on the mailbox, if any. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 268 | |
| 269 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 270 | .. method:: close() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 271 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 272 | Flush the mailbox, unlock it if necessary, and close any open files. For |
| 273 | some :class:`Mailbox` subclasses, this method does nothing. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 274 | |
| 275 | |
| 276 | .. _mailbox-maildir: |
| 277 | |
| 278 | :class:`Maildir` |
| 279 | ^^^^^^^^^^^^^^^^ |
| 280 | |
| 281 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 282 | .. class:: Maildir(dirname, factory=rfc822.Message, create=True) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 283 | |
| 284 | A subclass of :class:`Mailbox` for mailboxes in Maildir format. Parameter |
| 285 | *factory* is a callable object that accepts a file-like message representation |
| 286 | (which behaves as if opened in binary mode) and returns a custom representation. |
| 287 | If *factory* is ``None``, :class:`MaildirMessage` is used as the default message |
| 288 | representation. If *create* is ``True``, the mailbox is created if it does not |
| 289 | exist. |
| 290 | |
| 291 | It is for historical reasons that *factory* defaults to :class:`rfc822.Message` |
| 292 | and that *dirname* is named as such rather than *path*. For a :class:`Maildir` |
| 293 | instance that behaves like instances of other :class:`Mailbox` subclasses, set |
| 294 | *factory* to ``None``. |
| 295 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 296 | Maildir is a directory-based mailbox format invented for the qmail mail |
| 297 | transfer agent and now widely supported by other programs. Messages in a |
| 298 | Maildir mailbox are stored in separate files within a common directory |
| 299 | structure. This design allows Maildir mailboxes to be accessed and modified |
| 300 | by multiple unrelated programs without data corruption, so file locking is |
| 301 | unnecessary. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 302 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 303 | Maildir mailboxes contain three subdirectories, namely: :file:`tmp`, |
| 304 | :file:`new`, and :file:`cur`. Messages are created momentarily in the |
| 305 | :file:`tmp` subdirectory and then moved to the :file:`new` subdirectory to |
| 306 | finalize delivery. A mail user agent may subsequently move the message to the |
| 307 | :file:`cur` subdirectory and store information about the state of the message |
| 308 | in a special "info" section appended to its file name. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 309 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 310 | Folders of the style introduced by the Courier mail transfer agent are also |
| 311 | supported. Any subdirectory of the main mailbox is considered a folder if |
| 312 | ``'.'`` is the first character in its name. Folder names are represented by |
| 313 | :class:`Maildir` without the leading ``'.'``. Each folder is itself a Maildir |
| 314 | mailbox but should not contain other folders. Instead, a logical nesting is |
| 315 | indicated using ``'.'`` to delimit levels, e.g., "Archived.2005.07". |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 316 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 317 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 318 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 319 | The Maildir specification requires the use of a colon (``':'``) in certain |
| 320 | message file names. However, some operating systems do not permit this |
| 321 | character in file names, If you wish to use a Maildir-like format on such |
| 322 | an operating system, you should specify another character to use |
| 323 | instead. The exclamation point (``'!'``) is a popular choice. For |
| 324 | example:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 325 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 326 | import mailbox |
| 327 | mailbox.Maildir.colon = '!' |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 328 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 329 | The :attr:`colon` attribute may also be set on a per-instance basis. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 330 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 331 | :class:`Maildir` instances have all of the methods of :class:`Mailbox` in |
| 332 | addition to the following: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 333 | |
| 334 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 335 | .. method:: list_folders() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 336 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 337 | Return a list of the names of all folders. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 338 | |
| 339 | |
Ezio Melotti | 038f38d | 2009-09-15 18:41:43 +0000 | [diff] [blame] | 340 | .. method:: get_folder(folder) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 341 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 342 | Return a :class:`Maildir` instance representing the folder whose name is |
| 343 | *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder |
| 344 | does not exist. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 345 | |
| 346 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 347 | .. method:: add_folder(folder) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 348 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 349 | Create a folder whose name is *folder* and return a :class:`Maildir` |
| 350 | instance representing it. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 351 | |
| 352 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 353 | .. method:: remove_folder(folder) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 354 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 355 | Delete the folder whose name is *folder*. If the folder contains any |
| 356 | messages, a :exc:`NotEmptyError` exception will be raised and the folder |
| 357 | will not be deleted. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 358 | |
| 359 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 360 | .. method:: clean() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 361 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 362 | Delete temporary files from the mailbox that have not been accessed in the |
| 363 | last 36 hours. The Maildir specification says that mail-reading programs |
| 364 | should do this occasionally. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 365 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 366 | Some :class:`Mailbox` methods implemented by :class:`Maildir` deserve special |
| 367 | remarks: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 368 | |
| 369 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 370 | .. method:: add(message) |
| 371 | __setitem__(key, message) |
| 372 | update(arg) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 373 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 374 | .. warning:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 375 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 376 | These methods generate unique file names based upon the current process |
| 377 | ID. When using multiple threads, undetected name clashes may occur and |
| 378 | cause corruption of the mailbox unless threads are coordinated to avoid |
| 379 | using these methods to manipulate the same mailbox simultaneously. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 380 | |
| 381 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 382 | .. method:: flush() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 383 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 384 | All changes to Maildir mailboxes are immediately applied, so this method |
| 385 | does nothing. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 386 | |
| 387 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 388 | .. method:: lock() |
| 389 | unlock() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 390 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 391 | Maildir mailboxes do not support (or require) locking, so these methods do |
| 392 | nothing. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 393 | |
| 394 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 395 | .. method:: close() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 396 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 397 | :class:`Maildir` instances do not keep any open files and the underlying |
| 398 | mailboxes do not support locking, so this method does nothing. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 399 | |
| 400 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 401 | .. method:: get_file(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 402 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 403 | Depending upon the host platform, it may not be possible to modify or |
| 404 | remove the underlying message while the returned file remains open. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 405 | |
| 406 | |
| 407 | .. seealso:: |
| 408 | |
| 409 | `maildir man page from qmail <http://www.qmail.org/man/man5/maildir.html>`_ |
| 410 | The original specification of the format. |
| 411 | |
| 412 | `Using maildir format <http://cr.yp.to/proto/maildir.html>`_ |
| 413 | Notes on Maildir by its inventor. Includes an updated name-creation scheme and |
| 414 | details on "info" semantics. |
| 415 | |
Georg Brandl | 0267781 | 2008-03-15 00:20:19 +0000 | [diff] [blame] | 416 | `maildir man page from Courier <http://www.courier-mta.org/maildir.html>`_ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 417 | Another specification of the format. Describes a common extension for supporting |
| 418 | folders. |
| 419 | |
| 420 | |
| 421 | .. _mailbox-mbox: |
| 422 | |
| 423 | :class:`mbox` |
| 424 | ^^^^^^^^^^^^^ |
| 425 | |
| 426 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 427 | .. class:: mbox(path, factory=None, create=True) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 428 | |
| 429 | A subclass of :class:`Mailbox` for mailboxes in mbox format. Parameter *factory* |
| 430 | is a callable object that accepts a file-like message representation (which |
| 431 | behaves as if opened in binary mode) and returns a custom representation. If |
| 432 | *factory* is ``None``, :class:`mboxMessage` is used as the default message |
| 433 | representation. If *create* is ``True``, the mailbox is created if it does not |
| 434 | exist. |
| 435 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 436 | The mbox format is the classic format for storing mail on Unix systems. All |
| 437 | messages in an mbox mailbox are stored in a single file with the beginning of |
| 438 | each message indicated by a line whose first five characters are "From ". |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 439 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 440 | Several variations of the mbox format exist to address perceived shortcomings in |
| 441 | the original. In the interest of compatibility, :class:`mbox` implements the |
| 442 | original format, which is sometimes referred to as :dfn:`mboxo`. This means that |
| 443 | the :mailheader:`Content-Length` header, if present, is ignored and that any |
| 444 | occurrences of "From " at the beginning of a line in a message body are |
| 445 | transformed to ">From " when storing the message, although occurrences of ">From |
| 446 | " are not transformed to "From " when reading the message. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 447 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 448 | Some :class:`Mailbox` methods implemented by :class:`mbox` deserve special |
| 449 | remarks: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 450 | |
| 451 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 452 | .. method:: get_file(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 453 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 454 | Using the file after calling :meth:`flush` or :meth:`close` on the |
| 455 | :class:`mbox` instance may yield unpredictable results or raise an |
| 456 | exception. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 457 | |
| 458 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 459 | .. method:: lock() |
| 460 | unlock() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 461 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 462 | Three locking mechanisms are used---dot locking and, if available, the |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 463 | :c:func:`flock` and :c:func:`lockf` system calls. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 464 | |
| 465 | |
| 466 | .. seealso:: |
| 467 | |
| 468 | `mbox man page from qmail <http://www.qmail.org/man/man5/mbox.html>`_ |
| 469 | A specification of the format and its variations. |
| 470 | |
| 471 | `mbox man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mbox>`_ |
| 472 | Another specification of the format, with details on locking. |
| 473 | |
Georg Brandl | 0267781 | 2008-03-15 00:20:19 +0000 | [diff] [blame] | 474 | `Configuring Netscape Mail on Unix: Why The Content-Length Format is Bad <http://www.jwz.org/doc/content-length.html>`_ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 475 | An argument for using the original mbox format rather than a variation. |
| 476 | |
| 477 | `"mbox" is a family of several mutually incompatible mailbox formats <http://homepages.tesco.net./~J.deBoynePollard/FGA/mail-mbox-formats.html>`_ |
| 478 | A history of mbox variations. |
| 479 | |
| 480 | |
| 481 | .. _mailbox-mh: |
| 482 | |
| 483 | :class:`MH` |
| 484 | ^^^^^^^^^^^ |
| 485 | |
| 486 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 487 | .. class:: MH(path, factory=None, create=True) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 488 | |
| 489 | A subclass of :class:`Mailbox` for mailboxes in MH format. Parameter *factory* |
| 490 | is a callable object that accepts a file-like message representation (which |
| 491 | behaves as if opened in binary mode) and returns a custom representation. If |
| 492 | *factory* is ``None``, :class:`MHMessage` is used as the default message |
| 493 | representation. If *create* is ``True``, the mailbox is created if it does not |
| 494 | exist. |
| 495 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 496 | MH is a directory-based mailbox format invented for the MH Message Handling |
| 497 | System, a mail user agent. Each message in an MH mailbox resides in its own |
| 498 | file. An MH mailbox may contain other MH mailboxes (called :dfn:`folders`) in |
| 499 | addition to messages. Folders may be nested indefinitely. MH mailboxes also |
| 500 | support :dfn:`sequences`, which are named lists used to logically group |
| 501 | messages without moving them to sub-folders. Sequences are defined in a file |
| 502 | called :file:`.mh_sequences` in each folder. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 503 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 504 | The :class:`MH` class manipulates MH mailboxes, but it does not attempt to |
| 505 | emulate all of :program:`mh`'s behaviors. In particular, it does not modify |
| 506 | and is not affected by the :file:`context` or :file:`.mh_profile` files that |
| 507 | are used by :program:`mh` to store its state and configuration. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 508 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 509 | :class:`MH` instances have all of the methods of :class:`Mailbox` in addition |
| 510 | to the following: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 511 | |
| 512 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 513 | .. method:: list_folders() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 514 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 515 | Return a list of the names of all folders. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 516 | |
| 517 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 518 | .. method:: get_folder(folder) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 519 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 520 | Return an :class:`MH` instance representing the folder whose name is |
| 521 | *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder |
| 522 | does not exist. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 523 | |
| 524 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 525 | .. method:: add_folder(folder) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 526 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 527 | Create a folder whose name is *folder* and return an :class:`MH` instance |
| 528 | representing it. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 529 | |
| 530 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 531 | .. method:: remove_folder(folder) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 532 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 533 | Delete the folder whose name is *folder*. If the folder contains any |
| 534 | messages, a :exc:`NotEmptyError` exception will be raised and the folder |
| 535 | will not be deleted. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 536 | |
| 537 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 538 | .. method:: get_sequences() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 539 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 540 | Return a dictionary of sequence names mapped to key lists. If there are no |
| 541 | sequences, the empty dictionary is returned. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 542 | |
| 543 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 544 | .. method:: set_sequences(sequences) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 545 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 546 | Re-define the sequences that exist in the mailbox based upon *sequences*, |
| 547 | a dictionary of names mapped to key lists, like returned by |
| 548 | :meth:`get_sequences`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 549 | |
| 550 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 551 | .. method:: pack() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 552 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 553 | Rename messages in the mailbox as necessary to eliminate gaps in |
| 554 | numbering. Entries in the sequences list are updated correspondingly. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 555 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 556 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 557 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 558 | Already-issued keys are invalidated by this operation and should not be |
| 559 | subsequently used. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 560 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 561 | Some :class:`Mailbox` methods implemented by :class:`MH` deserve special |
| 562 | remarks: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 563 | |
| 564 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 565 | .. method:: remove(key) |
| 566 | __delitem__(key) |
| 567 | discard(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 568 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 569 | These methods immediately delete the message. The MH convention of marking |
| 570 | a message for deletion by prepending a comma to its name is not used. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 571 | |
| 572 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 573 | .. method:: lock() |
| 574 | unlock() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 575 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 576 | Three locking mechanisms are used---dot locking and, if available, the |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 577 | :c:func:`flock` and :c:func:`lockf` system calls. For MH mailboxes, locking |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 578 | the mailbox means locking the :file:`.mh_sequences` file and, only for the |
| 579 | duration of any operations that affect them, locking individual message |
| 580 | files. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 581 | |
| 582 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 583 | .. method:: get_file(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 584 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 585 | Depending upon the host platform, it may not be possible to remove the |
| 586 | underlying message while the returned file remains open. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 587 | |
| 588 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 589 | .. method:: flush() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 590 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 591 | All changes to MH mailboxes are immediately applied, so this method does |
| 592 | nothing. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 593 | |
| 594 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 595 | .. method:: close() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 596 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 597 | :class:`MH` instances do not keep any open files, so this method is |
| 598 | equivalent to :meth:`unlock`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 599 | |
| 600 | |
| 601 | .. seealso:: |
| 602 | |
| 603 | `nmh - Message Handling System <http://www.nongnu.org/nmh/>`_ |
| 604 | Home page of :program:`nmh`, an updated version of the original :program:`mh`. |
| 605 | |
Georg Brandl | a4314c2 | 2009-10-11 20:16:16 +0000 | [diff] [blame] | 606 | `MH & nmh: Email for Users & Programmers <http://rand-mh.sourceforge.net/book/>`_ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 607 | A GPL-licensed book on :program:`mh` and :program:`nmh`, with some information |
| 608 | on the mailbox format. |
| 609 | |
| 610 | |
| 611 | .. _mailbox-babyl: |
| 612 | |
| 613 | :class:`Babyl` |
| 614 | ^^^^^^^^^^^^^^ |
| 615 | |
| 616 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 617 | .. class:: Babyl(path, factory=None, create=True) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 618 | |
| 619 | A subclass of :class:`Mailbox` for mailboxes in Babyl format. Parameter |
| 620 | *factory* is a callable object that accepts a file-like message representation |
| 621 | (which behaves as if opened in binary mode) and returns a custom representation. |
| 622 | If *factory* is ``None``, :class:`BabylMessage` is used as the default message |
| 623 | representation. If *create* is ``True``, the mailbox is created if it does not |
| 624 | exist. |
| 625 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 626 | Babyl is a single-file mailbox format used by the Rmail mail user agent |
| 627 | included with Emacs. The beginning of a message is indicated by a line |
| 628 | containing the two characters Control-Underscore (``'\037'``) and Control-L |
| 629 | (``'\014'``). The end of a message is indicated by the start of the next |
| 630 | message or, in the case of the last message, a line containing a |
| 631 | Control-Underscore (``'\037'``) character. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 632 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 633 | Messages in a Babyl mailbox have two sets of headers, original headers and |
| 634 | so-called visible headers. Visible headers are typically a subset of the |
| 635 | original headers that have been reformatted or abridged to be more |
| 636 | attractive. Each message in a Babyl mailbox also has an accompanying list of |
| 637 | :dfn:`labels`, or short strings that record extra information about the |
| 638 | message, and a list of all user-defined labels found in the mailbox is kept |
| 639 | in the Babyl options section. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 640 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 641 | :class:`Babyl` instances have all of the methods of :class:`Mailbox` in |
| 642 | addition to the following: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 643 | |
| 644 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 645 | .. method:: get_labels() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 646 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 647 | Return a list of the names of all user-defined labels used in the mailbox. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 648 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 649 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 650 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 651 | The actual messages are inspected to determine which labels exist in |
| 652 | the mailbox rather than consulting the list of labels in the Babyl |
| 653 | options section, but the Babyl section is updated whenever the mailbox |
| 654 | is modified. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 655 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 656 | Some :class:`Mailbox` methods implemented by :class:`Babyl` deserve special |
| 657 | remarks: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 658 | |
| 659 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 660 | .. method:: get_file(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 661 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 662 | In Babyl mailboxes, the headers of a message are not stored contiguously |
| 663 | with the body of the message. To generate a file-like representation, the |
Serhiy Storchaka | 6d5bd52 | 2013-08-29 11:44:44 +0300 | [diff] [blame] | 664 | headers and body are copied together into a :class:`~StringIO.StringIO` instance |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 665 | (from the :mod:`StringIO` module), which has an API identical to that of a |
| 666 | file. As a result, the file-like object is truly independent of the |
| 667 | underlying mailbox but does not save memory compared to a string |
| 668 | representation. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 669 | |
| 670 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 671 | .. method:: lock() |
| 672 | unlock() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 673 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 674 | Three locking mechanisms are used---dot locking and, if available, the |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 675 | :c:func:`flock` and :c:func:`lockf` system calls. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 676 | |
| 677 | |
| 678 | .. seealso:: |
| 679 | |
| 680 | `Format of Version 5 Babyl Files <http://quimby.gnus.org/notes/BABYL>`_ |
| 681 | A specification of the Babyl format. |
| 682 | |
Georg Brandl | 0267781 | 2008-03-15 00:20:19 +0000 | [diff] [blame] | 683 | `Reading Mail with Rmail <http://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html>`_ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 684 | The Rmail manual, with some information on Babyl semantics. |
| 685 | |
| 686 | |
| 687 | .. _mailbox-mmdf: |
| 688 | |
| 689 | :class:`MMDF` |
| 690 | ^^^^^^^^^^^^^ |
| 691 | |
| 692 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 693 | .. class:: MMDF(path, factory=None, create=True) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 694 | |
| 695 | A subclass of :class:`Mailbox` for mailboxes in MMDF format. Parameter *factory* |
| 696 | is a callable object that accepts a file-like message representation (which |
| 697 | behaves as if opened in binary mode) and returns a custom representation. If |
| 698 | *factory* is ``None``, :class:`MMDFMessage` is used as the default message |
| 699 | representation. If *create* is ``True``, the mailbox is created if it does not |
| 700 | exist. |
| 701 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 702 | MMDF is a single-file mailbox format invented for the Multichannel Memorandum |
| 703 | Distribution Facility, a mail transfer agent. Each message is in the same |
| 704 | form as an mbox message but is bracketed before and after by lines containing |
| 705 | four Control-A (``'\001'``) characters. As with the mbox format, the |
| 706 | beginning of each message is indicated by a line whose first five characters |
| 707 | are "From ", but additional occurrences of "From " are not transformed to |
| 708 | ">From " when storing messages because the extra message separator lines |
| 709 | prevent mistaking such occurrences for the starts of subsequent messages. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 710 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 711 | Some :class:`Mailbox` methods implemented by :class:`MMDF` deserve special |
| 712 | remarks: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 713 | |
| 714 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 715 | .. method:: get_file(key) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 716 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 717 | Using the file after calling :meth:`flush` or :meth:`close` on the |
| 718 | :class:`MMDF` instance may yield unpredictable results or raise an |
| 719 | exception. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 720 | |
| 721 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 722 | .. method:: lock() |
| 723 | unlock() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 724 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 725 | Three locking mechanisms are used---dot locking and, if available, the |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 726 | :c:func:`flock` and :c:func:`lockf` system calls. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 727 | |
| 728 | |
| 729 | .. seealso:: |
| 730 | |
| 731 | `mmdf man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mmdf>`_ |
| 732 | A specification of MMDF format from the documentation of tin, a newsreader. |
| 733 | |
| 734 | `MMDF <http://en.wikipedia.org/wiki/MMDF>`_ |
| 735 | A Wikipedia article describing the Multichannel Memorandum Distribution |
| 736 | Facility. |
| 737 | |
| 738 | |
| 739 | .. _mailbox-message-objects: |
| 740 | |
| 741 | :class:`Message` objects |
| 742 | ------------------------ |
| 743 | |
| 744 | |
| 745 | .. class:: Message([message]) |
| 746 | |
R David Murray | 9faaf1b | 2012-09-28 15:21:32 -0400 | [diff] [blame] | 747 | A subclass of the :mod:`email.message` module's |
| 748 | :class:`~email.message.Message`. Subclasses of :class:`mailbox.Message` add |
| 749 | mailbox-format-specific state and behavior. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 750 | |
| 751 | If *message* is omitted, the new instance is created in a default, empty state. |
R David Murray | 9faaf1b | 2012-09-28 15:21:32 -0400 | [diff] [blame] | 752 | If *message* is an :class:`email.message.Message` instance, its contents are |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 753 | copied; furthermore, any format-specific information is converted insofar as |
| 754 | possible if *message* is a :class:`Message` instance. If *message* is a string |
| 755 | or a file, it should contain an :rfc:`2822`\ -compliant message, which is read |
| 756 | and parsed. |
| 757 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 758 | The format-specific state and behaviors offered by subclasses vary, but in |
| 759 | general it is only the properties that are not specific to a particular |
| 760 | mailbox that are supported (although presumably the properties are specific |
| 761 | to a particular mailbox format). For example, file offsets for single-file |
| 762 | mailbox formats and file names for directory-based mailbox formats are not |
| 763 | retained, because they are only applicable to the original mailbox. But state |
| 764 | such as whether a message has been read by the user or marked as important is |
| 765 | retained, because it applies to the message itself. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 766 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 767 | There is no requirement that :class:`Message` instances be used to represent |
| 768 | messages retrieved using :class:`Mailbox` instances. In some situations, the |
| 769 | time and memory required to generate :class:`Message` representations might |
Ezio Melotti | 1e87da1 | 2011-10-19 10:39:35 +0300 | [diff] [blame] | 770 | not be acceptable. For such situations, :class:`Mailbox` instances also |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 771 | offer string and file-like representations, and a custom message factory may |
| 772 | be specified when a :class:`Mailbox` instance is initialized. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 773 | |
| 774 | |
| 775 | .. _mailbox-maildirmessage: |
| 776 | |
| 777 | :class:`MaildirMessage` |
| 778 | ^^^^^^^^^^^^^^^^^^^^^^^ |
| 779 | |
| 780 | |
| 781 | .. class:: MaildirMessage([message]) |
| 782 | |
| 783 | A message with Maildir-specific behaviors. Parameter *message* has the same |
| 784 | meaning as with the :class:`Message` constructor. |
| 785 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 786 | Typically, a mail user agent application moves all of the messages in the |
| 787 | :file:`new` subdirectory to the :file:`cur` subdirectory after the first time |
| 788 | the user opens and closes the mailbox, recording that the messages are old |
| 789 | whether or not they've actually been read. Each message in :file:`cur` has an |
| 790 | "info" section added to its file name to store information about its state. |
| 791 | (Some mail readers may also add an "info" section to messages in |
| 792 | :file:`new`.) The "info" section may take one of two forms: it may contain |
| 793 | "2," followed by a list of standardized flags (e.g., "2,FR") or it may |
| 794 | contain "1," followed by so-called experimental information. Standard flags |
| 795 | for Maildir messages are as follows: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 796 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 797 | +------+---------+--------------------------------+ |
| 798 | | Flag | Meaning | Explanation | |
| 799 | +======+=========+================================+ |
| 800 | | D | Draft | Under composition | |
| 801 | +------+---------+--------------------------------+ |
| 802 | | F | Flagged | Marked as important | |
| 803 | +------+---------+--------------------------------+ |
| 804 | | P | Passed | Forwarded, resent, or bounced | |
| 805 | +------+---------+--------------------------------+ |
| 806 | | R | Replied | Replied to | |
| 807 | +------+---------+--------------------------------+ |
| 808 | | S | Seen | Read | |
| 809 | +------+---------+--------------------------------+ |
| 810 | | T | Trashed | Marked for subsequent deletion | |
| 811 | +------+---------+--------------------------------+ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 812 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 813 | :class:`MaildirMessage` instances offer the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 814 | |
| 815 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 816 | .. method:: get_subdir() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 817 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 818 | Return either "new" (if the message should be stored in the :file:`new` |
| 819 | subdirectory) or "cur" (if the message should be stored in the :file:`cur` |
| 820 | subdirectory). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 821 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 822 | .. note:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 823 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 824 | A message is typically moved from :file:`new` to :file:`cur` after its |
| 825 | mailbox has been accessed, whether or not the message is has been |
| 826 | read. A message ``msg`` has been read if ``"S" in msg.get_flags()`` is |
| 827 | ``True``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 828 | |
| 829 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 830 | .. method:: set_subdir(subdir) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 831 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 832 | Set the subdirectory the message should be stored in. Parameter *subdir* |
| 833 | must be either "new" or "cur". |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 834 | |
| 835 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 836 | .. method:: get_flags() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 837 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 838 | Return a string specifying the flags that are currently set. If the |
| 839 | message complies with the standard Maildir format, the result is the |
| 840 | concatenation in alphabetical order of zero or one occurrence of each of |
| 841 | ``'D'``, ``'F'``, ``'P'``, ``'R'``, ``'S'``, and ``'T'``. The empty string |
| 842 | is returned if no flags are set or if "info" contains experimental |
| 843 | semantics. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 844 | |
| 845 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 846 | .. method:: set_flags(flags) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 847 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 848 | Set the flags specified by *flags* and unset all others. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 849 | |
| 850 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 851 | .. method:: add_flag(flag) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 852 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 853 | Set the flag(s) specified by *flag* without changing other flags. To add |
| 854 | more than one flag at a time, *flag* may be a string of more than one |
| 855 | character. The current "info" is overwritten whether or not it contains |
| 856 | experimental information rather than flags. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 857 | |
| 858 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 859 | .. method:: remove_flag(flag) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 860 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 861 | Unset the flag(s) specified by *flag* without changing other flags. To |
| 862 | remove more than one flag at a time, *flag* maybe a string of more than |
| 863 | one character. If "info" contains experimental information rather than |
| 864 | flags, the current "info" is not modified. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 865 | |
| 866 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 867 | .. method:: get_date() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 868 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 869 | Return the delivery date of the message as a floating-point number |
| 870 | representing seconds since the epoch. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 871 | |
| 872 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 873 | .. method:: set_date(date) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 874 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 875 | Set the delivery date of the message to *date*, a floating-point number |
| 876 | representing seconds since the epoch. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 877 | |
| 878 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 879 | .. method:: get_info() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 880 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 881 | Return a string containing the "info" for a message. This is useful for |
| 882 | accessing and modifying "info" that is experimental (i.e., not a list of |
| 883 | flags). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 884 | |
| 885 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 886 | .. method:: set_info(info) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 887 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 888 | Set "info" to *info*, which should be a string. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 889 | |
| 890 | When a :class:`MaildirMessage` instance is created based upon an |
| 891 | :class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status` |
| 892 | and :mailheader:`X-Status` headers are omitted and the following conversions |
| 893 | take place: |
| 894 | |
| 895 | +--------------------+----------------------------------------------+ |
| 896 | | Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` | |
| 897 | | | state | |
| 898 | +====================+==============================================+ |
| 899 | | "cur" subdirectory | O flag | |
| 900 | +--------------------+----------------------------------------------+ |
| 901 | | F flag | F flag | |
| 902 | +--------------------+----------------------------------------------+ |
| 903 | | R flag | A flag | |
| 904 | +--------------------+----------------------------------------------+ |
| 905 | | S flag | R flag | |
| 906 | +--------------------+----------------------------------------------+ |
| 907 | | T flag | D flag | |
| 908 | +--------------------+----------------------------------------------+ |
| 909 | |
| 910 | When a :class:`MaildirMessage` instance is created based upon an |
| 911 | :class:`MHMessage` instance, the following conversions take place: |
| 912 | |
| 913 | +-------------------------------+--------------------------+ |
| 914 | | Resulting state | :class:`MHMessage` state | |
| 915 | +===============================+==========================+ |
| 916 | | "cur" subdirectory | "unseen" sequence | |
| 917 | +-------------------------------+--------------------------+ |
| 918 | | "cur" subdirectory and S flag | no "unseen" sequence | |
| 919 | +-------------------------------+--------------------------+ |
| 920 | | F flag | "flagged" sequence | |
| 921 | +-------------------------------+--------------------------+ |
| 922 | | R flag | "replied" sequence | |
| 923 | +-------------------------------+--------------------------+ |
| 924 | |
| 925 | When a :class:`MaildirMessage` instance is created based upon a |
| 926 | :class:`BabylMessage` instance, the following conversions take place: |
| 927 | |
| 928 | +-------------------------------+-------------------------------+ |
| 929 | | Resulting state | :class:`BabylMessage` state | |
| 930 | +===============================+===============================+ |
| 931 | | "cur" subdirectory | "unseen" label | |
| 932 | +-------------------------------+-------------------------------+ |
| 933 | | "cur" subdirectory and S flag | no "unseen" label | |
| 934 | +-------------------------------+-------------------------------+ |
| 935 | | P flag | "forwarded" or "resent" label | |
| 936 | +-------------------------------+-------------------------------+ |
| 937 | | R flag | "answered" label | |
| 938 | +-------------------------------+-------------------------------+ |
| 939 | | T flag | "deleted" label | |
| 940 | +-------------------------------+-------------------------------+ |
| 941 | |
| 942 | |
| 943 | .. _mailbox-mboxmessage: |
| 944 | |
| 945 | :class:`mboxMessage` |
| 946 | ^^^^^^^^^^^^^^^^^^^^ |
| 947 | |
| 948 | |
| 949 | .. class:: mboxMessage([message]) |
| 950 | |
| 951 | A message with mbox-specific behaviors. Parameter *message* has the same meaning |
| 952 | as with the :class:`Message` constructor. |
| 953 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 954 | Messages in an mbox mailbox are stored together in a single file. The |
| 955 | sender's envelope address and the time of delivery are typically stored in a |
| 956 | line beginning with "From " that is used to indicate the start of a message, |
| 957 | though there is considerable variation in the exact format of this data among |
| 958 | mbox implementations. Flags that indicate the state of the message, such as |
| 959 | whether it has been read or marked as important, are typically stored in |
| 960 | :mailheader:`Status` and :mailheader:`X-Status` headers. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 961 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 962 | Conventional flags for mbox messages are as follows: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 963 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 964 | +------+----------+--------------------------------+ |
| 965 | | Flag | Meaning | Explanation | |
| 966 | +======+==========+================================+ |
| 967 | | R | Read | Read | |
| 968 | +------+----------+--------------------------------+ |
| 969 | | O | Old | Previously detected by MUA | |
| 970 | +------+----------+--------------------------------+ |
| 971 | | D | Deleted | Marked for subsequent deletion | |
| 972 | +------+----------+--------------------------------+ |
| 973 | | F | Flagged | Marked as important | |
| 974 | +------+----------+--------------------------------+ |
| 975 | | A | Answered | Replied to | |
| 976 | +------+----------+--------------------------------+ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 977 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 978 | The "R" and "O" flags are stored in the :mailheader:`Status` header, and the |
| 979 | "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The |
| 980 | flags and headers typically appear in the order mentioned. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 981 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 982 | :class:`mboxMessage` instances offer the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 983 | |
| 984 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 985 | .. method:: get_from() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 986 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 987 | Return a string representing the "From " line that marks the start of the |
| 988 | message in an mbox mailbox. The leading "From " and the trailing newline |
| 989 | are excluded. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 990 | |
| 991 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 992 | .. method:: set_from(from_, time_=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 993 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 994 | Set the "From " line to *from_*, which should be specified without a |
| 995 | leading "From " or trailing newline. For convenience, *time_* may be |
| 996 | specified and will be formatted appropriately and appended to *from_*. If |
Serhiy Storchaka | b33336f | 2013-10-13 23:09:00 +0300 | [diff] [blame^] | 997 | *time_* is specified, it should be a :class:`time.struct_time` instance, a |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 998 | tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use |
| 999 | :meth:`time.gmtime`). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1000 | |
| 1001 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1002 | .. method:: get_flags() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1003 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1004 | Return a string specifying the flags that are currently set. If the |
| 1005 | message complies with the conventional format, the result is the |
| 1006 | concatenation in the following order of zero or one occurrence of each of |
| 1007 | ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1008 | |
| 1009 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1010 | .. method:: set_flags(flags) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1011 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1012 | Set the flags specified by *flags* and unset all others. Parameter *flags* |
| 1013 | should be the concatenation in any order of zero or more occurrences of |
| 1014 | each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1015 | |
| 1016 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1017 | .. method:: add_flag(flag) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1018 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1019 | Set the flag(s) specified by *flag* without changing other flags. To add |
| 1020 | more than one flag at a time, *flag* may be a string of more than one |
| 1021 | character. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1022 | |
| 1023 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1024 | .. method:: remove_flag(flag) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1025 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1026 | Unset the flag(s) specified by *flag* without changing other flags. To |
| 1027 | remove more than one flag at a time, *flag* maybe a string of more than |
| 1028 | one character. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1029 | |
| 1030 | When an :class:`mboxMessage` instance is created based upon a |
| 1031 | :class:`MaildirMessage` instance, a "From " line is generated based upon the |
| 1032 | :class:`MaildirMessage` instance's delivery date, and the following conversions |
| 1033 | take place: |
| 1034 | |
| 1035 | +-----------------+-------------------------------+ |
| 1036 | | Resulting state | :class:`MaildirMessage` state | |
| 1037 | +=================+===============================+ |
| 1038 | | R flag | S flag | |
| 1039 | +-----------------+-------------------------------+ |
| 1040 | | O flag | "cur" subdirectory | |
| 1041 | +-----------------+-------------------------------+ |
| 1042 | | D flag | T flag | |
| 1043 | +-----------------+-------------------------------+ |
| 1044 | | F flag | F flag | |
| 1045 | +-----------------+-------------------------------+ |
| 1046 | | A flag | R flag | |
| 1047 | +-----------------+-------------------------------+ |
| 1048 | |
| 1049 | When an :class:`mboxMessage` instance is created based upon an |
| 1050 | :class:`MHMessage` instance, the following conversions take place: |
| 1051 | |
| 1052 | +-------------------+--------------------------+ |
| 1053 | | Resulting state | :class:`MHMessage` state | |
| 1054 | +===================+==========================+ |
| 1055 | | R flag and O flag | no "unseen" sequence | |
| 1056 | +-------------------+--------------------------+ |
| 1057 | | O flag | "unseen" sequence | |
| 1058 | +-------------------+--------------------------+ |
| 1059 | | F flag | "flagged" sequence | |
| 1060 | +-------------------+--------------------------+ |
| 1061 | | A flag | "replied" sequence | |
| 1062 | +-------------------+--------------------------+ |
| 1063 | |
| 1064 | When an :class:`mboxMessage` instance is created based upon a |
| 1065 | :class:`BabylMessage` instance, the following conversions take place: |
| 1066 | |
| 1067 | +-------------------+-----------------------------+ |
| 1068 | | Resulting state | :class:`BabylMessage` state | |
| 1069 | +===================+=============================+ |
| 1070 | | R flag and O flag | no "unseen" label | |
| 1071 | +-------------------+-----------------------------+ |
| 1072 | | O flag | "unseen" label | |
| 1073 | +-------------------+-----------------------------+ |
| 1074 | | D flag | "deleted" label | |
| 1075 | +-------------------+-----------------------------+ |
| 1076 | | A flag | "answered" label | |
| 1077 | +-------------------+-----------------------------+ |
| 1078 | |
| 1079 | When a :class:`Message` instance is created based upon an :class:`MMDFMessage` |
| 1080 | instance, the "From " line is copied and all flags directly correspond: |
| 1081 | |
| 1082 | +-----------------+----------------------------+ |
| 1083 | | Resulting state | :class:`MMDFMessage` state | |
| 1084 | +=================+============================+ |
| 1085 | | R flag | R flag | |
| 1086 | +-----------------+----------------------------+ |
| 1087 | | O flag | O flag | |
| 1088 | +-----------------+----------------------------+ |
| 1089 | | D flag | D flag | |
| 1090 | +-----------------+----------------------------+ |
| 1091 | | F flag | F flag | |
| 1092 | +-----------------+----------------------------+ |
| 1093 | | A flag | A flag | |
| 1094 | +-----------------+----------------------------+ |
| 1095 | |
| 1096 | |
| 1097 | .. _mailbox-mhmessage: |
| 1098 | |
| 1099 | :class:`MHMessage` |
| 1100 | ^^^^^^^^^^^^^^^^^^ |
| 1101 | |
| 1102 | |
| 1103 | .. class:: MHMessage([message]) |
| 1104 | |
| 1105 | A message with MH-specific behaviors. Parameter *message* has the same meaning |
| 1106 | as with the :class:`Message` constructor. |
| 1107 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1108 | MH messages do not support marks or flags in the traditional sense, but they |
| 1109 | do support sequences, which are logical groupings of arbitrary messages. Some |
| 1110 | mail reading programs (although not the standard :program:`mh` and |
| 1111 | :program:`nmh`) use sequences in much the same way flags are used with other |
| 1112 | formats, as follows: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1113 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1114 | +----------+------------------------------------------+ |
| 1115 | | Sequence | Explanation | |
| 1116 | +==========+==========================================+ |
| 1117 | | unseen | Not read, but previously detected by MUA | |
| 1118 | +----------+------------------------------------------+ |
| 1119 | | replied | Replied to | |
| 1120 | +----------+------------------------------------------+ |
| 1121 | | flagged | Marked as important | |
| 1122 | +----------+------------------------------------------+ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1123 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1124 | :class:`MHMessage` instances offer the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1125 | |
| 1126 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1127 | .. method:: get_sequences() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1128 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1129 | Return a list of the names of sequences that include this message. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1130 | |
| 1131 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1132 | .. method:: set_sequences(sequences) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1133 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1134 | Set the list of sequences that include this message. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1135 | |
| 1136 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1137 | .. method:: add_sequence(sequence) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1138 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1139 | Add *sequence* to the list of sequences that include this message. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1140 | |
| 1141 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1142 | .. method:: remove_sequence(sequence) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1143 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1144 | Remove *sequence* from the list of sequences that include this message. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1145 | |
| 1146 | When an :class:`MHMessage` instance is created based upon a |
| 1147 | :class:`MaildirMessage` instance, the following conversions take place: |
| 1148 | |
| 1149 | +--------------------+-------------------------------+ |
| 1150 | | Resulting state | :class:`MaildirMessage` state | |
| 1151 | +====================+===============================+ |
| 1152 | | "unseen" sequence | no S flag | |
| 1153 | +--------------------+-------------------------------+ |
| 1154 | | "replied" sequence | R flag | |
| 1155 | +--------------------+-------------------------------+ |
| 1156 | | "flagged" sequence | F flag | |
| 1157 | +--------------------+-------------------------------+ |
| 1158 | |
| 1159 | When an :class:`MHMessage` instance is created based upon an |
| 1160 | :class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status` |
| 1161 | and :mailheader:`X-Status` headers are omitted and the following conversions |
| 1162 | take place: |
| 1163 | |
| 1164 | +--------------------+----------------------------------------------+ |
| 1165 | | Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` | |
| 1166 | | | state | |
| 1167 | +====================+==============================================+ |
| 1168 | | "unseen" sequence | no R flag | |
| 1169 | +--------------------+----------------------------------------------+ |
| 1170 | | "replied" sequence | A flag | |
| 1171 | +--------------------+----------------------------------------------+ |
| 1172 | | "flagged" sequence | F flag | |
| 1173 | +--------------------+----------------------------------------------+ |
| 1174 | |
| 1175 | When an :class:`MHMessage` instance is created based upon a |
| 1176 | :class:`BabylMessage` instance, the following conversions take place: |
| 1177 | |
| 1178 | +--------------------+-----------------------------+ |
| 1179 | | Resulting state | :class:`BabylMessage` state | |
| 1180 | +====================+=============================+ |
| 1181 | | "unseen" sequence | "unseen" label | |
| 1182 | +--------------------+-----------------------------+ |
| 1183 | | "replied" sequence | "answered" label | |
| 1184 | +--------------------+-----------------------------+ |
| 1185 | |
| 1186 | |
| 1187 | .. _mailbox-babylmessage: |
| 1188 | |
| 1189 | :class:`BabylMessage` |
| 1190 | ^^^^^^^^^^^^^^^^^^^^^ |
| 1191 | |
| 1192 | |
| 1193 | .. class:: BabylMessage([message]) |
| 1194 | |
| 1195 | A message with Babyl-specific behaviors. Parameter *message* has the same |
| 1196 | meaning as with the :class:`Message` constructor. |
| 1197 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1198 | Certain message labels, called :dfn:`attributes`, are defined by convention |
| 1199 | to have special meanings. The attributes are as follows: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1200 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1201 | +-----------+------------------------------------------+ |
| 1202 | | Label | Explanation | |
| 1203 | +===========+==========================================+ |
| 1204 | | unseen | Not read, but previously detected by MUA | |
| 1205 | +-----------+------------------------------------------+ |
| 1206 | | deleted | Marked for subsequent deletion | |
| 1207 | +-----------+------------------------------------------+ |
| 1208 | | filed | Copied to another file or mailbox | |
| 1209 | +-----------+------------------------------------------+ |
| 1210 | | answered | Replied to | |
| 1211 | +-----------+------------------------------------------+ |
| 1212 | | forwarded | Forwarded | |
| 1213 | +-----------+------------------------------------------+ |
| 1214 | | edited | Modified by the user | |
| 1215 | +-----------+------------------------------------------+ |
| 1216 | | resent | Resent | |
| 1217 | +-----------+------------------------------------------+ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1218 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1219 | By default, Rmail displays only visible headers. The :class:`BabylMessage` |
| 1220 | class, though, uses the original headers because they are more |
| 1221 | complete. Visible headers may be accessed explicitly if desired. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1222 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1223 | :class:`BabylMessage` instances offer the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1224 | |
| 1225 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1226 | .. method:: get_labels() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1227 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1228 | Return a list of labels on the message. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1229 | |
| 1230 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1231 | .. method:: set_labels(labels) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1232 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1233 | Set the list of labels on the message to *labels*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1234 | |
| 1235 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1236 | .. method:: add_label(label) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1237 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1238 | Add *label* to the list of labels on the message. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1239 | |
| 1240 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1241 | .. method:: remove_label(label) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1242 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1243 | Remove *label* from the list of labels on the message. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1244 | |
| 1245 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1246 | .. method:: get_visible() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1247 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1248 | Return an :class:`Message` instance whose headers are the message's |
| 1249 | visible headers and whose body is empty. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1250 | |
| 1251 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1252 | .. method:: set_visible(visible) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1253 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1254 | Set the message's visible headers to be the same as the headers in |
| 1255 | *message*. Parameter *visible* should be a :class:`Message` instance, an |
R David Murray | 9faaf1b | 2012-09-28 15:21:32 -0400 | [diff] [blame] | 1256 | :class:`email.message.Message` instance, a string, or a file-like object |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1257 | (which should be open in text mode). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1258 | |
| 1259 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1260 | .. method:: update_visible() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1261 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1262 | When a :class:`BabylMessage` instance's original headers are modified, the |
| 1263 | visible headers are not automatically modified to correspond. This method |
| 1264 | updates the visible headers as follows: each visible header with a |
| 1265 | corresponding original header is set to the value of the original header, |
| 1266 | each visible header without a corresponding original header is removed, |
| 1267 | and any of :mailheader:`Date`, :mailheader:`From`, :mailheader:`Reply-To`, |
| 1268 | :mailheader:`To`, :mailheader:`CC`, and :mailheader:`Subject` that are |
| 1269 | present in the original headers but not the visible headers are added to |
| 1270 | the visible headers. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1271 | |
| 1272 | When a :class:`BabylMessage` instance is created based upon a |
| 1273 | :class:`MaildirMessage` instance, the following conversions take place: |
| 1274 | |
| 1275 | +-------------------+-------------------------------+ |
| 1276 | | Resulting state | :class:`MaildirMessage` state | |
| 1277 | +===================+===============================+ |
| 1278 | | "unseen" label | no S flag | |
| 1279 | +-------------------+-------------------------------+ |
| 1280 | | "deleted" label | T flag | |
| 1281 | +-------------------+-------------------------------+ |
| 1282 | | "answered" label | R flag | |
| 1283 | +-------------------+-------------------------------+ |
| 1284 | | "forwarded" label | P flag | |
| 1285 | +-------------------+-------------------------------+ |
| 1286 | |
| 1287 | When a :class:`BabylMessage` instance is created based upon an |
| 1288 | :class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status` |
| 1289 | and :mailheader:`X-Status` headers are omitted and the following conversions |
| 1290 | take place: |
| 1291 | |
| 1292 | +------------------+----------------------------------------------+ |
| 1293 | | Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` | |
| 1294 | | | state | |
| 1295 | +==================+==============================================+ |
| 1296 | | "unseen" label | no R flag | |
| 1297 | +------------------+----------------------------------------------+ |
| 1298 | | "deleted" label | D flag | |
| 1299 | +------------------+----------------------------------------------+ |
| 1300 | | "answered" label | A flag | |
| 1301 | +------------------+----------------------------------------------+ |
| 1302 | |
| 1303 | When a :class:`BabylMessage` instance is created based upon an |
| 1304 | :class:`MHMessage` instance, the following conversions take place: |
| 1305 | |
| 1306 | +------------------+--------------------------+ |
| 1307 | | Resulting state | :class:`MHMessage` state | |
| 1308 | +==================+==========================+ |
| 1309 | | "unseen" label | "unseen" sequence | |
| 1310 | +------------------+--------------------------+ |
| 1311 | | "answered" label | "replied" sequence | |
| 1312 | +------------------+--------------------------+ |
| 1313 | |
| 1314 | |
| 1315 | .. _mailbox-mmdfmessage: |
| 1316 | |
| 1317 | :class:`MMDFMessage` |
| 1318 | ^^^^^^^^^^^^^^^^^^^^ |
| 1319 | |
| 1320 | |
| 1321 | .. class:: MMDFMessage([message]) |
| 1322 | |
| 1323 | A message with MMDF-specific behaviors. Parameter *message* has the same meaning |
| 1324 | as with the :class:`Message` constructor. |
| 1325 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1326 | As with message in an mbox mailbox, MMDF messages are stored with the |
| 1327 | sender's address and the delivery date in an initial line beginning with |
| 1328 | "From ". Likewise, flags that indicate the state of the message are |
| 1329 | typically stored in :mailheader:`Status` and :mailheader:`X-Status` headers. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1330 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1331 | Conventional flags for MMDF messages are identical to those of mbox message |
| 1332 | and are as follows: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1333 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1334 | +------+----------+--------------------------------+ |
| 1335 | | Flag | Meaning | Explanation | |
| 1336 | +======+==========+================================+ |
| 1337 | | R | Read | Read | |
| 1338 | +------+----------+--------------------------------+ |
| 1339 | | O | Old | Previously detected by MUA | |
| 1340 | +------+----------+--------------------------------+ |
| 1341 | | D | Deleted | Marked for subsequent deletion | |
| 1342 | +------+----------+--------------------------------+ |
| 1343 | | F | Flagged | Marked as important | |
| 1344 | +------+----------+--------------------------------+ |
| 1345 | | A | Answered | Replied to | |
| 1346 | +------+----------+--------------------------------+ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1347 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1348 | The "R" and "O" flags are stored in the :mailheader:`Status` header, and the |
| 1349 | "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The |
| 1350 | flags and headers typically appear in the order mentioned. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1351 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1352 | :class:`MMDFMessage` instances offer the following methods, which are |
| 1353 | identical to those offered by :class:`mboxMessage`: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1354 | |
| 1355 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1356 | .. method:: get_from() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1357 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1358 | Return a string representing the "From " line that marks the start of the |
| 1359 | message in an mbox mailbox. The leading "From " and the trailing newline |
| 1360 | are excluded. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1361 | |
| 1362 | |
Hynek Schlawack | e58ce01 | 2012-05-22 10:27:40 +0200 | [diff] [blame] | 1363 | .. method:: set_from(from_, time_=None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1364 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1365 | Set the "From " line to *from_*, which should be specified without a |
| 1366 | leading "From " or trailing newline. For convenience, *time_* may be |
| 1367 | specified and will be formatted appropriately and appended to *from_*. If |
Serhiy Storchaka | b33336f | 2013-10-13 23:09:00 +0300 | [diff] [blame^] | 1368 | *time_* is specified, it should be a :class:`time.struct_time` instance, a |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1369 | tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use |
| 1370 | :meth:`time.gmtime`). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1371 | |
| 1372 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1373 | .. method:: get_flags() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1374 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1375 | Return a string specifying the flags that are currently set. If the |
| 1376 | message complies with the conventional format, the result is the |
| 1377 | concatenation in the following order of zero or one occurrence of each of |
| 1378 | ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1379 | |
| 1380 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1381 | .. method:: set_flags(flags) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1382 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1383 | Set the flags specified by *flags* and unset all others. Parameter *flags* |
| 1384 | should be the concatenation in any order of zero or more occurrences of |
| 1385 | each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1386 | |
| 1387 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1388 | .. method:: add_flag(flag) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1389 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1390 | Set the flag(s) specified by *flag* without changing other flags. To add |
| 1391 | more than one flag at a time, *flag* may be a string of more than one |
| 1392 | character. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1393 | |
| 1394 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1395 | .. method:: remove_flag(flag) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1396 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1397 | Unset the flag(s) specified by *flag* without changing other flags. To |
| 1398 | remove more than one flag at a time, *flag* maybe a string of more than |
| 1399 | one character. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1400 | |
| 1401 | When an :class:`MMDFMessage` instance is created based upon a |
| 1402 | :class:`MaildirMessage` instance, a "From " line is generated based upon the |
| 1403 | :class:`MaildirMessage` instance's delivery date, and the following conversions |
| 1404 | take place: |
| 1405 | |
| 1406 | +-----------------+-------------------------------+ |
| 1407 | | Resulting state | :class:`MaildirMessage` state | |
| 1408 | +=================+===============================+ |
| 1409 | | R flag | S flag | |
| 1410 | +-----------------+-------------------------------+ |
| 1411 | | O flag | "cur" subdirectory | |
| 1412 | +-----------------+-------------------------------+ |
| 1413 | | D flag | T flag | |
| 1414 | +-----------------+-------------------------------+ |
| 1415 | | F flag | F flag | |
| 1416 | +-----------------+-------------------------------+ |
| 1417 | | A flag | R flag | |
| 1418 | +-----------------+-------------------------------+ |
| 1419 | |
| 1420 | When an :class:`MMDFMessage` instance is created based upon an |
| 1421 | :class:`MHMessage` instance, the following conversions take place: |
| 1422 | |
| 1423 | +-------------------+--------------------------+ |
| 1424 | | Resulting state | :class:`MHMessage` state | |
| 1425 | +===================+==========================+ |
| 1426 | | R flag and O flag | no "unseen" sequence | |
| 1427 | +-------------------+--------------------------+ |
| 1428 | | O flag | "unseen" sequence | |
| 1429 | +-------------------+--------------------------+ |
| 1430 | | F flag | "flagged" sequence | |
| 1431 | +-------------------+--------------------------+ |
| 1432 | | A flag | "replied" sequence | |
| 1433 | +-------------------+--------------------------+ |
| 1434 | |
| 1435 | When an :class:`MMDFMessage` instance is created based upon a |
| 1436 | :class:`BabylMessage` instance, the following conversions take place: |
| 1437 | |
| 1438 | +-------------------+-----------------------------+ |
| 1439 | | Resulting state | :class:`BabylMessage` state | |
| 1440 | +===================+=============================+ |
| 1441 | | R flag and O flag | no "unseen" label | |
| 1442 | +-------------------+-----------------------------+ |
| 1443 | | O flag | "unseen" label | |
| 1444 | +-------------------+-----------------------------+ |
| 1445 | | D flag | "deleted" label | |
| 1446 | +-------------------+-----------------------------+ |
| 1447 | | A flag | "answered" label | |
| 1448 | +-------------------+-----------------------------+ |
| 1449 | |
| 1450 | When an :class:`MMDFMessage` instance is created based upon an |
| 1451 | :class:`mboxMessage` instance, the "From " line is copied and all flags directly |
| 1452 | correspond: |
| 1453 | |
| 1454 | +-----------------+----------------------------+ |
| 1455 | | Resulting state | :class:`mboxMessage` state | |
| 1456 | +=================+============================+ |
| 1457 | | R flag | R flag | |
| 1458 | +-----------------+----------------------------+ |
| 1459 | | O flag | O flag | |
| 1460 | +-----------------+----------------------------+ |
| 1461 | | D flag | D flag | |
| 1462 | +-----------------+----------------------------+ |
| 1463 | | F flag | F flag | |
| 1464 | +-----------------+----------------------------+ |
| 1465 | | A flag | A flag | |
| 1466 | +-----------------+----------------------------+ |
| 1467 | |
| 1468 | |
| 1469 | Exceptions |
| 1470 | ---------- |
| 1471 | |
| 1472 | The following exception classes are defined in the :mod:`mailbox` module: |
| 1473 | |
| 1474 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1475 | .. exception:: Error() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1476 | |
| 1477 | The based class for all other module-specific exceptions. |
| 1478 | |
| 1479 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1480 | .. exception:: NoSuchMailboxError() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1481 | |
| 1482 | Raised when a mailbox is expected but is not found, such as when instantiating a |
| 1483 | :class:`Mailbox` subclass with a path that does not exist (and with the *create* |
| 1484 | parameter set to ``False``), or when opening a folder that does not exist. |
| 1485 | |
| 1486 | |
Georg Brandl | ac014e9 | 2008-08-11 10:27:31 +0000 | [diff] [blame] | 1487 | .. exception:: NotEmptyError() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1488 | |
| 1489 | Raised when a mailbox is not empty but is expected to be, such as when deleting |
| 1490 | a folder that contains messages. |
| 1491 | |
| 1492 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1493 | .. exception:: ExternalClashError() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1494 | |
| 1495 | Raised when some mailbox-related condition beyond the control of the program |
| 1496 | causes it to be unable to proceed, such as when failing to acquire a lock that |
| 1497 | another program already holds a lock, or when a uniquely-generated file name |
| 1498 | already exists. |
| 1499 | |
| 1500 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 1501 | .. exception:: FormatError() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1502 | |
| 1503 | Raised when the data in a file cannot be parsed, such as when an :class:`MH` |
| 1504 | instance attempts to read a corrupted :file:`.mh_sequences` file. |
| 1505 | |
| 1506 | |
| 1507 | .. _mailbox-deprecated: |
| 1508 | |
| 1509 | Deprecated classes and methods |
| 1510 | ------------------------------ |
| 1511 | |
Georg Brandl | 5a32772 | 2008-05-11 21:54:09 +0000 | [diff] [blame] | 1512 | .. deprecated:: 2.6 |
| 1513 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1514 | Older versions of the :mod:`mailbox` module do not support modification of |
| 1515 | mailboxes, such as adding or removing message, and do not provide classes to |
| 1516 | represent format-specific message properties. For backward compatibility, the |
| 1517 | older mailbox classes are still available, but the newer classes should be used |
Éric Araujo | 06176a8 | 2012-07-02 17:46:40 -0400 | [diff] [blame] | 1518 | in preference to them. The old classes have been removed in Python 3. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1519 | |
| 1520 | Older mailbox objects support only iteration and provide a single public method: |
| 1521 | |
| 1522 | |
| 1523 | .. method:: oldmailbox.next() |
| 1524 | |
| 1525 | Return the next message in the mailbox, created with the optional *factory* |
| 1526 | argument passed into the mailbox object's constructor. By default this is an |
| 1527 | :class:`rfc822.Message` object (see the :mod:`rfc822` module). Depending on the |
| 1528 | mailbox implementation the *fp* attribute of this object may be a true file |
| 1529 | object or a class instance simulating a file object, taking care of things like |
| 1530 | message boundaries if multiple mail messages are contained in a single file, |
| 1531 | etc. If no more messages are available, this method returns ``None``. |
| 1532 | |
| 1533 | Most of the older mailbox classes have names that differ from the current |
| 1534 | mailbox class names, except for :class:`Maildir`. For this reason, the new |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 1535 | :class:`Maildir` class defines a :meth:`!next` method and its constructor differs |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1536 | slightly from those of the other new mailbox classes. |
| 1537 | |
| 1538 | The older mailbox classes whose names are not the same as their newer |
| 1539 | counterparts are as follows: |
| 1540 | |
| 1541 | |
| 1542 | .. class:: UnixMailbox(fp[, factory]) |
| 1543 | |
| 1544 | Access to a classic Unix-style mailbox, where all messages are contained in a |
| 1545 | single file and separated by ``From`` (a.k.a. ``From_``) lines. The file object |
| 1546 | *fp* points to the mailbox file. The optional *factory* parameter is a callable |
| 1547 | that should create new message objects. *factory* is called with one argument, |
Georg Brandl | 9fa61bb | 2009-07-26 14:19:57 +0000 | [diff] [blame] | 1548 | *fp* by the :meth:`!next` method of the mailbox object. The default is the |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1549 | :class:`rfc822.Message` class (see the :mod:`rfc822` module -- and the note |
| 1550 | below). |
| 1551 | |
| 1552 | .. note:: |
| 1553 | |
| 1554 | For reasons of this module's internal implementation, you will probably want to |
| 1555 | open the *fp* object in binary mode. This is especially important on Windows. |
| 1556 | |
| 1557 | For maximum portability, messages in a Unix-style mailbox are separated by any |
| 1558 | line that begins exactly with the string ``'From '`` (note the trailing space) |
| 1559 | if preceded by exactly two newlines. Because of the wide-range of variations in |
| 1560 | practice, nothing else on the ``From_`` line should be considered. However, the |
| 1561 | current implementation doesn't check for the leading two newlines. This is |
| 1562 | usually fine for most applications. |
| 1563 | |
| 1564 | The :class:`UnixMailbox` class implements a more strict version of ``From_`` |
| 1565 | line checking, using a regular expression that usually correctly matched |
| 1566 | ``From_`` delimiters. It considers delimiter line to be separated by ``From |
| 1567 | name time`` lines. For maximum portability, use the |
| 1568 | :class:`PortableUnixMailbox` class instead. This class is identical to |
| 1569 | :class:`UnixMailbox` except that individual messages are separated by only |
| 1570 | ``From`` lines. |
| 1571 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1572 | |
| 1573 | .. class:: PortableUnixMailbox(fp[, factory]) |
| 1574 | |
| 1575 | A less-strict version of :class:`UnixMailbox`, which considers only the ``From`` |
| 1576 | at the beginning of the line separating messages. The "*name* *time*" portion |
| 1577 | of the From line is ignored, to protect against some variations that are |
| 1578 | observed in practice. This works since lines in the message which begin with |
| 1579 | ``'From '`` are quoted by mail handling software at delivery-time. |
| 1580 | |
| 1581 | |
| 1582 | .. class:: MmdfMailbox(fp[, factory]) |
| 1583 | |
| 1584 | Access an MMDF-style mailbox, where all messages are contained in a single file |
| 1585 | and separated by lines consisting of 4 control-A characters. The file object |
| 1586 | *fp* points to the mailbox file. Optional *factory* is as with the |
| 1587 | :class:`UnixMailbox` class. |
| 1588 | |
| 1589 | |
| 1590 | .. class:: MHMailbox(dirname[, factory]) |
| 1591 | |
| 1592 | Access an MH mailbox, a directory with each message in a separate file with a |
| 1593 | numeric name. The name of the mailbox directory is passed in *dirname*. |
| 1594 | *factory* is as with the :class:`UnixMailbox` class. |
| 1595 | |
| 1596 | |
| 1597 | .. class:: BabylMailbox(fp[, factory]) |
| 1598 | |
| 1599 | Access a Babyl mailbox, which is similar to an MMDF mailbox. In Babyl format, |
| 1600 | each message has two sets of headers, the *original* headers and the *visible* |
| 1601 | headers. The original headers appear before a line containing only ``'*** EOOH |
| 1602 | ***'`` (End-Of-Original-Headers) and the visible headers appear after the |
| 1603 | ``EOOH`` line. Babyl-compliant mail readers will show you only the visible |
| 1604 | headers, and :class:`BabylMailbox` objects will return messages containing only |
| 1605 | the visible headers. You'll have to do your own parsing of the mailbox file to |
| 1606 | get at the original headers. Mail messages start with the EOOH line and end |
| 1607 | with a line containing only ``'\037\014'``. *factory* is as with the |
| 1608 | :class:`UnixMailbox` class. |
| 1609 | |
| 1610 | If you wish to use the older mailbox classes with the :mod:`email` module rather |
| 1611 | than the deprecated :mod:`rfc822` module, you can do so as follows:: |
| 1612 | |
| 1613 | import email |
| 1614 | import email.Errors |
| 1615 | import mailbox |
| 1616 | |
| 1617 | def msgfactory(fp): |
| 1618 | try: |
| 1619 | return email.message_from_file(fp) |
| 1620 | except email.Errors.MessageParseError: |
| 1621 | # Don't return None since that will |
| 1622 | # stop the mailbox iterator |
| 1623 | return '' |
| 1624 | |
| 1625 | mbox = mailbox.UnixMailbox(fp, msgfactory) |
| 1626 | |
| 1627 | Alternatively, if you know your mailbox contains only well-formed MIME messages, |
| 1628 | you can simplify this to:: |
| 1629 | |
| 1630 | import email |
| 1631 | import mailbox |
| 1632 | |
| 1633 | mbox = mailbox.UnixMailbox(fp, email.message_from_file) |
| 1634 | |
| 1635 | |
| 1636 | .. _mailbox-examples: |
| 1637 | |
| 1638 | Examples |
| 1639 | -------- |
| 1640 | |
| 1641 | A simple example of printing the subjects of all messages in a mailbox that seem |
| 1642 | interesting:: |
| 1643 | |
| 1644 | import mailbox |
| 1645 | for message in mailbox.mbox('~/mbox'): |
| 1646 | subject = message['subject'] # Could possibly be None. |
| 1647 | if subject and 'python' in subject.lower(): |
| 1648 | print subject |
| 1649 | |
| 1650 | To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the |
| 1651 | format-specific information that can be converted:: |
| 1652 | |
| 1653 | import mailbox |
| 1654 | destination = mailbox.MH('~/Mail') |
| 1655 | destination.lock() |
| 1656 | for message in mailbox.Babyl('~/RMAIL'): |
Georg Brandl | d85a13a | 2008-03-13 07:15:56 +0000 | [diff] [blame] | 1657 | destination.add(mailbox.MHMessage(message)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1658 | destination.flush() |
| 1659 | destination.unlock() |
| 1660 | |
| 1661 | This example sorts mail from several mailing lists into different mailboxes, |
| 1662 | being careful to avoid mail corruption due to concurrent modification by other |
| 1663 | programs, mail loss due to interruption of the program, or premature termination |
| 1664 | due to malformed messages in the mailbox:: |
| 1665 | |
| 1666 | import mailbox |
| 1667 | import email.Errors |
| 1668 | |
| 1669 | list_names = ('python-list', 'python-dev', 'python-bugs') |
| 1670 | |
| 1671 | boxes = dict((name, mailbox.mbox('~/email/%s' % name)) for name in list_names) |
| 1672 | inbox = mailbox.Maildir('~/Maildir', factory=None) |
| 1673 | |
| 1674 | for key in inbox.iterkeys(): |
| 1675 | try: |
| 1676 | message = inbox[key] |
| 1677 | except email.Errors.MessageParseError: |
| 1678 | continue # The message is malformed. Just leave it. |
| 1679 | |
| 1680 | for name in list_names: |
| 1681 | list_id = message['list-id'] |
| 1682 | if list_id and name in list_id: |
| 1683 | # Get mailbox to use |
| 1684 | box = boxes[name] |
| 1685 | |
| 1686 | # Write copy to disk before removing original. |
| 1687 | # If there's a crash, you might duplicate a message, but |
| 1688 | # that's better than losing a message completely. |
| 1689 | box.lock() |
| 1690 | box.add(message) |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 1691 | box.flush() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1692 | box.unlock() |
| 1693 | |
| 1694 | # Remove original message |
| 1695 | inbox.lock() |
| 1696 | inbox.discard(key) |
| 1697 | inbox.flush() |
| 1698 | inbox.unlock() |
| 1699 | break # Found destination, so stop looking. |
| 1700 | |
| 1701 | for box in boxes.itervalues(): |
| 1702 | box.close() |
| 1703 | |