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