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