annotate Lib/mailbox.py @ 79691:05c8928362ee

Merge __next__ method link fixes with 3.3.
author Ezio Melotti <ezio.melotti@gmail.com>
date Fri, 12 Oct 2012 13:46:37 +0300
parents 15949f34e08f
children 7d69d04522e3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59522
fcfe324663a6 convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents: 57450
diff changeset
1 #! /usr/bin/env python3
4316
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
2
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
3 """Read/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
4
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
5 # Notes for authors of new mailbox subclasses:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
6 #
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
7 # Remember to fsync() changes to disk before closing a modified file
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
8 # or returning from a flush() method. See functions _sync_flush() and
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
9 # _sync_close().
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
10
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
11 import sys
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
12 import os
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
13 import time
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
14 import calendar
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
15 import socket
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
16 import errno
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
17 import copy
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
18 import warnings
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
19 import email
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
20 import email.message
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
21 import email.generator
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42331
diff changeset
22 import io
70843
fea1920ae75f #11767: use context manager to close file in __getitem__ to prevent FD leak
R David Murray <rdmurray@bitdance.com>
parents: 69883
diff changeset
23 import contextlib
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
24 try:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
25 import fcntl
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
26 except ImportError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
27 fcntl = None
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
28
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
29 __all__ = [ 'Mailbox', 'Maildir', 'mbox', 'MH', 'Babyl', 'MMDF',
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
30 'Message', 'MaildirMessage', 'mboxMessage', 'MHMessage',
46893
67e4363ea2e0 fix __all__
Benjamin Peterson <benjamin@python.org>
parents: 46868
diff changeset
31 'BabylMessage', 'MMDFMessage']
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
32
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
33 linesep = os.linesep.encode('ascii')
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
34
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
35 class Mailbox:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
36 """A group of messages in a particular place."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
37
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
38 def __init__(self, path, factory=None, create=True):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
39 """Initialize a Mailbox instance."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
40 self._path = os.path.abspath(os.path.expanduser(path))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
41 self._factory = factory
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
42
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
43 def add(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
44 """Add message and return assigned key."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
45 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
46
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
47 def remove(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
48 """Remove the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
49 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
50
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
51 def __delitem__(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
52 self.remove(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
53
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
54 def discard(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
55 """If the keyed message exists, remove it."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
56 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
57 self.remove(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
58 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
59 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
60
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
61 def __setitem__(self, key, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
62 """Replace the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
63 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
64
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
65 def get(self, key, default=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
66 """Return the keyed message, or default if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
67 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
68 return self.__getitem__(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
69 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
70 return default
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
71
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
72 def __getitem__(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
73 """Return the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
74 if not self._factory:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
75 return self.get_message(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
76 else:
70843
fea1920ae75f #11767: use context manager to close file in __getitem__ to prevent FD leak
R David Murray <rdmurray@bitdance.com>
parents: 69883
diff changeset
77 with contextlib.closing(self.get_file(key)) as file:
fea1920ae75f #11767: use context manager to close file in __getitem__ to prevent FD leak
R David Murray <rdmurray@bitdance.com>
parents: 69883
diff changeset
78 return self._factory(file)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
79
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
80 def get_message(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
81 """Return a Message representation or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
82 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
83
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
84 def get_string(self, key):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
85 """Return a string representation or raise a KeyError.
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
86
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
87 Uses email.message.Message to create a 7bit clean string
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
88 representation of the message."""
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
89 return email.message_from_bytes(self.get_bytes(key)).as_string()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
90
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
91 def get_bytes(self, key):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
92 """Return a byte string representation or raise a KeyError."""
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
93 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
94
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
95 def get_file(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
96 """Return a file-like representation or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
97 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
98
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
99 def iterkeys(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
100 """Return an iterator over keys."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
101 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
102
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
103 def keys(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
104 """Return a list of keys."""
40821
429a4bc31c8e Fix another unit test.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
105 return list(self.iterkeys())
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
106
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
107 def itervalues(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
108 """Return an iterator over all messages."""
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
109 for key in self.keys():
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
110 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
111 value = self[key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
112 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
113 continue
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
114 yield value
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
115
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
116 def __iter__(self):
42309
bd6acfdb47d2 Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
117 return self.itervalues()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
118
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
119 def values(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
120 """Return a list of messages. Memory intensive."""
40821
429a4bc31c8e Fix another unit test.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
121 return list(self.itervalues())
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
122
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
123 def iteritems(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
124 """Return an iterator over (key, message) tuples."""
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
125 for key in self.keys():
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
126 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
127 value = self[key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
128 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
129 continue
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
130 yield (key, value)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
131
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
132 def items(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
133 """Return a list of (key, message) tuples. Memory intensive."""
40821
429a4bc31c8e Fix another unit test.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
134 return list(self.iteritems())
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
135
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
136 def __contains__(self, key):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
137 """Return True if the keyed message exists, False otherwise."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
138 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
139
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
140 def __len__(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
141 """Return a count of messages in the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
142 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
143
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
144 def clear(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
145 """Delete all messages."""
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
146 for key in self.keys():
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
147 self.discard(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
148
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
149 def pop(self, key, default=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
150 """Delete the keyed message and return it, or default."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
151 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
152 result = self[key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
153 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
154 return default
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
155 self.discard(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
156 return result
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
157
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
158 def popitem(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
159 """Delete an arbitrary (key, message) pair and return it."""
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
160 for key in self.keys():
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
161 return (key, self.pop(key)) # This is only run once.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
162 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
163 raise KeyError('No messages in mailbox')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
164
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
165 def update(self, arg=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
166 """Change the messages that correspond to certain keys."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
167 if hasattr(arg, 'iteritems'):
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
168 source = arg.items()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
169 elif hasattr(arg, 'items'):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
170 source = arg.items()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
171 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
172 source = arg
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
173 bad_key = False
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
174 for key, message in source:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
175 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
176 self[key] = message
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
177 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
178 bad_key = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
179 if bad_key:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
180 raise KeyError('No message with key(s)')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
181
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
182 def flush(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
183 """Write any pending changes to the disk."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
184 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
185
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
186 def lock(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
187 """Lock the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
188 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
189
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
190 def unlock(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
191 """Unlock the mailbox if it is locked."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
192 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
193
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
194 def close(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
195 """Flush and close the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
196 raise NotImplementedError('Method must be implemented by subclass')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
197
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
198 def _string_to_bytes(self, message):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
199 # If a message is not 7bit clean, we refuse to handle it since it
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
200 # likely came from reading invalid messages in text mode, and that way
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
201 # lies mojibake.
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
202 try:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
203 return message.encode('ascii')
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
204 except UnicodeError:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
205 raise ValueError("String input must be ASCII-only; "
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
206 "use bytes or a Message instead")
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
207
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
208 # Whether each message must end in a newline
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
209 _append_newline = False
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
210
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
211 def _dump_message(self, message, target, mangle_from_=False):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
212 # This assumes the target file is open in binary mode.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
213 """Dump message contents to target file."""
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
214 if isinstance(message, email.message.Message):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
215 buffer = io.BytesIO()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
216 gen = email.generator.BytesGenerator(buffer, mangle_from_, 0)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
217 gen.flatten(message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
218 buffer.seek(0)
43122
3424803337a3 Force test_mailbox and test_old_mailbox into submission.
Guido van Rossum <guido@python.org>
parents: 42501
diff changeset
219 data = buffer.read()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
220 data = data.replace(b'\n', linesep)
43122
3424803337a3 Force test_mailbox and test_old_mailbox into submission.
Guido van Rossum <guido@python.org>
parents: 42501
diff changeset
221 target.write(data)
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
222 if self._append_newline and not data.endswith(linesep):
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
223 # Make sure the message ends with a newline
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
224 target.write(linesep)
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
225 elif isinstance(message, (str, bytes, io.StringIO)):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
226 if isinstance(message, io.StringIO):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
227 warnings.warn("Use of StringIO input is deprecated, "
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
228 "use BytesIO instead", DeprecationWarning, 3)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
229 message = message.getvalue()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
230 if isinstance(message, str):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
231 message = self._string_to_bytes(message)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
232 if mangle_from_:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
233 message = message.replace(b'\nFrom ', b'\n>From ')
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
234 message = message.replace(b'\n', linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
235 target.write(message)
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
236 if self._append_newline and not message.endswith(linesep):
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
237 # Make sure the message ends with a newline
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
238 target.write(linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
239 elif hasattr(message, 'read'):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
240 if hasattr(message, 'buffer'):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
241 warnings.warn("Use of text mode files is deprecated, "
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
242 "use a binary mode file instead", DeprecationWarning, 3)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
243 message = message.buffer
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
244 lastline = None
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
245 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
246 line = message.readline()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
247 # Universal newline support.
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
248 if line.endswith(b'\r\n'):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
249 line = line[:-2] + b'\n'
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
250 elif line.endswith(b'\r'):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
251 line = line[:-1] + b'\n'
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
252 if not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
253 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
254 if mangle_from_ and line.startswith(b'From '):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
255 line = b'>From ' + line[5:]
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
256 line = line.replace(b'\n', linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
257 target.write(line)
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
258 lastline = line
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
259 if self._append_newline and lastline and not lastline.endswith(linesep):
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
260 # Make sure the message ends with a newline
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
261 target.write(linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
262 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
263 raise TypeError('Invalid message type: %s' % type(message))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
264
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
265
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
266 class Maildir(Mailbox):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
267 """A qmail-style Maildir mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
268
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
269 colon = ':'
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
270
46907
6c15db6ce5da #756982: Remove rfc822 reference.
Georg Brandl <georg@python.org>
parents: 46893
diff changeset
271 def __init__(self, dirname, factory=None, create=True):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
272 """Initialize a Maildir instance."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
273 Mailbox.__init__(self, dirname, factory, create)
69882
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
274 self._paths = {
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
275 'tmp': os.path.join(self._path, 'tmp'),
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
276 'new': os.path.join(self._path, 'new'),
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
277 'cur': os.path.join(self._path, 'cur'),
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
278 }
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
279 if not os.path.exists(self._path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
280 if create:
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41990
diff changeset
281 os.mkdir(self._path, 0o700)
69882
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
282 for path in self._paths.values():
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
283 os.mkdir(path, 0o700)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
284 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
285 raise NoSuchMailboxError(self._path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
286 self._toc = {}
73369
5f27a9f67a34 Fix Maildir initialization so that maildir contents are read correctly.
Petri Lehtinen <petri@digip.org>
parents: 70857
diff changeset
287 self._toc_mtimes = {'cur': 0, 'new': 0}
5f27a9f67a34 Fix Maildir initialization so that maildir contents are read correctly.
Petri Lehtinen <petri@digip.org>
parents: 70857
diff changeset
288 self._last_read = 0 # Records last time we read cur/new
5f27a9f67a34 Fix Maildir initialization so that maildir contents are read correctly.
Petri Lehtinen <petri@digip.org>
parents: 70857
diff changeset
289 self._skewfactor = 0.1 # Adjust if os/fs clocks are skewing
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
290
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
291 def add(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
292 """Add message and return assigned key."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
293 tmp_file = self._create_tmp()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
294 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
295 self._dump_message(message, tmp_file)
67927
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
296 except BaseException:
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
297 tmp_file.close()
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
298 os.remove(tmp_file.name)
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
299 raise
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
300 _sync_close(tmp_file)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
301 if isinstance(message, MaildirMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
302 subdir = message.get_subdir()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
303 suffix = self.colon + message.get_info()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
304 if suffix == self.colon:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
305 suffix = ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
306 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
307 subdir = 'new'
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
308 suffix = ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
309 uniq = os.path.basename(tmp_file.name).split(self.colon)[0]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
310 dest = os.path.join(self._path, subdir, uniq + suffix)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
311 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
312 if hasattr(os, 'link'):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
313 os.link(tmp_file.name, dest)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
314 os.remove(tmp_file.name)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
315 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
316 os.rename(tmp_file.name, dest)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
317 except OSError as e:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
318 os.remove(tmp_file.name)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
319 if e.errno == errno.EEXIST:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
320 raise ExternalClashError('Name clash with existing message: %s'
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
321 % dest)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
322 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
323 raise
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
324 if isinstance(message, MaildirMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
325 os.utime(dest, (os.path.getatime(dest), message.get_date()))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
326 return uniq
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
327
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
328 def remove(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
329 """Remove the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
330 os.remove(os.path.join(self._path, self._lookup(key)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
331
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
332 def discard(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
333 """If the keyed message exists, remove it."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
334 # This overrides an inapplicable implementation in the superclass.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
335 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
336 self.remove(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
337 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
338 pass
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
339 except OSError as e:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
340 if e.errno != errno.ENOENT:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
341 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
342
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
343 def __setitem__(self, key, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
344 """Replace the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
345 old_subpath = self._lookup(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
346 temp_key = self.add(message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
347 temp_subpath = self._lookup(temp_key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
348 if isinstance(message, MaildirMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
349 # temp's subdir and suffix were specified by message.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
350 dominant_subpath = temp_subpath
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
351 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
352 # temp's subdir and suffix were defaults from add().
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
353 dominant_subpath = old_subpath
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
354 subdir = os.path.dirname(dominant_subpath)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
355 if self.colon in dominant_subpath:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
356 suffix = self.colon + dominant_subpath.split(self.colon)[-1]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
357 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
358 suffix = ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
359 self.discard(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
360 new_path = os.path.join(self._path, subdir, key + suffix)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
361 os.rename(os.path.join(self._path, temp_subpath), new_path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
362 if isinstance(message, MaildirMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
363 os.utime(new_path, (os.path.getatime(new_path),
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
364 message.get_date()))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
365
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
366 def get_message(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
367 """Return a Message representation or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
368 subpath = self._lookup(key)
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
369 f = open(os.path.join(self._path, subpath), 'rb')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
370 try:
44810
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
371 if self._factory:
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
372 msg = self._factory(f)
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
373 else:
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
374 msg = MaildirMessage(f)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
375 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
376 f.close()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
377 subdir, name = os.path.split(subpath)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
378 msg.set_subdir(subdir)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
379 if self.colon in name:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
380 msg.set_info(name.split(self.colon)[-1])
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
381 msg.set_date(os.path.getmtime(os.path.join(self._path, subpath)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
382 return msg
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
383
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
384 def get_bytes(self, key):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
385 """Return a bytes representation or raise a KeyError."""
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
386 f = open(os.path.join(self._path, self._lookup(key)), 'rb')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
387 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
388 return f.read().replace(linesep, b'\n')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
389 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
390 f.close()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
391
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
392 def get_file(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
393 """Return a file-like representation or raise a KeyError."""
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
394 f = open(os.path.join(self._path, self._lookup(key)), 'rb')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
395 return _ProxyFile(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
396
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
397 def iterkeys(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
398 """Return an iterator over keys."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
399 self._refresh()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
400 for key in self._toc:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
401 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
402 self._lookup(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
403 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
404 continue
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
405 yield key
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
406
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
407 def __contains__(self, key):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
408 """Return True if the keyed message exists, False otherwise."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
409 self._refresh()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
410 return key in self._toc
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
411
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
412 def __len__(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
413 """Return a count of messages in the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
414 self._refresh()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
415 return len(self._toc)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
416
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
417 def flush(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
418 """Write any pending changes to disk."""
57071
8a0d3c69ae21 Merged revisions 76055 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53698
diff changeset
419 # Maildir changes are always written immediately, so there's nothing
69882
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
420 # to do.
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
421 pass
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
422
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
423 def lock(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
424 """Lock the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
425 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
426
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
427 def unlock(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
428 """Unlock the mailbox if it is locked."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
429 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
430
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
431 def close(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
432 """Flush and close the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
433 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
434
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
435 def list_folders(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
436 """Return a list of folder names."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
437 result = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
438 for entry in os.listdir(self._path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
439 if len(entry) > 1 and entry[0] == '.' and \
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
440 os.path.isdir(os.path.join(self._path, entry)):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
441 result.append(entry[1:])
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
442 return result
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
443
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
444 def get_folder(self, folder):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
445 """Return a Maildir instance for the named folder."""
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
446 return Maildir(os.path.join(self._path, '.' + folder),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
447 factory=self._factory,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
448 create=False)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
449
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
450 def add_folder(self, folder):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
451 """Create a folder and return a Maildir instance representing it."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
452 path = os.path.join(self._path, '.' + folder)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
453 result = Maildir(path, factory=self._factory)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
454 maildirfolder_path = os.path.join(path, 'maildirfolder')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
455 if not os.path.exists(maildirfolder_path):
48680
7779af388aec Merged revisions 65459,65472,65481,65518,65536,65581,65609,65637,65641,65644-65645 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46907
diff changeset
456 os.close(os.open(maildirfolder_path, os.O_CREAT | os.O_WRONLY,
7779af388aec Merged revisions 65459,65472,65481,65518,65536,65581,65609,65637,65641,65644-65645 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46907
diff changeset
457 0o666))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
458 return result
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
459
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
460 def remove_folder(self, folder):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
461 """Delete the named folder, which must be empty."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
462 path = os.path.join(self._path, '.' + folder)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
463 for entry in os.listdir(os.path.join(path, 'new')) + \
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
464 os.listdir(os.path.join(path, 'cur')):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
465 if len(entry) < 1 or entry[0] != '.':
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
466 raise NotEmptyError('Folder contains message(s): %s' % folder)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
467 for entry in os.listdir(path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
468 if entry != 'new' and entry != 'cur' and entry != 'tmp' and \
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
469 os.path.isdir(os.path.join(path, entry)):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
470 raise NotEmptyError("Folder contains subdirectory '%s': %s" %
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
471 (folder, entry))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
472 for root, dirs, files in os.walk(path, topdown=False):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
473 for entry in files:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
474 os.remove(os.path.join(root, entry))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
475 for entry in dirs:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
476 os.rmdir(os.path.join(root, entry))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
477 os.rmdir(path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
478
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
479 def clean(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
480 """Delete old files in "tmp"."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
481 now = time.time()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
482 for entry in os.listdir(os.path.join(self._path, 'tmp')):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
483 path = os.path.join(self._path, 'tmp', entry)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
484 if now - os.path.getatime(path) > 129600: # 60 * 60 * 36
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
485 os.remove(path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
486
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
487 _count = 1 # This is used to generate unique file names.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
488
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
489 def _create_tmp(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
490 """Create a file in the tmp subdirectory and open and return it."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
491 now = time.time()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
492 hostname = socket.gethostname()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
493 if '/' in hostname:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
494 hostname = hostname.replace('/', r'\057')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
495 if ':' in hostname:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
496 hostname = hostname.replace(':', r'\072')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
497 uniq = "%s.M%sP%sQ%s.%s" % (int(now), int(now % 1 * 1e6), os.getpid(),
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
498 Maildir._count, hostname)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
499 path = os.path.join(self._path, 'tmp', uniq)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
500 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
501 os.stat(path)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
502 except OSError as e:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
503 if e.errno == errno.ENOENT:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
504 Maildir._count += 1
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
505 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
506 return _create_carefully(path)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
507 except OSError as e:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
508 if e.errno != errno.EEXIST:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
509 raise
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
510 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
511 raise
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
512
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
513 # Fall through to here if stat succeeded or open raised EEXIST.
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
514 raise ExternalClashError('Name clash prevented file creation: %s' %
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
515 path)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
516
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
517 def _refresh(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
518 """Update table of contents mapping."""
69882
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
519 # If it has been less than two seconds since the last _refresh() call,
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
520 # we have to unconditionally re-read the mailbox just in case it has
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
521 # been modified, because os.path.mtime() has a 2 sec resolution in the
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
522 # most common worst case (FAT) and a 1 sec resolution typically. This
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
523 # results in a few unnecessary re-reads when _refresh() is called
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
524 # multiple times in that interval, but once the clock ticks over, we
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
525 # will only re-read as needed. Because the filesystem might be being
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
526 # served by an independent system with its own clock, we record and
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
527 # compare with the mtimes from the filesystem. Because the other
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
528 # system's clock might be skewing relative to our clock, we add an
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
529 # extra delta to our wait. The default is one tenth second, but is an
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
530 # instance variable and so can be adjusted if dealing with a
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
531 # particularly skewed or irregular system.
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
532 if time.time() - self._last_read > 2 + self._skewfactor:
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
533 refresh = False
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
534 for subdir in self._toc_mtimes:
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
535 mtime = os.path.getmtime(self._paths[subdir])
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
536 if mtime > self._toc_mtimes[subdir]:
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
537 refresh = True
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
538 self._toc_mtimes[subdir] = mtime
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
539 if not refresh:
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
540 return
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
541 # Refresh toc
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
542 self._toc = {}
69882
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
543 for subdir in self._toc_mtimes:
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
544 path = self._paths[subdir]
53698
358ab1ffaa80 Merged revisions 72213 via svnmerge from
Andrew M. Kuchling <amk@amk.ca>
parents: 52747
diff changeset
545 for entry in os.listdir(path):
358ab1ffaa80 Merged revisions 72213 via svnmerge from
Andrew M. Kuchling <amk@amk.ca>
parents: 52747
diff changeset
546 p = os.path.join(path, entry)
42309
bd6acfdb47d2 Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
547 if os.path.isdir(p):
bd6acfdb47d2 Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
548 continue
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
549 uniq = entry.split(self.colon)[0]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
550 self._toc[uniq] = os.path.join(subdir, entry)
69882
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
551 self._last_read = time.time()
53698
358ab1ffaa80 Merged revisions 72213 via svnmerge from
Andrew M. Kuchling <amk@amk.ca>
parents: 52747
diff changeset
552
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
553 def _lookup(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
554 """Use TOC to return subpath for given key, or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
555 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
556 if os.path.exists(os.path.join(self._path, self._toc[key])):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
557 return self._toc[key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
558 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
559 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
560 self._refresh()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
561 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
562 return self._toc[key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
563 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
564 raise KeyError('No message with key: %s' % key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
565
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
566 # This method is for backward compatibility only.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
567 def next(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
568 """Return the next message in a one-time iteration."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
569 if not hasattr(self, '_onetime_keys'):
40821
429a4bc31c8e Fix another unit test.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
570 self._onetime_keys = iter(self.keys())
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
571 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
572 try:
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40821
diff changeset
573 return self[next(self._onetime_keys)]
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
574 except StopIteration:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
575 return None
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
576 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
577 continue
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
578
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
579
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
580 class _singlefileMailbox(Mailbox):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
581 """A single-file mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
582
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
583 def __init__(self, path, factory=None, create=True):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
584 """Initialize a single-file mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
585 Mailbox.__init__(self, path, factory, create)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
586 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
587 f = open(self._path, 'rb+')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
588 except IOError as e:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
589 if e.errno == errno.ENOENT:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
590 if create:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
591 f = open(self._path, 'wb+')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
592 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
593 raise NoSuchMailboxError(self._path)
68220
b5281f5fade4 #11306: Treat EROFS like EACCES when making a 'file is read-only' decision
R. David Murray <rdmurray@bitdance.com>
parents: 67931
diff changeset
594 elif e.errno in (errno.EACCES, errno.EROFS):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
595 f = open(self._path, 'rb')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
596 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
597 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
598 self._file = f
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
599 self._toc = None
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
600 self._next_key = 0
77833
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
601 self._pending = False # No changes require rewriting the file.
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
602 self._pending_sync = False # No need to sync the file
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
603 self._locked = False
77833
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
604 self._file_length = None # Used to record mailbox size
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
605
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
606 def add(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
607 """Add message and return assigned key."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
608 self._lookup()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
609 self._toc[self._next_key] = self._append_message(message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
610 self._next_key += 1
77833
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
611 # _append_message appends the message to the mailbox file. We
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
612 # don't need a full rewrite + rename, sync is enough.
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
613 self._pending_sync = True
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
614 return self._next_key - 1
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
615
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
616 def remove(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
617 """Remove the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
618 self._lookup(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
619 del self._toc[key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
620 self._pending = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
621
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
622 def __setitem__(self, key, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
623 """Replace the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
624 self._lookup(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
625 self._toc[key] = self._append_message(message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
626 self._pending = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
627
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
628 def iterkeys(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
629 """Return an iterator over keys."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
630 self._lookup()
79378
fb90e2ff95b7 utilize yield from
Philip Jenvey <pjenvey@underboss.org>
parents: 79185
diff changeset
631 yield from self._toc.keys()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
632
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
633 def __contains__(self, key):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
634 """Return True if the keyed message exists, False otherwise."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
635 self._lookup()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
636 return key in self._toc
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
637
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
638 def __len__(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
639 """Return a count of messages in the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
640 self._lookup()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
641 return len(self._toc)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
642
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
643 def lock(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
644 """Lock the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
645 if not self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
646 _lock_file(self._file)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
647 self._locked = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
648
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
649 def unlock(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
650 """Unlock the mailbox if it is locked."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
651 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
652 _unlock_file(self._file)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
653 self._locked = False
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
654
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
655 def flush(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
656 """Write any pending changes to disk."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
657 if not self._pending:
77833
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
658 if self._pending_sync:
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
659 # Messages have only been added, so syncing the file
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
660 # is enough.
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
661 _sync_flush(self._file)
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
662 self._pending_sync = False
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
663 return
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
664
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
665 # In order to be writing anything out at all, self._toc must
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
666 # already have been generated (and presumably has been modified
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
667 # by adding or deleting an item).
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
668 assert self._toc is not None
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40737
diff changeset
669
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
670 # Check length of self._file; if it's changed, some other process
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
671 # has modified the mailbox since we scanned it.
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
672 self._file.seek(0, 2)
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
673 cur_len = self._file.tell()
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
674 if cur_len != self._file_length:
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
675 raise ExternalClashError('Size of mailbox file changed '
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
676 '(expected %i, found %i)' %
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
677 (self._file_length, cur_len))
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40737
diff changeset
678
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
679 new_file = _create_temporary(self._path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
680 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
681 new_toc = {}
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
682 self._pre_mailbox_hook(new_file)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
683 for key in sorted(self._toc.keys()):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
684 start, stop = self._toc[key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
685 self._file.seek(start)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
686 self._pre_message_hook(new_file)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
687 new_start = new_file.tell()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
688 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
689 buffer = self._file.read(min(4096,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
690 stop - self._file.tell()))
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
691 if not buffer:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
692 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
693 new_file.write(buffer)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
694 new_toc[key] = (new_start, new_file.tell())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
695 self._post_message_hook(new_file)
77444
714b8f91f3d4 #15036: Make a repeated changes and flushes work with single-file mailboxes
Petri Lehtinen <petri@digip.org>
parents: 73369
diff changeset
696 self._file_length = new_file.tell()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
697 except:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
698 new_file.close()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
699 os.remove(new_file.name)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
700 raise
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
701 _sync_close(new_file)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
702 # self._file is about to get replaced, so no need to sync.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
703 self._file.close()
77851
13fb85ef0eea #5346: Preserve permissions of mbox, MMDF and Babyl mailbox files on flush()
Petri Lehtinen <petri@digip.org>
parents: 77848
diff changeset
704 # Make sure the new file's mode is the same as the old file's
13fb85ef0eea #5346: Preserve permissions of mbox, MMDF and Babyl mailbox files on flush()
Petri Lehtinen <petri@digip.org>
parents: 77848
diff changeset
705 mode = os.stat(self._path).st_mode
13fb85ef0eea #5346: Preserve permissions of mbox, MMDF and Babyl mailbox files on flush()
Petri Lehtinen <petri@digip.org>
parents: 77848
diff changeset
706 os.chmod(new_file.name, mode)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
707 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
708 os.rename(new_file.name, self._path)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
709 except OSError as e:
79480
15949f34e08f #16135: Removal of OS/2 support (Python code partial cleanup)
Jesus Cea <jcea@jcea.es>
parents: 79479
diff changeset
710 if e.errno == errno.EEXIST:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
711 os.remove(self._path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
712 os.rename(new_file.name, self._path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
713 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
714 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
715 self._file = open(self._path, 'rb+')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
716 self._toc = new_toc
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
717 self._pending = False
77833
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
718 self._pending_sync = False
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
719 if self._locked:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
720 _lock_file(self._file, dotlock=False)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
721
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
722 def _pre_mailbox_hook(self, f):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
723 """Called before writing the mailbox to file f."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
724 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
725
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
726 def _pre_message_hook(self, f):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
727 """Called before writing each message to file f."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
728 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
729
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
730 def _post_message_hook(self, f):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
731 """Called after writing each message to file f."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
732 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
733
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
734 def close(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
735 """Flush and close the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
736 self.flush()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
737 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
738 self.unlock()
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
739 self._file.close() # Sync has been done by self.flush() above.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
740
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
741 def _lookup(self, key=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
742 """Return (start, stop) or raise KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
743 if self._toc is None:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
744 self._generate_toc()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
745 if key is not None:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
746 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
747 return self._toc[key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
748 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
749 raise KeyError('No message with key: %s' % key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
750
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
751 def _append_message(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
752 """Append message to mailbox and return (start, stop) offsets."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
753 self._file.seek(0, 2)
67927
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
754 before = self._file.tell()
77848
7cf5a629fde2 #9559: Don't call _pre_mailbox_hook more than once
Petri Lehtinen <petri@digip.org>
parents: 77833
diff changeset
755 if len(self._toc) == 0 and not self._pending:
7cf5a629fde2 #9559: Don't call _pre_mailbox_hook more than once
Petri Lehtinen <petri@digip.org>
parents: 77833
diff changeset
756 # This is the first message, and the _pre_mailbox_hook
7cf5a629fde2 #9559: Don't call _pre_mailbox_hook more than once
Petri Lehtinen <petri@digip.org>
parents: 77833
diff changeset
757 # hasn't yet been called. If self._pending is True,
7cf5a629fde2 #9559: Don't call _pre_mailbox_hook more than once
Petri Lehtinen <petri@digip.org>
parents: 77833
diff changeset
758 # messages have been removed, so _pre_mailbox_hook must
7cf5a629fde2 #9559: Don't call _pre_mailbox_hook more than once
Petri Lehtinen <petri@digip.org>
parents: 77833
diff changeset
759 # have been called already.
77833
5f447a005d67 #9559: Append data to single-file mailbox files if messages are only added
Petri Lehtinen <petri@digip.org>
parents: 77444
diff changeset
760 self._pre_mailbox_hook(self._file)
67927
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
761 try:
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
762 self._pre_message_hook(self._file)
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
763 offsets = self._install_message(message)
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
764 self._post_message_hook(self._file)
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
765 except BaseException:
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
766 self._file.truncate(before)
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
767 raise
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
768 self._file.flush()
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
769 self._file_length = self._file.tell() # Record current length of mailbox
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
770 return offsets
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
771
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
772
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
773
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
774 class _mboxMMDF(_singlefileMailbox):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
775 """An mbox or MMDF mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
776
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
777 _mangle_from_ = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
778
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
779 def get_message(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
780 """Return a Message representation or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
781 start, stop = self._lookup(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
782 self._file.seek(start)
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
783 from_line = self._file.readline().replace(linesep, b'')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
784 string = self._file.read(stop - self._file.tell())
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
785 msg = self._message_factory(string.replace(linesep, b'\n'))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
786 msg.set_from(from_line[5:].decode('ascii'))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
787 return msg
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
788
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
789 def get_string(self, key, from_=False):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
790 """Return a string representation or raise a KeyError."""
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
791 return email.message_from_bytes(
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
792 self.get_bytes(key)).as_string(unixfrom=from_)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
793
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
794 def get_bytes(self, key, from_=False):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
795 """Return a string representation or raise a KeyError."""
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
796 start, stop = self._lookup(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
797 self._file.seek(start)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
798 if not from_:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
799 self._file.readline()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
800 string = self._file.read(stop - self._file.tell())
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
801 return string.replace(linesep, b'\n')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
802
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
803 def get_file(self, key, from_=False):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
804 """Return a file-like representation or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
805 start, stop = self._lookup(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
806 self._file.seek(start)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
807 if not from_:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
808 self._file.readline()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
809 return _PartialFile(self._file, self._file.tell(), stop)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
810
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
811 def _install_message(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
812 """Format a message and blindly write to self._file."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
813 from_line = None
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
814 if isinstance(message, str):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
815 message = self._string_to_bytes(message)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
816 if isinstance(message, bytes) and message.startswith(b'From '):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
817 newline = message.find(b'\n')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
818 if newline != -1:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
819 from_line = message[:newline]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
820 message = message[newline + 1:]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
821 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
822 from_line = message
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
823 message = b''
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
824 elif isinstance(message, _mboxMMDFMessage):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
825 author = message.get_from().encode('ascii')
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
826 from_line = b'From ' + author
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
827 elif isinstance(message, email.message.Message):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
828 from_line = message.get_unixfrom() # May be None.
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
829 if from_line is not None:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
830 from_line = from_line.encode('ascii')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
831 if from_line is None:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
832 from_line = b'From MAILER-DAEMON ' + time.asctime(time.gmtime()).encode()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
833 start = self._file.tell()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
834 self._file.write(from_line + linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
835 self._dump_message(message, self._file, self._mangle_from_)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
836 stop = self._file.tell()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
837 return (start, stop)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
838
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
839
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
840 class mbox(_mboxMMDF):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
841 """A classic mbox mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
842
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
843 _mangle_from_ = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
844
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
845 # All messages must end in a newline character, and
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
846 # _post_message_hooks outputs an empty line between messages.
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
847 _append_newline = True
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
848
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
849 def __init__(self, path, factory=None, create=True):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
850 """Initialize an mbox mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
851 self._message_factory = mboxMessage
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
852 _mboxMMDF.__init__(self, path, factory, create)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
853
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
854 def _post_message_hook(self, f):
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
855 """Called after writing each message to file f."""
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
856 f.write(linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
857
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
858 def _generate_toc(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
859 """Generate key-to-(start, stop) table of contents."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
860 starts, stops = [], []
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
861 last_was_empty = False
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
862 self._file.seek(0)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
863 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
864 line_pos = self._file.tell()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
865 line = self._file.readline()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
866 if line.startswith(b'From '):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
867 if len(stops) < len(starts):
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
868 if last_was_empty:
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
869 stops.append(line_pos - len(linesep))
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
870 else:
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
871 # The last line before the "From " line wasn't
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
872 # blank, but we consider it a start of a
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
873 # message anyway.
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
874 stops.append(line_pos)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
875 starts.append(line_pos)
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
876 last_was_empty = False
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
877 elif not line:
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
878 if last_was_empty:
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
879 stops.append(line_pos - len(linesep))
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
880 else:
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
881 stops.append(line_pos)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
882 break
79184
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
883 elif line == linesep:
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
884 last_was_empty = True
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
885 else:
f7615ee43318 #15222: Insert blank line after each message in mbox mailboxes
Petri Lehtinen <petri@digip.org>
parents: 78608
diff changeset
886 last_was_empty = False
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
887 self._toc = dict(enumerate(zip(starts, stops)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
888 self._next_key = len(self._toc)
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
889 self._file_length = self._file.tell()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
890
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
891
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
892 class MMDF(_mboxMMDF):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
893 """An MMDF mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
894
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
895 def __init__(self, path, factory=None, create=True):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
896 """Initialize an MMDF mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
897 self._message_factory = MMDFMessage
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
898 _mboxMMDF.__init__(self, path, factory, create)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
899
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
900 def _pre_message_hook(self, f):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
901 """Called before writing each message to file f."""
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
902 f.write(b'\001\001\001\001' + linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
903
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
904 def _post_message_hook(self, f):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
905 """Called after writing each message to file f."""
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
906 f.write(linesep + b'\001\001\001\001' + linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
907
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
908 def _generate_toc(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
909 """Generate key-to-(start, stop) table of contents."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
910 starts, stops = [], []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
911 self._file.seek(0)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
912 next_pos = 0
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
913 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
914 line_pos = next_pos
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
915 line = self._file.readline()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
916 next_pos = self._file.tell()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
917 if line.startswith(b'\001\001\001\001' + linesep):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
918 starts.append(next_pos)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
919 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
920 line_pos = next_pos
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
921 line = self._file.readline()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
922 next_pos = self._file.tell()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
923 if line == b'\001\001\001\001' + linesep:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
924 stops.append(line_pos - len(linesep))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
925 break
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
926 elif not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
927 stops.append(line_pos)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
928 break
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
929 elif not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
930 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
931 self._toc = dict(enumerate(zip(starts, stops)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
932 self._next_key = len(self._toc)
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
933 self._file.seek(0, 2)
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
934 self._file_length = self._file.tell()
4316
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
935
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
936
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
937 class MH(Mailbox):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
938 """An MH mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
939
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
940 def __init__(self, path, factory=None, create=True):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
941 """Initialize an MH instance."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
942 Mailbox.__init__(self, path, factory, create)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
943 if not os.path.exists(self._path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
944 if create:
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41990
diff changeset
945 os.mkdir(self._path, 0o700)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
946 os.close(os.open(os.path.join(self._path, '.mh_sequences'),
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41990
diff changeset
947 os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
948 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
949 raise NoSuchMailboxError(self._path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
950 self._locked = False
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
951
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
952 def add(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
953 """Add message and return assigned key."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
954 keys = self.keys()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
955 if len(keys) == 0:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
956 new_key = 1
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
957 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
958 new_key = max(keys) + 1
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
959 new_path = os.path.join(self._path, str(new_key))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
960 f = _create_carefully(new_path)
67931
f3ce4abbaaf8 Fix #11116 fix on Windows (close file before removing in MH code)
R. David Murray <rdmurray@bitdance.com>
parents: 67927
diff changeset
961 closed = False
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
962 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
963 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
964 _lock_file(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
965 try:
67927
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
966 try:
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
967 self._dump_message(message, f)
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
968 except BaseException:
67931
f3ce4abbaaf8 Fix #11116 fix on Windows (close file before removing in MH code)
R. David Murray <rdmurray@bitdance.com>
parents: 67927
diff changeset
969 # Unlock and close so it can be deleted on Windows
f3ce4abbaaf8 Fix #11116 fix on Windows (close file before removing in MH code)
R. David Murray <rdmurray@bitdance.com>
parents: 67927
diff changeset
970 if self._locked:
f3ce4abbaaf8 Fix #11116 fix on Windows (close file before removing in MH code)
R. David Murray <rdmurray@bitdance.com>
parents: 67927
diff changeset
971 _unlock_file(f)
f3ce4abbaaf8 Fix #11116 fix on Windows (close file before removing in MH code)
R. David Murray <rdmurray@bitdance.com>
parents: 67927
diff changeset
972 _sync_close(f)
f3ce4abbaaf8 Fix #11116 fix on Windows (close file before removing in MH code)
R. David Murray <rdmurray@bitdance.com>
parents: 67927
diff changeset
973 closed = True
67927
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
974 os.remove(new_path)
2cbe9d058f7e #11116: roll back on error during add so mailbox isn't left corrupted.
R. David Murray <rdmurray@bitdance.com>
parents: 67814
diff changeset
975 raise
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
976 if isinstance(message, MHMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
977 self._dump_sequences(message, new_key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
978 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
979 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
980 _unlock_file(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
981 finally:
67931
f3ce4abbaaf8 Fix #11116 fix on Windows (close file before removing in MH code)
R. David Murray <rdmurray@bitdance.com>
parents: 67927
diff changeset
982 if not closed:
f3ce4abbaaf8 Fix #11116 fix on Windows (close file before removing in MH code)
R. David Murray <rdmurray@bitdance.com>
parents: 67927
diff changeset
983 _sync_close(f)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
984 return new_key
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
985
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
986 def remove(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
987 """Remove the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
988 path = os.path.join(self._path, str(key))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
989 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
990 f = open(path, 'rb+')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
991 except IOError as e:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
992 if e.errno == errno.ENOENT:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
993 raise KeyError('No message with key: %s' % key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
994 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
995 raise
59869
fbacf972fa44 Merged revisions 77952,78030,78102,78104,78107,78206,78216,78296-78297,78328,78331-78332,78336,78339,78343,78378-78379,78415,78559,78717,78791 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59522
diff changeset
996 else:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
997 f.close()
59869
fbacf972fa44 Merged revisions 77952,78030,78102,78104,78107,78206,78216,78296-78297,78328,78331-78332,78336,78339,78343,78378-78379,78415,78559,78717,78791 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59522
diff changeset
998 os.remove(path)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
999
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1000 def __setitem__(self, key, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1001 """Replace the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1002 path = os.path.join(self._path, str(key))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1003 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1004 f = open(path, 'rb+')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
1005 except IOError as e:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1006 if e.errno == errno.ENOENT:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1007 raise KeyError('No message with key: %s' % key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1008 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1009 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1010 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1011 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1012 _lock_file(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1013 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1014 os.close(os.open(path, os.O_WRONLY | os.O_TRUNC))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1015 self._dump_message(message, f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1016 if isinstance(message, MHMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1017 self._dump_sequences(message, key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1018 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1019 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1020 _unlock_file(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1021 finally:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1022 _sync_close(f)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1023
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1024 def get_message(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1025 """Return a Message representation or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1026 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1027 if self._locked:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1028 f = open(os.path.join(self._path, str(key)), 'rb+')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1029 else:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1030 f = open(os.path.join(self._path, str(key)), 'rb')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
1031 except IOError as e:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1032 if e.errno == errno.ENOENT:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1033 raise KeyError('No message with key: %s' % key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1034 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1035 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1036 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1037 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1038 _lock_file(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1039 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1040 msg = MHMessage(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1041 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1042 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1043 _unlock_file(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1044 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1045 f.close()
52747
d28ea2ee7070 Actually this was merged by hand because svnmerge messed up the properties
R. David Murray <rdmurray@bitdance.com>
parents: 48680
diff changeset
1046 for name, key_list in self.get_sequences().items():
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1047 if key in key_list:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1048 msg.add_sequence(name)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1049 return msg
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1050
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1051 def get_bytes(self, key):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1052 """Return a bytes representation or raise a KeyError."""
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1053 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1054 if self._locked:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1055 f = open(os.path.join(self._path, str(key)), 'rb+')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1056 else:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1057 f = open(os.path.join(self._path, str(key)), 'rb')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
1058 except IOError as e:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1059 if e.errno == errno.ENOENT:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1060 raise KeyError('No message with key: %s' % key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1061 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1062 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1063 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1064 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1065 _lock_file(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1066 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1067 return f.read().replace(linesep, b'\n')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1068 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1069 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1070 _unlock_file(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1071 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1072 f.close()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1073
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1074 def get_file(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1075 """Return a file-like representation or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1076 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1077 f = open(os.path.join(self._path, str(key)), 'rb')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
1078 except IOError as e:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1079 if e.errno == errno.ENOENT:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1080 raise KeyError('No message with key: %s' % key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1081 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1082 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1083 return _ProxyFile(f)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1084
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1085 def iterkeys(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1086 """Return an iterator over keys."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1087 return iter(sorted(int(entry) for entry in os.listdir(self._path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1088 if entry.isdigit()))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1089
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1090 def __contains__(self, key):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1091 """Return True if the keyed message exists, False otherwise."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1092 return os.path.exists(os.path.join(self._path, str(key)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1093
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1094 def __len__(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1095 """Return a count of messages in the mailbox."""
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
1096 return len(list(self.keys()))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1097
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1098 def lock(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1099 """Lock the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1100 if not self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1101 self._file = open(os.path.join(self._path, '.mh_sequences'), 'rb+')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1102 _lock_file(self._file)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1103 self._locked = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1104
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1105 def unlock(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1106 """Unlock the mailbox if it is locked."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1107 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1108 _unlock_file(self._file)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1109 _sync_close(self._file)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1110 del self._file
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1111 self._locked = False
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1112
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1113 def flush(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1114 """Write any pending changes to the disk."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1115 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1116
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1117 def close(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1118 """Flush and close the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1119 if self._locked:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1120 self.unlock()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1121
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1122 def list_folders(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1123 """Return a list of folder names."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1124 result = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1125 for entry in os.listdir(self._path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1126 if os.path.isdir(os.path.join(self._path, entry)):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1127 result.append(entry)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1128 return result
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1129
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1130 def get_folder(self, folder):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1131 """Return an MH instance for the named folder."""
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1132 return MH(os.path.join(self._path, folder),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1133 factory=self._factory, create=False)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1134
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1135 def add_folder(self, folder):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1136 """Create a folder and return an MH instance representing it."""
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1137 return MH(os.path.join(self._path, folder),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1138 factory=self._factory)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1139
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1140 def remove_folder(self, folder):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1141 """Delete the named folder, which must be empty."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1142 path = os.path.join(self._path, folder)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1143 entries = os.listdir(path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1144 if entries == ['.mh_sequences']:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1145 os.remove(os.path.join(path, '.mh_sequences'))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1146 elif entries == []:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1147 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1148 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1149 raise NotEmptyError('Folder not empty: %s' % self._path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1150 os.rmdir(path)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1151
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1152 def get_sequences(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1153 """Return a name-to-key-list dictionary to define each sequence."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1154 results = {}
72966
5ea81e4c58a7 Close #12454: The mailbox module is now using ASCII, instead of the locale
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70857
diff changeset
1155 with open(os.path.join(self._path, '.mh_sequences'), 'r', encoding='ASCII') as f:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1156 all_keys = set(self.keys())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1157 for line in f:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1158 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1159 name, contents = line.split(':')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1160 keys = set()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1161 for spec in contents.split():
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1162 if spec.isdigit():
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1163 keys.add(int(spec))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1164 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1165 start, stop = (int(x) for x in spec.split('-'))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1166 keys.update(range(start, stop + 1))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1167 results[name] = [key for key in sorted(keys) \
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1168 if key in all_keys]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1169 if len(results[name]) == 0:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1170 del results[name]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1171 except ValueError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1172 raise FormatError('Invalid sequence specification: %s' %
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1173 line.rstrip())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1174 return results
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1175
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1176 def set_sequences(self, sequences):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1177 """Set sequences using the given name-to-key-list dictionary."""
72966
5ea81e4c58a7 Close #12454: The mailbox module is now using ASCII, instead of the locale
Victor Stinner <victor.stinner@haypocalc.com>
parents: 70857
diff changeset
1178 f = open(os.path.join(self._path, '.mh_sequences'), 'r+', encoding='ASCII')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1179 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1180 os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC))
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
1181 for name, keys in sequences.items():
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1182 if len(keys) == 0:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1183 continue
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1184 f.write(name + ':')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1185 prev = None
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1186 completing = False
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1187 for key in sorted(set(keys)):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1188 if key - 1 == prev:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1189 if not completing:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1190 completing = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1191 f.write('-')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1192 elif completing:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1193 completing = False
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1194 f.write('%s %s' % (prev, key))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1195 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1196 f.write(' %s' % key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1197 prev = key
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1198 if completing:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1199 f.write(str(prev) + '\n')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1200 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1201 f.write('\n')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1202 finally:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1203 _sync_close(f)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1204
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1205 def pack(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1206 """Re-name messages to eliminate numbering gaps. Invalidates keys."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1207 sequences = self.get_sequences()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1208 prev = 0
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1209 changes = []
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
1210 for key in self.keys():
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1211 if key - 1 != prev:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1212 changes.append((key, prev + 1))
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1213 if hasattr(os, 'link'):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1214 os.link(os.path.join(self._path, str(key)),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1215 os.path.join(self._path, str(prev + 1)))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1216 os.unlink(os.path.join(self._path, str(key)))
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1217 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1218 os.rename(os.path.join(self._path, str(key)),
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1219 os.path.join(self._path, str(prev + 1)))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1220 prev += 1
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1221 self._next_key = prev + 1
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1222 if len(changes) == 0:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1223 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1224 for name, key_list in sequences.items():
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1225 for old, new in changes:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1226 if old in key_list:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1227 key_list[key_list.index(old)] = new
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1228 self.set_sequences(sequences)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1229
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1230 def _dump_sequences(self, message, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1231 """Inspect a new MHMessage and update sequences appropriately."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1232 pending_sequences = message.get_sequences()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1233 all_sequences = self.get_sequences()
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
1234 for name, key_list in all_sequences.items():
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1235 if name in pending_sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1236 key_list.append(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1237 elif key in key_list:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1238 del key_list[key_list.index(key)]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1239 for sequence in pending_sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1240 if sequence not in all_sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1241 all_sequences[sequence] = [key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1242 self.set_sequences(all_sequences)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1243
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1244
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1245 class Babyl(_singlefileMailbox):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1246 """An Rmail-style Babyl mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1247
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1248 _special_labels = frozenset(('unseen', 'deleted', 'filed', 'answered',
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1249 'forwarded', 'edited', 'resent'))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1250
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1251 def __init__(self, path, factory=None, create=True):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1252 """Initialize a Babyl mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1253 _singlefileMailbox.__init__(self, path, factory, create)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1254 self._labels = {}
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1255
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1256 def add(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1257 """Add message and return assigned key."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1258 key = _singlefileMailbox.add(self, message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1259 if isinstance(message, BabylMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1260 self._labels[key] = message.get_labels()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1261 return key
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1262
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1263 def remove(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1264 """Remove the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1265 _singlefileMailbox.remove(self, key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1266 if key in self._labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1267 del self._labels[key]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1268
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1269 def __setitem__(self, key, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1270 """Replace the keyed message; raise KeyError if it doesn't exist."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1271 _singlefileMailbox.__setitem__(self, key, message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1272 if isinstance(message, BabylMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1273 self._labels[key] = message.get_labels()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1274
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1275 def get_message(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1276 """Return a Message representation or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1277 start, stop = self._lookup(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1278 self._file.seek(start)
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1279 self._file.readline() # Skip b'1,' line specifying labels.
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1280 original_headers = io.BytesIO()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1281 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1282 line = self._file.readline()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1283 if line == b'*** EOOH ***' + linesep or not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1284 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1285 original_headers.write(line.replace(linesep, b'\n'))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1286 visible_headers = io.BytesIO()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1287 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1288 line = self._file.readline()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1289 if line == linesep or not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1290 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1291 visible_headers.write(line.replace(linesep, b'\n'))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1292 # Read up to the stop, or to the end
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1293 n = stop - self._file.tell()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1294 assert n >= 0
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1295 body = self._file.read(n)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1296 body = body.replace(linesep, b'\n')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1297 msg = BabylMessage(original_headers.getvalue() + body)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1298 msg.set_visible(visible_headers.getvalue())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1299 if key in self._labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1300 msg.set_labels(self._labels[key])
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1301 return msg
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1302
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1303 def get_bytes(self, key):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1304 """Return a string representation or raise a KeyError."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1305 start, stop = self._lookup(key)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1306 self._file.seek(start)
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1307 self._file.readline() # Skip b'1,' line specifying labels.
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1308 original_headers = io.BytesIO()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1309 while True:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1310 line = self._file.readline()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1311 if line == b'*** EOOH ***' + linesep or not line:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1312 break
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1313 original_headers.write(line.replace(linesep, b'\n'))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1314 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1315 line = self._file.readline()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1316 if line == linesep or not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1317 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1318 headers = original_headers.getvalue()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1319 n = stop - self._file.tell()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1320 assert n >= 0
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1321 data = self._file.read(n)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1322 data = data.replace(linesep, b'\n')
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1323 return headers + data
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1324
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1325 def get_file(self, key):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1326 """Return a file-like representation or raise a KeyError."""
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1327 return io.BytesIO(self.get_bytes(key).replace(b'\n', linesep))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1328
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1329 def get_labels(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1330 """Return a list of user-defined labels in the mailbox."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1331 self._lookup()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1332 labels = set()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1333 for label_list in self._labels.values():
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1334 labels.update(label_list)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1335 labels.difference_update(self._special_labels)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1336 return list(labels)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1337
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1338 def _generate_toc(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1339 """Generate key-to-(start, stop) table of contents."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1340 starts, stops = [], []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1341 self._file.seek(0)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1342 next_pos = 0
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1343 label_lists = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1344 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1345 line_pos = next_pos
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1346 line = self._file.readline()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1347 next_pos = self._file.tell()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1348 if line == b'\037\014' + linesep:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1349 if len(stops) < len(starts):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1350 stops.append(line_pos - len(linesep))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1351 starts.append(next_pos)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1352 labels = [label.strip() for label
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1353 in self._file.readline()[1:].split(b',')
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1354 if label.strip()]
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1355 label_lists.append(labels)
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1356 elif line == b'\037' or line == b'\037' + linesep:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1357 if len(stops) < len(starts):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1358 stops.append(line_pos - len(linesep))
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1359 elif not line:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1360 stops.append(line_pos - len(linesep))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1361 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1362 self._toc = dict(enumerate(zip(starts, stops)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1363 self._labels = dict(enumerate(label_lists))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1364 self._next_key = len(self._toc)
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
1365 self._file.seek(0, 2)
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
1366 self._file_length = self._file.tell()
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40737
diff changeset
1367
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1368 def _pre_mailbox_hook(self, f):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1369 """Called before writing the mailbox to file f."""
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1370 babyl = b'BABYL OPTIONS:' + linesep
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1371 babyl += b'Version: 5' + linesep
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1372 labels = self.get_labels()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1373 labels = (label.encode() for label in labels)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1374 babyl += b'Labels:' + b','.join(labels) + linesep
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1375 babyl += b'\037'
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1376 f.write(babyl)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1377
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1378 def _pre_message_hook(self, f):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1379 """Called before writing each message to file f."""
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1380 f.write(b'\014' + linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1381
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1382 def _post_message_hook(self, f):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1383 """Called after writing each message to file f."""
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1384 f.write(linesep + b'\037')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1385
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1386 def _install_message(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1387 """Write message contents and return (start, stop)."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1388 start = self._file.tell()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1389 if isinstance(message, BabylMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1390 special_labels = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1391 labels = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1392 for label in message.get_labels():
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1393 if label in self._special_labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1394 special_labels.append(label)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1395 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1396 labels.append(label)
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1397 self._file.write(b'1')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1398 for label in special_labels:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1399 self._file.write(b', ' + label.encode())
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1400 self._file.write(b',,')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1401 for label in labels:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1402 self._file.write(b' ' + label.encode() + b',')
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1403 self._file.write(linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1404 else:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1405 self._file.write(b'1,,' + linesep)
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1406 if isinstance(message, email.message.Message):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1407 orig_buffer = io.BytesIO()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1408 orig_generator = email.generator.BytesGenerator(orig_buffer, False, 0)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1409 orig_generator.flatten(message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1410 orig_buffer.seek(0)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1411 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1412 line = orig_buffer.readline()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1413 self._file.write(line.replace(b'\n', linesep))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1414 if line == b'\n' or not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1415 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1416 self._file.write(b'*** EOOH ***' + linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1417 if isinstance(message, BabylMessage):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1418 vis_buffer = io.BytesIO()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1419 vis_generator = email.generator.BytesGenerator(vis_buffer, False, 0)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1420 vis_generator.flatten(message.get_visible())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1421 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1422 line = vis_buffer.readline()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1423 self._file.write(line.replace(b'\n', linesep))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1424 if line == b'\n' or not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1425 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1426 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1427 orig_buffer.seek(0)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1428 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1429 line = orig_buffer.readline()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1430 self._file.write(line.replace(b'\n', linesep))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1431 if line == b'\n' or not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1432 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1433 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1434 buffer = orig_buffer.read(4096) # Buffer size is arbitrary.
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1435 if not buffer:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1436 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1437 self._file.write(buffer.replace(b'\n', linesep))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1438 elif isinstance(message, (bytes, str, io.StringIO)):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1439 if isinstance(message, io.StringIO):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1440 warnings.warn("Use of StringIO input is deprecated, "
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1441 "use BytesIO instead", DeprecationWarning, 3)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1442 message = message.getvalue()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1443 if isinstance(message, str):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1444 message = self._string_to_bytes(message)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1445 body_start = message.find(b'\n\n') + 2
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1446 if body_start - 2 != -1:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1447 self._file.write(message[:body_start].replace(b'\n', linesep))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1448 self._file.write(b'*** EOOH ***' + linesep)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1449 self._file.write(message[:body_start].replace(b'\n', linesep))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1450 self._file.write(message[body_start:].replace(b'\n', linesep))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1451 else:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1452 self._file.write(b'*** EOOH ***' + linesep + linesep)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1453 self._file.write(message.replace(b'\n', linesep))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1454 elif hasattr(message, 'readline'):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1455 if hasattr(message, 'buffer'):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1456 warnings.warn("Use of text mode files is deprecated, "
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1457 "use a binary mode file instead", DeprecationWarning, 3)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1458 message = message.buffer
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1459 original_pos = message.tell()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1460 first_pass = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1461 while True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1462 line = message.readline()
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1463 # Universal newline support.
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1464 if line.endswith(b'\r\n'):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1465 line = line[:-2] + b'\n'
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1466 elif line.endswith(b'\r'):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1467 line = line[:-1] + b'\n'
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1468 self._file.write(line.replace(b'\n', linesep))
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1469 if line == b'\n' or not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1470 if first_pass:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1471 first_pass = False
78585
cbc1dc8cda06 #11062: Fix adding a message from file to Babyl mailbox
Petri Lehtinen <petri@digip.org>
parents: 77851
diff changeset
1472 self._file.write(b'*** EOOH ***' + linesep)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1473 message.seek(original_pos)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1474 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1475 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1476 while True:
78608
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1477 line = message.readline()
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1478 if not line:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1479 break
78608
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1480 # Universal newline support.
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1481 if line.endswith(b'\r\n'):
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1482 line = line[:-2] + linesep
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1483 elif line.endswith(b'\r'):
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1484 line = line[:-1] + linesep
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1485 elif line.endswith(b'\n'):
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1486 line = line[:-1] + linesep
770ffc91a82e #11062: Fix universal newline support in Babyl._install_message()
Petri Lehtinen <petri@digip.org>
parents: 78585
diff changeset
1487 self._file.write(line)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1488 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1489 raise TypeError('Invalid message type: %s' % type(message))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1490 stop = self._file.tell()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1491 return (start, stop)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1492
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1493
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1494 class Message(email.message.Message):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1495 """Message with mailbox-format-specific properties."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1496
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1497 def __init__(self, message=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1498 """Initialize a Message instance."""
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1499 if isinstance(message, email.message.Message):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1500 self._become_message(copy.deepcopy(message))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1501 if isinstance(message, Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1502 message._explain_to(self)
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1503 elif isinstance(message, bytes):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1504 self._become_message(email.message_from_bytes(message))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1505 elif isinstance(message, str):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1506 self._become_message(email.message_from_string(message))
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1507 elif isinstance(message, io.TextIOWrapper):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1508 self._become_message(email.message_from_file(message))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1509 elif hasattr(message, "read"):
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1510 self._become_message(email.message_from_binary_file(message))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1511 elif message is None:
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1512 email.message.Message.__init__(self)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1513 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1514 raise TypeError('Invalid message type: %s' % type(message))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1515
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1516 def _become_message(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1517 """Assume the non-format-specific state of message."""
76175
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1518 type_specific = getattr(message, '_type_specific_attributes', [])
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1519 for name in message.__dict__:
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1520 if name not in type_specific:
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1521 self.__dict__[name] = message.__dict__[name]
1596
2197dd241d8d Jack's module for parsing UNIX mailbox files
Guido van Rossum <guido@python.org>
parents:
diff changeset
1522
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1523 def _explain_to(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1524 """Copy format-specific state to message insofar as possible."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1525 if isinstance(message, Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1526 return # There's nothing format-specific to explain.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1527 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1528 raise TypeError('Cannot convert to specified type')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1529
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1530
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1531 class MaildirMessage(Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1532 """Message with Maildir-specific properties."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1533
76175
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1534 _type_specific_attributes = ['_subdir', '_info', '_date']
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1535
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1536 def __init__(self, message=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1537 """Initialize a MaildirMessage instance."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1538 self._subdir = 'new'
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1539 self._info = ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1540 self._date = time.time()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1541 Message.__init__(self, message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1542
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1543 def get_subdir(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1544 """Return 'new' or 'cur'."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1545 return self._subdir
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1546
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1547 def set_subdir(self, subdir):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1548 """Set subdir to 'new' or 'cur'."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1549 if subdir == 'new' or subdir == 'cur':
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1550 self._subdir = subdir
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1551 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1552 raise ValueError("subdir must be 'new' or 'cur': %s" % subdir)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1553
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1554 def get_flags(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1555 """Return as a string the flags that are set."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1556 if self._info.startswith('2,'):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1557 return self._info[2:]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1558 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1559 return ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1560
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1561 def set_flags(self, flags):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1562 """Set the given flags and unset all others."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1563 self._info = '2,' + ''.join(sorted(flags))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1564
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1565 def add_flag(self, flag):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1566 """Set the given flag(s) without changing others."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1567 self.set_flags(''.join(set(self.get_flags()) | set(flag)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1568
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1569 def remove_flag(self, flag):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1570 """Unset the given string flag(s) without changing others."""
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1571 if self.get_flags():
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1572 self.set_flags(''.join(set(self.get_flags()) - set(flag)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1573
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1574 def get_date(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1575 """Return delivery date of message, in seconds since the epoch."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1576 return self._date
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1577
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1578 def set_date(self, date):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1579 """Set delivery date of message, in seconds since the epoch."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1580 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1581 self._date = float(date)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1582 except ValueError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1583 raise TypeError("can't convert to float: %s" % date)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1584
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1585 def get_info(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1586 """Get the message's "info" as a string."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1587 return self._info
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1588
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1589 def set_info(self, info):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1590 """Set the message's "info" string."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1591 if isinstance(info, str):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1592 self._info = info
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1593 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1594 raise TypeError('info must be a string: %s' % type(info))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1595
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1596 def _explain_to(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1597 """Copy Maildir-specific state to message insofar as possible."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1598 if isinstance(message, MaildirMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1599 message.set_flags(self.get_flags())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1600 message.set_subdir(self.get_subdir())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1601 message.set_date(self.get_date())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1602 elif isinstance(message, _mboxMMDFMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1603 flags = set(self.get_flags())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1604 if 'S' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1605 message.add_flag('R')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1606 if self.get_subdir() == 'cur':
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1607 message.add_flag('O')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1608 if 'T' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1609 message.add_flag('D')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1610 if 'F' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1611 message.add_flag('F')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1612 if 'R' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1613 message.add_flag('A')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1614 message.set_from('MAILER-DAEMON', time.gmtime(self.get_date()))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1615 elif isinstance(message, MHMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1616 flags = set(self.get_flags())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1617 if 'S' not in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1618 message.add_sequence('unseen')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1619 if 'R' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1620 message.add_sequence('replied')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1621 if 'F' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1622 message.add_sequence('flagged')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1623 elif isinstance(message, BabylMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1624 flags = set(self.get_flags())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1625 if 'S' not in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1626 message.add_label('unseen')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1627 if 'T' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1628 message.add_label('deleted')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1629 if 'R' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1630 message.add_label('answered')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1631 if 'P' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1632 message.add_label('forwarded')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1633 elif isinstance(message, Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1634 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1635 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1636 raise TypeError('Cannot convert to specified type: %s' %
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1637 type(message))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1638
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1639
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1640 class _mboxMMDFMessage(Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1641 """Message with mbox- or MMDF-specific properties."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1642
76175
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1643 _type_specific_attributes = ['_from']
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1644
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1645 def __init__(self, message=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1646 """Initialize an mboxMMDFMessage instance."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1647 self.set_from('MAILER-DAEMON', True)
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1648 if isinstance(message, email.message.Message):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1649 unixfrom = message.get_unixfrom()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1650 if unixfrom is not None and unixfrom.startswith('From '):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1651 self.set_from(unixfrom[5:])
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1652 Message.__init__(self, message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1653
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1654 def get_from(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1655 """Return contents of "From " line."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1656 return self._from
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1657
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1658 def set_from(self, from_, time_=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1659 """Set "From " line, formatting and appending time_ if specified."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1660 if time_ is not None:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1661 if time_ is True:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1662 time_ = time.gmtime()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1663 from_ += ' ' + time.asctime(time_)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1664 self._from = from_
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1665
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1666 def get_flags(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1667 """Return as a string the flags that are set."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1668 return self.get('Status', '') + self.get('X-Status', '')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1669
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1670 def set_flags(self, flags):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1671 """Set the given flags and unset all others."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1672 flags = set(flags)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1673 status_flags, xstatus_flags = '', ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1674 for flag in ('R', 'O'):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1675 if flag in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1676 status_flags += flag
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1677 flags.remove(flag)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1678 for flag in ('D', 'F', 'A'):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1679 if flag in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1680 xstatus_flags += flag
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1681 flags.remove(flag)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1682 xstatus_flags += ''.join(sorted(flags))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1683 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1684 self.replace_header('Status', status_flags)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1685 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1686 self.add_header('Status', status_flags)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1687 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1688 self.replace_header('X-Status', xstatus_flags)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1689 except KeyError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1690 self.add_header('X-Status', xstatus_flags)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1691
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1692 def add_flag(self, flag):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1693 """Set the given flag(s) without changing others."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1694 self.set_flags(''.join(set(self.get_flags()) | set(flag)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1695
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1696 def remove_flag(self, flag):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1697 """Unset the given string flag(s) without changing others."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1698 if 'Status' in self or 'X-Status' in self:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1699 self.set_flags(''.join(set(self.get_flags()) - set(flag)))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1700
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1701 def _explain_to(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1702 """Copy mbox- or MMDF-specific state to message insofar as possible."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1703 if isinstance(message, MaildirMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1704 flags = set(self.get_flags())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1705 if 'O' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1706 message.set_subdir('cur')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1707 if 'F' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1708 message.add_flag('F')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1709 if 'A' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1710 message.add_flag('R')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1711 if 'R' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1712 message.add_flag('S')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1713 if 'D' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1714 message.add_flag('T')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1715 del message['status']
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1716 del message['x-status']
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1717 maybe_date = ' '.join(self.get_from().split()[-5:])
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1718 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1719 message.set_date(calendar.timegm(time.strptime(maybe_date,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1720 '%a %b %d %H:%M:%S %Y')))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1721 except (ValueError, OverflowError):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1722 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1723 elif isinstance(message, _mboxMMDFMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1724 message.set_flags(self.get_flags())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1725 message.set_from(self.get_from())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1726 elif isinstance(message, MHMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1727 flags = set(self.get_flags())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1728 if 'R' not in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1729 message.add_sequence('unseen')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1730 if 'A' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1731 message.add_sequence('replied')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1732 if 'F' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1733 message.add_sequence('flagged')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1734 del message['status']
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1735 del message['x-status']
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1736 elif isinstance(message, BabylMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1737 flags = set(self.get_flags())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1738 if 'R' not in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1739 message.add_label('unseen')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1740 if 'D' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1741 message.add_label('deleted')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1742 if 'A' in flags:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1743 message.add_label('answered')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1744 del message['status']
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1745 del message['x-status']
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1746 elif isinstance(message, Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1747 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1748 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1749 raise TypeError('Cannot convert to specified type: %s' %
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1750 type(message))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1751
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1752
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1753 class mboxMessage(_mboxMMDFMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1754 """Message with mbox-specific properties."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1755
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1756
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1757 class MHMessage(Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1758 """Message with MH-specific properties."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1759
76175
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1760 _type_specific_attributes = ['_sequences']
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1761
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1762 def __init__(self, message=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1763 """Initialize an MHMessage instance."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1764 self._sequences = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1765 Message.__init__(self, message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1766
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1767 def get_sequences(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1768 """Return a list of sequences that include the message."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1769 return self._sequences[:]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1770
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1771 def set_sequences(self, sequences):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1772 """Set the list of sequences that include the message."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1773 self._sequences = list(sequences)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1774
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1775 def add_sequence(self, sequence):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1776 """Add sequence to list of sequences including the message."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1777 if isinstance(sequence, str):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1778 if not sequence in self._sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1779 self._sequences.append(sequence)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1780 else:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1781 raise TypeError('sequence type must be str: %s' % type(sequence))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1782
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1783 def remove_sequence(self, sequence):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1784 """Remove sequence from the list of sequences including the message."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1785 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1786 self._sequences.remove(sequence)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1787 except ValueError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1788 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1789
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1790 def _explain_to(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1791 """Copy MH-specific state to message insofar as possible."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1792 if isinstance(message, MaildirMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1793 sequences = set(self.get_sequences())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1794 if 'unseen' in sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1795 message.set_subdir('cur')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1796 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1797 message.set_subdir('cur')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1798 message.add_flag('S')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1799 if 'flagged' in sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1800 message.add_flag('F')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1801 if 'replied' in sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1802 message.add_flag('R')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1803 elif isinstance(message, _mboxMMDFMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1804 sequences = set(self.get_sequences())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1805 if 'unseen' not in sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1806 message.add_flag('RO')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1807 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1808 message.add_flag('O')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1809 if 'flagged' in sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1810 message.add_flag('F')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1811 if 'replied' in sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1812 message.add_flag('A')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1813 elif isinstance(message, MHMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1814 for sequence in self.get_sequences():
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1815 message.add_sequence(sequence)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1816 elif isinstance(message, BabylMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1817 sequences = set(self.get_sequences())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1818 if 'unseen' in sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1819 message.add_label('unseen')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1820 if 'replied' in sequences:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1821 message.add_label('answered')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1822 elif isinstance(message, Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1823 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1824 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1825 raise TypeError('Cannot convert to specified type: %s' %
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1826 type(message))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1827
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1828
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1829 class BabylMessage(Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1830 """Message with Babyl-specific properties."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1831
76175
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1832 _type_specific_attributes = ['_labels', '_visible']
5c1c402a63e5 #12537: in mailbox avoid depending on knowledge of email package internals
R David Murray <rdmurray@bitdance.com>
parents: 73370
diff changeset
1833
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1834 def __init__(self, message=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1835 """Initialize an BabylMessage instance."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1836 self._labels = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1837 self._visible = Message()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1838 Message.__init__(self, message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1839
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1840 def get_labels(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1841 """Return a list of labels on the message."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1842 return self._labels[:]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1843
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1844 def set_labels(self, labels):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1845 """Set the list of labels on the message."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1846 self._labels = list(labels)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1847
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1848 def add_label(self, label):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1849 """Add label to list of labels on the message."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1850 if isinstance(label, str):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1851 if label not in self._labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1852 self._labels.append(label)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1853 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1854 raise TypeError('label must be a string: %s' % type(label))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1855
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1856 def remove_label(self, label):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1857 """Remove label from the list of labels on the message."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1858 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1859 self._labels.remove(label)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1860 except ValueError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1861 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1862
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1863 def get_visible(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1864 """Return a Message representation of visible headers."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1865 return Message(self._visible)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1866
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1867 def set_visible(self, visible):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1868 """Set the Message representation of visible headers."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1869 self._visible = Message(visible)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1870
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1871 def update_visible(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1872 """Update and/or sensibly generate a set of visible headers."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1873 for header in self._visible.keys():
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1874 if header in self:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1875 self._visible.replace_header(header, self[header])
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1876 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1877 del self._visible[header]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1878 for header in ('Date', 'From', 'Reply-To', 'To', 'CC', 'Subject'):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1879 if header in self and header not in self._visible:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1880 self._visible[header] = self[header]
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1881
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1882 def _explain_to(self, message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1883 """Copy Babyl-specific state to message insofar as possible."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1884 if isinstance(message, MaildirMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1885 labels = set(self.get_labels())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1886 if 'unseen' in labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1887 message.set_subdir('cur')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1888 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1889 message.set_subdir('cur')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1890 message.add_flag('S')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1891 if 'forwarded' in labels or 'resent' in labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1892 message.add_flag('P')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1893 if 'answered' in labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1894 message.add_flag('R')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1895 if 'deleted' in labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1896 message.add_flag('T')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1897 elif isinstance(message, _mboxMMDFMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1898 labels = set(self.get_labels())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1899 if 'unseen' not in labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1900 message.add_flag('RO')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1901 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1902 message.add_flag('O')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1903 if 'deleted' in labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1904 message.add_flag('D')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1905 if 'answered' in labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1906 message.add_flag('A')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1907 elif isinstance(message, MHMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1908 labels = set(self.get_labels())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1909 if 'unseen' in labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1910 message.add_sequence('unseen')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1911 if 'answered' in labels:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1912 message.add_sequence('replied')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1913 elif isinstance(message, BabylMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1914 message.set_visible(self.get_visible())
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1915 for label in self.get_labels():
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1916 message.add_label(label)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1917 elif isinstance(message, Message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1918 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1919 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1920 raise TypeError('Cannot convert to specified type: %s' %
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1921 type(message))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1922
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1923
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1924 class MMDFMessage(_mboxMMDFMessage):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1925 """Message with MMDF-specific properties."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1926
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1927
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1928 class _ProxyFile:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1929 """A read-only wrapper of a file."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1930
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1931 def __init__(self, f, pos=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1932 """Initialize a _ProxyFile."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1933 self._file = f
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1934 if pos is None:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1935 self._pos = f.tell()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1936 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1937 self._pos = pos
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1938
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1939 def read(self, size=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1940 """Read bytes."""
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43122
diff changeset
1941 return self._read(size, self._file.read)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1942
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1943 def read1(self, size=None):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1944 """Read bytes."""
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1945 return self._read(size, self._file.read1)
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1946
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1947 def readline(self, size=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1948 """Read a line."""
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43122
diff changeset
1949 return self._read(size, self._file.readline)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1950
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1951 def readlines(self, sizehint=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1952 """Read multiple lines."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1953 result = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1954 for line in self:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1955 result.append(line)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1956 if sizehint is not None:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1957 sizehint -= len(line)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1958 if sizehint <= 0:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1959 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1960 return result
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1961
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1962 def __iter__(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1963 """Iterate over lines."""
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1964 while True:
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1965 line = self.readline()
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1966 if not line:
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1967 raise StopIteration
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
1968 yield line
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1969
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1970 def tell(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1971 """Return the position."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1972 return self._pos
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1973
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1974 def seek(self, offset, whence=0):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1975 """Change position."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1976 if whence == 1:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1977 self._file.seek(self._pos)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1978 self._file.seek(offset, whence)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1979 self._pos = self._file.tell()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1980
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1981 def close(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1982 """Close the file."""
70857
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
1983 if hasattr(self, '_file'):
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
1984 if hasattr(self._file, 'close'):
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
1985 self._file.close()
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
1986 del self._file
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1987
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1988 def _read(self, size, read_method):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1989 """Read size bytes using read_method."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1990 if size is None:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1991 size = -1
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1992 self._file.seek(self._pos)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1993 result = read_method(size)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1994 self._pos = self._file.tell()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1995 return result
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1996
65761
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
1997 def __enter__(self):
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
1998 """Context manager protocol support."""
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
1999 return self
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
2000
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
2001 def __exit__(self, *exc):
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
2002 self.close()
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
2003
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2004 def readable(self):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2005 return self._file.readable()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2006
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2007 def writable(self):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2008 return self._file.writable()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2009
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2010 def seekable(self):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2011 return self._file.seekable()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2012
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2013 def flush(self):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2014 return self._file.flush()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2015
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2016 @property
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2017 def closed(self):
70857
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
2018 if not hasattr(self, '_file'):
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
2019 return True
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
2020 if not hasattr(self._file, 'closed'):
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
2021 return False
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2022 return self._file.closed
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2023
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2024
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2025 class _PartialFile(_ProxyFile):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2026 """A read-only wrapper of part of a file."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2027
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2028 def __init__(self, f, start=None, stop=None):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2029 """Initialize a _PartialFile."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2030 _ProxyFile.__init__(self, f, start)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2031 self._start = start
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2032 self._stop = stop
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2033
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2034 def tell(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2035 """Return the position with respect to start."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2036 return _ProxyFile.tell(self) - self._start
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2037
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2038 def seek(self, offset, whence=0):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2039 """Change position, possibly with respect to start or stop."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2040 if whence == 0:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2041 self._pos = self._start
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2042 whence = 1
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2043 elif whence == 2:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2044 self._pos = self._stop
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2045 whence = 1
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2046 _ProxyFile.seek(self, offset, whence)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2047
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2048 def _read(self, size, read_method):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2049 """Read size bytes using read_method, honoring start and stop."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2050 remaining = self._stop - self._pos
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2051 if remaining <= 0:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2052 return b''
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2053 if size is None or size < 0 or size > remaining:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2054 size = remaining
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2055 return _ProxyFile._read(self, size, read_method)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2056
65761
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
2057 def close(self):
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
2058 # do *not* close the underlying file object for partial files,
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
2059 # since it's global to the mailbox object
70857
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
2060 if hasattr(self, '_file'):
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
2061 del self._file
65761
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
2062
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2063
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2064 def _lock_file(f, dotlock=True):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2065 """Lock file f using lockf and dot locking."""
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2066 dotlock_done = False
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2067 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2068 if fcntl:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2069 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2070 fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
2071 except IOError as e:
68220
b5281f5fade4 #11306: Treat EROFS like EACCES when making a 'file is read-only' decision
R. David Murray <rdmurray@bitdance.com>
parents: 67931
diff changeset
2072 if e.errno in (errno.EAGAIN, errno.EACCES, errno.EROFS):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2073 raise ExternalClashError('lockf: lock unavailable: %s' %
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2074 f.name)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2075 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2076 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2077 if dotlock:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2078 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2079 pre_lock = _create_temporary(f.name + '.lock')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2080 pre_lock.close()
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
2081 except IOError as e:
68220
b5281f5fade4 #11306: Treat EROFS like EACCES when making a 'file is read-only' decision
R. David Murray <rdmurray@bitdance.com>
parents: 67931
diff changeset
2082 if e.errno in (errno.EACCES, errno.EROFS):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2083 return # Without write access, just skip dotlocking.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2084 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2085 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2086 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2087 if hasattr(os, 'link'):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2088 os.link(pre_lock.name, f.name + '.lock')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2089 dotlock_done = True
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2090 os.unlink(pre_lock.name)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2091 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2092 os.rename(pre_lock.name, f.name + '.lock')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2093 dotlock_done = True
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
2094 except OSError as e:
79480
15949f34e08f #16135: Removal of OS/2 support (Python code partial cleanup)
Jesus Cea <jcea@jcea.es>
parents: 79479
diff changeset
2095 if e.errno == errno.EEXIST:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2096 os.remove(pre_lock.name)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2097 raise ExternalClashError('dot lock unavailable: %s' %
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2098 f.name)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2099 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2100 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2101 except:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2102 if fcntl:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2103 fcntl.lockf(f, fcntl.LOCK_UN)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2104 if dotlock_done:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2105 os.remove(f.name + '.lock')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2106 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2107
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2108 def _unlock_file(f):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2109 """Unlock file f using lockf and dot locking."""
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2110 if fcntl:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2111 fcntl.lockf(f, fcntl.LOCK_UN)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2112 if os.path.exists(f.name + '.lock'):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2113 os.remove(f.name + '.lock')
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2114
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2115 def _create_carefully(path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2116 """Create a file if it doesn't exist and open for reading and writing."""
48680
7779af388aec Merged revisions 65459,65472,65481,65518,65536,65581,65609,65637,65641,65644-65645 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46907
diff changeset
2117 fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o666)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2118 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2119 return open(path, 'rb+')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2120 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2121 os.close(fd)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2122
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2123 def _create_temporary(path):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2124 """Create a temp file based on path and open for reading and writing."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2125 return _create_carefully('%s.%s.%s.%s' % (path, int(time.time()),
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2126 socket.gethostname(),
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2127 os.getpid()))
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2128
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2129 def _sync_flush(f):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2130 """Ensure changes to file f are physically on disk."""
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2131 f.flush()
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
2132 if hasattr(os, 'fsync'):
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
2133 os.fsync(f.fileno())
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2134
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2135 def _sync_close(f):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2136 """Close file f, ensuring all changes are physically on disk."""
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2137 _sync_flush(f)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2138 f.close()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2139
4316
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
2140
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2141 class Error(Exception):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2142 """Raised for module-specific errors."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2143
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2144 class NoSuchMailboxError(Error):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2145 """The specified mailbox does not exist and won't be created."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2146
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2147 class NotEmptyError(Error):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2148 """The specified mailbox is not empty and deletion was requested."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2149
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2150 class ExternalClashError(Error):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2151 """Another process caused an action to fail."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2152
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2153 class FormatError(Error):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2154 """A file appears to have an invalid format."""