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