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