annotate Lib/mailbox.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 13fb85ef0eea
children cbc1dc8cda06 23beb7f8561a
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 if sys.platform == 'os2emx':
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
26 # OS/2 EMX fcntl() not adequate
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
27 raise ImportError
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
28 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
29 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
30 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
31
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
32 __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
33 'Message', 'MaildirMessage', 'mboxMessage', 'MHMessage',
46893
67e4363ea2e0 fix __all__
Benjamin Peterson <benjamin@python.org>
parents: 46868
diff changeset
34 '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
35
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
36 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
37
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
38 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
39 """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
40
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
41 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
42 """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
43 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
44 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
45
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
46 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
47 """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
48 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
49
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
50 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
51 """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
52 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
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 __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
55 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
56
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
57 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
58 """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
59 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
60 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
61 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
62 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
63
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
64 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
65 """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
66 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
67
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
68 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
69 """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
70 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
71 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
72 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
73 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
74
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
75 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
76 """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
77 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
78 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
79 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
80 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
81 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
82
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
83 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
84 """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
85 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
86
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
87 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
88 """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
89
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
90 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
91 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
92 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
93
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
94 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
95 """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
96 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
97
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
98 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
99 """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
100 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
101
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
102 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
103 """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
104 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
105
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
106 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
107 """Return a list of keys."""
40821
429a4bc31c8e Fix another unit test.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
108 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
109
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
110 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
111 """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
112 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
113 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
114 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
115 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
116 continue
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
117 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
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 __iter__(self):
42309
bd6acfdb47d2 Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
120 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
121
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
122 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
123 """Return a list of messages. Memory intensive."""
40821
429a4bc31c8e Fix another unit test.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
124 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
125
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
126 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
127 """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
128 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
129 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
130 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
131 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
132 continue
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
133 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
134
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
135 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
136 """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
137 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
138
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
139 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
140 """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
141 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
142
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
143 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
144 """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
145 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
146
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
147 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
148 """Delete all messages."""
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
149 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
150 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
151
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
152 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
153 """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
154 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
155 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
156 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
157 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
158 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
159 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
160
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
161 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
162 """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
163 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
164 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
165 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
166 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
167
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
168 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
169 """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
170 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
171 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
172 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
173 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
174 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
175 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
176 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
177 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
178 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
179 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
180 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
181 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
182 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
183 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
184
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
185 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
186 """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
187 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
188
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
189 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
190 """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
191 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
192
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
193 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
194 """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
195 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
196
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
197 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
198 """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
199 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
200
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
201 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
202 # 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
203 # 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
204 # lies mojibake.
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
205 try:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
206 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
207 except UnicodeError:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
208 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
209 "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
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)
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
222 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
223 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
224 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
225 "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
226 message = message.getvalue()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
227 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
228 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
229 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
230 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
231 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
232 target.write(message)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
233 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
234 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
235 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
236 "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
237 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
238 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
239 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
240 # Universal newline support.
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
241 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
242 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
243 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
244 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
245 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
246 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
247 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
248 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
249 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
250 target.write(line)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
251 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
252 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
253
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
254
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
255 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
256 """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
257
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
258 colon = ':'
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
259
46907
6c15db6ce5da #756982: Remove rfc822 reference.
Georg Brandl <georg@python.org>
parents: 46893
diff changeset
260 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
261 """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
262 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
263 self._paths = {
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
264 '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
265 '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
266 '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
267 }
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
268 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
269 if create:
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41990
diff changeset
270 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
271 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
272 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
273 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
274 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
275 self._toc = {}
73369
5f27a9f67a34 Fix Maildir initialization so that maildir contents are read correctly.
Petri Lehtinen <petri@digip.org>
parents: 70857
diff changeset
276 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
277 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
278 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
279
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
280 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
281 """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
282 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
283 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
284 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
285 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
286 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
287 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
288 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
289 _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
290 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
291 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
292 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
293 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
294 suffix = ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
295 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
296 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
297 suffix = ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
298 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
299 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
300 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
301 if hasattr(os, 'link'):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
302 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
303 os.remove(tmp_file.name)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
304 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
305 os.rename(tmp_file.name, dest)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
306 except OSError as e:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
307 os.remove(tmp_file.name)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
308 if e.errno == errno.EEXIST:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
309 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
310 % dest)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
311 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
312 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
313 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
314 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
315 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
316
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
317 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
318 """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
319 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
320
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
321 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
322 """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
323 # 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
324 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
325 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
326 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
327 pass
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
328 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
329 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
330 raise
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 __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
333 """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
334 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
335 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
336 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
337 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
338 # 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
339 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
340 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
341 # 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
342 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
343 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
344 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
345 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
346 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
347 suffix = ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
348 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
349 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
350 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
351 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
352 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
353 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
354
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
355 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
356 """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
357 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
358 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
359 try:
44810
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
360 if self._factory:
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
361 msg = self._factory(f)
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
362 else:
fa6cee14dcba Merged revisions 60094-60123 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43777
diff changeset
363 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
364 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
365 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
366 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
367 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
368 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
369 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
370 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
371 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
372
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
373 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
374 """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
375 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
376 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
377 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
378 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
379 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
380
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
381 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
382 """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
383 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
384 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
385
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
386 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
387 """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
388 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
389 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
390 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
391 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
392 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
393 continue
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
394 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
395
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
396 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
397 """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
398 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
399 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
400
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
401 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
402 """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
403 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
404 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
405
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
406 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
407 """Write any pending changes to disk."""
57071
8a0d3c69ae21 Merged revisions 76055 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 53698
diff changeset
408 # 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
409 # to do.
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
410 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
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 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
413 """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
414 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
415
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
416 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
417 """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
418 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
419
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
420 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
421 """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
422 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
423
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
424 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
425 """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
426 result = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
427 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
428 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
429 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
430 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
431 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
432
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
433 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
434 """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
435 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
436 factory=self._factory,
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
437 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
438
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
439 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
440 """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
441 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
442 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
443 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
444 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
445 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
446 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
447 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
448
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
449 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
450 """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
451 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
452 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
453 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
454 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
455 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
456 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
457 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
458 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
459 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
460 (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
461 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
462 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
463 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
464 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
465 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
466 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
467
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
468 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
469 """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
470 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
471 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
472 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
473 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
474 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
475
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
476 _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
477
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
478 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
479 """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
480 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
481 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
482 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
483 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
484 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
485 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
486 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
487 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
488 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
489 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
490 os.stat(path)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
491 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
492 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
493 Maildir._count += 1
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
494 try:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
495 return _create_carefully(path)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
496 except OSError as e:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
497 if e.errno != errno.EEXIST:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
498 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
499 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
500 raise
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
501
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
502 # 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
503 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
504 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
505
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
506 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
507 """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
508 # 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
509 # 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
510 # 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
511 # 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
512 # 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
513 # 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
514 # 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
515 # 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
516 # 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
517 # 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
518 # 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
519 # 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
520 # 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
521 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
522 refresh = False
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
523 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
524 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
525 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
526 refresh = True
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
527 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
528 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
529 return
252451fef13f #11999: sync based on comparing mtimes, not mtime to system clock
R David Murray <rdmurray@bitdance.com>
parents: 57071
diff changeset
530 # 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
531 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
532 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
533 path = self._paths[subdir]
53698
358ab1ffaa80 Merged revisions 72213 via svnmerge from
Andrew M. Kuchling <amk@amk.ca>
parents: 52747
diff changeset
534 for entry in os.listdir(path):
358ab1ffaa80 Merged revisions 72213 via svnmerge from
Andrew M. Kuchling <amk@amk.ca>
parents: 52747
diff changeset
535 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
536 if os.path.isdir(p):
bd6acfdb47d2 Merged revisions 56301-56442 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 42084
diff changeset
537 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
538 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
539 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
540 self._last_read = time.time()
53698
358ab1ffaa80 Merged revisions 72213 via svnmerge from
Andrew M. Kuchling <amk@amk.ca>
parents: 52747
diff changeset
541
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 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
543 """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
544 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
545 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
546 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
547 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
548 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
549 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
550 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
551 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
552 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
553 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
554
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
555 # 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
556 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
557 """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
558 if not hasattr(self, '_onetime_keys'):
40821
429a4bc31c8e Fix another unit test.
Guido van Rossum <guido@python.org>
parents: 40809
diff changeset
559 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
560 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
561 try:
41501
0c5019afc55b PEP 3114: rename .next() to .__next__() and add next() builtin.
Georg Brandl <georg@python.org>
parents: 40821
diff changeset
562 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
563 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
564 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
565 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
566 continue
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
567
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
568
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
569 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
570 """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
571
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
572 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
573 """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
574 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
575 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
576 f = open(self._path, 'rb+')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
577 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
578 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
579 if create:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
580 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
581 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
582 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
583 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
584 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
585 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
586 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
587 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
588 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
589 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
590 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
591 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
592 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
593 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
594
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
595 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
596 """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
597 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
598 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
599 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
600 # _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
601 # 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
602 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
603 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
604
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
605 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
606 """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
607 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
608 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
609 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
610
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
611 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
612 """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
613 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
614 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
615 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
616
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
617 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
618 """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
619 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
620 for key in 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
621 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
622
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
623 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
624 """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
625 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
626 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
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 __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
629 """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
630 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
631 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
632
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
633 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
634 """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
635 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
636 _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
637 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
638
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
639 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
640 """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
641 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
642 _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
643 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
644
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
645 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
646 """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
647 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
648 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
649 # 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
650 # 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
651 _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
652 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
653 return
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
654
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
655 # 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
656 # 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
657 # by adding or deleting an item).
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
658 assert self._toc is not None
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40737
diff changeset
659
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
660 # 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
661 # 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
662 self._file.seek(0, 2)
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
663 cur_len = self._file.tell()
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
664 if cur_len != self._file_length:
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
665 raise ExternalClashError('Size of mailbox file changed '
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
666 '(expected %i, found %i)' %
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
667 (self._file_length, cur_len))
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40737
diff changeset
668
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
669 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
670 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
671 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
672 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
673 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
674 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
675 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
676 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
677 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
678 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
679 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
680 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
681 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
682 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
683 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
684 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
685 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
686 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
687 except:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
688 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
689 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
690 raise
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
691 _sync_close(new_file)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
692 # 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
693 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
694 # 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
695 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
696 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
697 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
698 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
699 except OSError as e:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
700 if e.errno == errno.EEXIST or \
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
701 (os.name == 'os2' and e.errno == errno.EACCES):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
702 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
703 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
704 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
705 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
706 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
707 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
708 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
709 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
710 if self._locked:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
711 _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
712
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
713 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
714 """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
715 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
716
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
717 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
718 """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
719 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
720
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
721 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
722 """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
723 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
724
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
725 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
726 """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
727 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
728 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
729 self.unlock()
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
730 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
731
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
732 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
733 """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
734 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
735 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
736 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
737 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
738 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
739 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
740 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
741
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
742 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
743 """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
744 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
745 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
746 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
747 # 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
748 # 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
749 # 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
750 # 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
751 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
752 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
753 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
754 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
755 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
756 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
757 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
758 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
759 self._file.flush()
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
760 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
761 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
762
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
763
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
764
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
765 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
766 """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
767
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
768 _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
769
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
770 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
771 """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
772 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
773 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
774 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
775 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
776 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
777 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
778 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
779
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
780 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
781 """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
782 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
783 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
784
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
785 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
786 """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
787 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
788 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
789 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
790 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
791 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
792 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
793
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
794 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
795 """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
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 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
801
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
802 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
803 """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
804 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
805 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
806 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
807 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
808 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
809 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
810 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
811 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
812 else:
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 = message
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
814 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
815 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
816 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
817 from_line = b'From ' + author
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
818 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
819 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
820 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
821 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
822 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
823 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
824 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
825 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
826 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
827 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
828 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
829
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
830
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
831 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
832 """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
833
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
834 _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
835
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
836 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
837 """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
838 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
839 _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
840
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
841 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
842 """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
843 if f.tell() != 0:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
844 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
845
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
846 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
847 """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
848 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
849 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
850 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
851 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
852 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
853 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
854 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
855 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
856 starts.append(line_pos)
41990
e153ba352b51 Just enoug fixes so that test_mailbox fails instead of loops forever.
Guido van Rossum <guido@python.org>
parents: 41501
diff changeset
857 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
858 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
859 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
860 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
861 self._next_key = len(self._toc)
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
862 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
863
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
864
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
865 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
866 """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
867
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
868 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
869 """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
870 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
871 _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
872
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
873 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
874 """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
875 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
876
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
877 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
878 """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
879 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
880
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
881 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
882 """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
883 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
884 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
885 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
886 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
887 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
888 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
889 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
890 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
891 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
892 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
893 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
894 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
895 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
896 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
897 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
898 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
899 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
900 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
901 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
902 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
903 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
904 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
905 self._next_key = len(self._toc)
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
906 self._file.seek(0, 2)
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
907 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
908
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
909
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
910 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
911 """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
912
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
913 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
914 """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
915 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
916 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
917 if create:
42084
229c28cb9afb Merged revisions 55817-55961 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41990
diff changeset
918 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
919 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
920 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
921 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
922 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
923 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
924
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
925 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
926 """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
927 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
928 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
929 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
930 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
931 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
932 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
933 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
934 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
935 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
936 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
937 _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
938 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
939 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
940 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
941 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
942 # 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
943 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
944 _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
945 _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
946 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
947 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
948 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
949 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
950 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
951 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
952 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
953 _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
954 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
955 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
956 _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
957 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
958
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
959 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
960 """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
961 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
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 f = open(path, 'rb+')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
964 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
965 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
966 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
967 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
968 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
969 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
970 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
971 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
972
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
973 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
974 """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
975 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
976 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
977 f = open(path, 'rb+')
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
978 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
979 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
980 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
981 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
982 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
983 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
984 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
985 _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
986 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
987 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
988 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
989 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
990 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
991 finally:
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 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
993 _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
994 finally:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
995 _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
996
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
997 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
998 """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
999 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1000 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
1001 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
1002 else:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1003 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
1004 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
1005 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
1006 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
1007 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1008 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1009 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1010 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
1011 _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
1012 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1013 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
1014 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1015 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
1016 _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
1017 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1018 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
1019 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
1020 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
1021 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
1022 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
1023
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1024 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
1025 """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
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:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1040 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
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()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1046
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1047 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
1048 """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
1049 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1050 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
1051 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
1052 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
1053 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
1054 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1055 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1056 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
1057
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1058 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
1059 """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
1060 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
1061 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
1062
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
1063 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
1064 """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
1065 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
1066
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1067 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
1068 """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
1069 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
1070
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1071 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
1072 """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
1073 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
1074 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
1075 _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
1076 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
1077
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1078 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
1079 """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
1080 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
1081 _unlock_file(self._file)
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1082 _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
1083 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
1084 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
1085
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1086 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
1087 """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
1088 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1089
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1090 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
1091 """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
1092 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
1093 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
1094
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1095 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
1096 """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
1097 result = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1098 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
1099 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
1100 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
1101 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
1102
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1103 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
1104 """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
1105 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
1106 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
1107
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1108 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
1109 """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
1110 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
1111 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
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 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
1114 """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
1115 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
1116 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
1117 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
1118 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
1119 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
1120 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1121 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1122 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
1123 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
1124
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1125 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
1126 """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
1127 results = {}
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1128 f = open(os.path.join(self._path, '.mh_sequences'), 'r')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1129 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1130 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
1131 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
1132 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1133 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
1134 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
1135 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
1136 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
1137 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
1138 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1139 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
1140 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
1141 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
1142 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
1143 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
1144 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
1145 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
1146 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
1147 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
1148 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1149 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
1150 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
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 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
1153 """Set sequences using the given name-to-key-list dictionary."""
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1154 f = open(os.path.join(self._path, '.mh_sequences'), 'r+')
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1155 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1156 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
1157 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
1158 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
1159 continue
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1160 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
1161 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
1162 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
1163 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
1164 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
1165 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
1166 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
1167 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
1168 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
1169 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
1170 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
1171 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1172 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
1173 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
1174 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
1175 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
1176 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1177 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
1178 finally:
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1179 _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
1180
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1181 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
1182 """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
1183 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
1184 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
1185 changes = []
40809
f340cb045bf9 - PEP 3106: dict.iterkeys(), .iteritems(), .itervalues() are now gone;
Guido van Rossum <guido@python.org>
parents: 40742
diff changeset
1186 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
1187 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
1188 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
1189 if hasattr(os, 'link'):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1190 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
1191 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
1192 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
1193 else:
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
1194 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
1195 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
1196 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
1197 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
1198 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
1199 return
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1200 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
1201 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
1202 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
1203 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
1204 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
1205
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1206 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
1207 """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
1208 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
1209 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
1210 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
1211 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
1212 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
1213 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
1214 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
1215 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
1216 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
1217 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
1218 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
1219
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1220
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1221 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
1222 """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
1223
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1224 _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
1225 '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
1226
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1227 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
1228 """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
1229 _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
1230 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
1231
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1232 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
1233 """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
1234 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
1235 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
1236 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
1237 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
1238
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1239 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
1240 """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
1241 _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
1242 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
1243 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
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 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
1246 """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
1247 _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
1248 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
1249 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
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 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
1252 """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
1253 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
1254 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
1255 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
1256 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
1257 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
1258 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
1259 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
1260 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1261 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
1262 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
1263 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
1264 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
1265 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
1266 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1267 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
1268 # 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
1269 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
1270 assert n >= 0
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1271 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
1272 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
1273 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
1274 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
1275 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
1276 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
1277 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
1278
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1279 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
1280 """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
1281 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
1282 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
1283 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
1284 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
1285 while True:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1286 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
1287 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
1288 break
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1289 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
1290 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
1291 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
1292 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
1293 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1294 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
1295 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
1296 assert n >= 0
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1297 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
1298 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
1299 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
1300
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1301 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
1302 """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
1303 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
1304
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1305 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
1306 """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
1307 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
1308 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
1309 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
1310 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
1311 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
1312 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
1313
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1314 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
1315 """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
1316 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
1317 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
1318 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
1319 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
1320 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
1321 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
1322 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
1323 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
1324 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
1325 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
1326 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
1327 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
1328 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
1329 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
1330 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
1331 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
1332 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
1333 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
1334 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
1335 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
1336 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
1337 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1338 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
1339 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
1340 self._next_key = len(self._toc)
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
1341 self._file.seek(0, 2)
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
1342 self._file_length = self._file.tell()
40742
f1701ba2452b Merged revisions 53538-53622 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40737
diff changeset
1343
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1344 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
1345 """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
1346 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
1347 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
1348 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
1349 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
1350 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
1351 babyl += b'\037'
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1352 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
1353
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1354 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
1355 """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
1356 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
1357
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1358 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
1359 """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
1360 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
1361
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1362 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
1363 """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
1364 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
1365 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
1366 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
1367 labels = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1368 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
1369 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
1370 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
1371 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1372 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
1373 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
1374 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
1375 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
1376 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
1377 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
1378 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
1379 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
1380 else:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1381 self._file.write(b'1,,' + linesep)
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1382 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
1383 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
1384 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
1385 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
1386 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
1387 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
1388 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
1389 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
1390 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
1391 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1392 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
1393 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
1394 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
1395 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
1396 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
1397 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
1398 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
1399 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
1400 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
1401 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1402 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1403 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
1404 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
1405 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
1406 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
1407 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
1408 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1409 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
1410 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
1411 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
1412 break
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(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
1414 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
1415 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
1416 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
1417 "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
1418 message = message.getvalue()
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1419 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
1420 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
1421 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
1422 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
1423 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
1424 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
1425 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
1426 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
1427 else:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1428 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
1429 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
1430 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
1431 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
1432 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
1433 "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
1434 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
1435 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
1436 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
1437 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
1438 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
1439 # Universal newline support.
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1440 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
1441 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
1442 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
1443 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
1444 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
1445 if line == b'\n' or not line:
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1446 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
1447 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
1448 first_pass = False
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1449 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
1450 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1451 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1452 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
1453 buffer = message.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
1454 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
1455 break
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1456 self._file.write(buffer.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
1457 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1458 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
1459 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
1460 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
1461
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1462
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1463 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
1464 """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
1465
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1466 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
1467 """Initialize a Message instance."""
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1468 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
1469 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
1470 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
1471 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
1472 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
1473 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
1474 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
1475 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
1476 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
1477 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
1478 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
1479 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
1480 elif message is None:
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1481 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
1482 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1483 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
1484
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1485 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
1486 """Assume the non-format-specific state of message."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1487 for name in ('_headers', '_unixfrom', '_payload', '_charset',
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1488 'preamble', 'epilogue', 'defects', '_default_type'):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1489 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
1490
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1491 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
1492 """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
1493 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
1494 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
1495 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1496 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
1497
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1498
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1499 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
1500 """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
1501
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1502 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
1503 """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
1504 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
1505 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
1506 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
1507 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
1508
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1509 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
1510 """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
1511 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
1512
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1513 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
1514 """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
1515 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
1516 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
1517 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1518 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
1519
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1520 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
1521 """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
1522 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
1523 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
1524 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1525 return ''
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1526
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1527 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
1528 """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
1529 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
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 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
1532 """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
1533 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
1534
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1535 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
1536 """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
1537 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
1538 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
1539
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1540 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
1541 """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
1542 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
1543
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1544 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
1545 """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
1546 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1547 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
1548 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
1549 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
1550
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1551 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
1552 """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
1553 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
1554
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1555 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
1556 """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
1557 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
1558 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
1559 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1560 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
1561
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1562 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
1563 """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
1564 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
1565 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
1566 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
1567 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
1568 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
1569 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
1570 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
1571 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
1572 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
1573 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
1574 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
1575 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
1576 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
1577 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
1578 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
1579 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
1580 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
1581 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
1582 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
1583 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
1584 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
1585 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
1586 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
1587 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
1588 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
1589 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
1590 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
1591 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
1592 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
1593 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
1594 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
1595 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
1596 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
1597 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
1598 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
1599 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
1600 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1601 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1602 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
1603 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
1604
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1605
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1606 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
1607 """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
1608
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1609 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
1610 """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
1611 self.set_from('MAILER-DAEMON', True)
40737
6ebad2a6b990 Merged revisions 53451-53537 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40644
diff changeset
1612 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
1613 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
1614 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
1615 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
1616 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
1617
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1618 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
1619 """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
1620 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
1621
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1622 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
1623 """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
1624 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
1625 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
1626 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
1627 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
1628 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
1629
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1630 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
1631 """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
1632 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
1633
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1634 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
1635 """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
1636 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
1637 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
1638 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
1639 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
1640 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
1641 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
1642 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
1643 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
1644 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
1645 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
1646 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
1647 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1648 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
1649 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
1650 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
1651 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1652 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
1653 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
1654 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
1655
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1656 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
1657 """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
1658 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
1659
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1660 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
1661 """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
1662 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
1663 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
1664
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1665 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
1666 """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
1667 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
1668 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
1669 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
1670 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
1671 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
1672 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
1673 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
1674 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
1675 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
1676 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
1677 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
1678 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
1679 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
1680 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
1681 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
1682 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1683 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
1684 '%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
1685 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
1686 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1687 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
1688 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
1689 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
1690 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
1691 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
1692 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
1693 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
1694 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
1695 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
1696 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
1697 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
1698 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
1699 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
1700 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
1701 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
1702 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
1703 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
1704 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
1705 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
1706 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
1707 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
1708 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
1709 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
1710 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
1711 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1712 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1713 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
1714 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
1715
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1716
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1717 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
1718 """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
1719
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1720
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1721 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
1722 """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
1723
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1724 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
1725 """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
1726 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
1727 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
1728
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1729 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
1730 """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
1731 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
1732
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1733 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
1734 """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
1735 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
1736
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1737 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
1738 """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
1739 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
1740 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
1741 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
1742 else:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1743 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
1744
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1745 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
1746 """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
1747 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1748 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
1749 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
1750 pass
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 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
1753 """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
1754 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
1755 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
1756 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
1757 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
1758 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1759 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
1760 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
1761 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
1762 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
1763 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
1764 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
1765 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
1766 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
1767 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
1768 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
1769 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1770 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
1771 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
1772 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
1773 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
1774 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
1775 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
1776 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
1777 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
1778 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
1779 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
1780 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
1781 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
1782 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
1783 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
1784 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
1785 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1786 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1787 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
1788 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
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
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1791 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
1792 """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
1793
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1794 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
1795 """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
1796 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
1797 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
1798 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
1799
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1800 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
1801 """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
1802 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
1803
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1804 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
1805 """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
1806 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
1807
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1808 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
1809 """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
1810 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
1811 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
1812 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
1813 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1814 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
1815
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1816 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
1817 """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
1818 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1819 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
1820 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
1821 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1822
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1823 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
1824 """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
1825 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
1826
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1827 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
1828 """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
1829 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
1830
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1831 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
1832 """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
1833 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
1834 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
1835 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
1836 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1837 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
1838 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
1839 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
1840 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
1841
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1842 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
1843 """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
1844 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
1845 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
1846 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
1847 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
1848 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1849 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
1850 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
1851 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
1852 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
1853 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
1854 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
1855 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
1856 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
1857 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
1858 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
1859 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
1860 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
1861 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1862 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
1863 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
1864 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
1865 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
1866 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
1867 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
1868 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
1869 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
1870 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
1871 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
1872 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
1873 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
1874 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
1875 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
1876 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
1877 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
1878 pass
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1879 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1880 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
1881 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
1882
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1883
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1884 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
1885 """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
1886
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1887
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1888 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
1889 """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
1890
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1891 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
1892 """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
1893 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
1894 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
1895 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
1896 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1897 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
1898
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1899 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
1900 """Read bytes."""
43777
0d462d789b18 Merging the py3k-pep3137 branch back into the py3k branch.
Guido van Rossum <guido@python.org>
parents: 43122
diff changeset
1901 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
1902
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1903 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
1904 """Read bytes."""
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1905 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
1906
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1907 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
1908 """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
1909 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
1910
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1911 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
1912 """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
1913 result = []
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1914 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
1915 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
1916 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
1917 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
1918 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
1919 break
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1920 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
1921
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1922 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
1923 """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
1924 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
1925 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
1926 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
1927 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
1928 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
1929
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1930 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
1931 """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
1932 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
1933
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1934 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
1935 """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
1936 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
1937 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
1938 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
1939 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
1940
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1941 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
1942 """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
1943 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
1944 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
1945 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
1946 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
1947
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1948 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
1949 """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
1950 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
1951 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
1952 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
1953 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
1954 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
1955 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
1956
65761
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
1957 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
1958 """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
1959 return self
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
1960
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
1961 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
1962 self.close()
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
1963
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1964 def readable(self):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1965 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
1966
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1967 def writable(self):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1968 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
1969
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1970 def seekable(self):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1971 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
1972
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1973 def flush(self):
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1974 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
1975
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1976 @property
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1977 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
1978 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
1979 return True
8319db2dd342 #11700: proxy object close methods can now be called multiple times
R David Murray <rdmurray@bitdance.com>
parents: 70843
diff changeset
1980 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
1981 return False
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
1982 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
1983
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1984
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1985 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
1986 """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
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 __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
1989 """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
1990 _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
1991 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
1992 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
1993
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1994 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
1995 """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
1996 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
1997
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
1998 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
1999 """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
2000 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
2001 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
2002 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
2003 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
2004 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
2005 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
2006 _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
2007
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2008 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
2009 """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
2010 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
2011 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
2012 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
2013 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
2014 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
2015 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
2016
65761
e6af146bb38b Fix test_mailbox by supporting context manager protocol for get_file() returns.
Georg Brandl <georg@python.org>
parents: 59869
diff changeset
2017 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
2018 # 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
2019 # 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
2020 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
2021 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
2022
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2023
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2024 def _lock_file(f, dotlock=True):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2025 """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
2026 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
2027 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2028 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
2029 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2030 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
2031 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
2032 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
2033 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
2034 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
2035 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2036 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2037 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
2038 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2039 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
2040 pre_lock.close()
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
2041 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
2042 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
2043 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
2044 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2045 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2046 try:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2047 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
2048 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
2049 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
2050 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
2051 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2052 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
2053 dotlock_done = True
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 40582
diff changeset
2054 except OSError as e:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2055 if e.errno == errno.EEXIST or \
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2056 (os.name == 'os2' and e.errno == errno.EACCES):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2057 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
2058 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
2059 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
2060 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2061 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2062 except:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2063 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
2064 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
2065 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
2066 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
2067 raise
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2068
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2069 def _unlock_file(f):
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
2070 """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
2071 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
2072 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
2073 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
2074 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
2075
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2076 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
2077 """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
2078 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
2079 try:
67814
dc501efa05b4 #9124: mailbox now accepts binary input and uses binary internally
R. David Murray <rdmurray@bitdance.com>
parents: 65761
diff changeset
2080 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
2081 finally:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2082 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
2083
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2084 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
2085 """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
2086 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
2087 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
2088 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
2089
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2090 def _sync_flush(f):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2091 """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
2092 f.flush()
40582
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
2093 if hasattr(os, 'fsync'):
67287ca4e1e8 Merged revisions 53005-53303 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 40450
diff changeset
2094 os.fsync(f.fileno())
40450
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2095
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2096 def _sync_close(f):
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2097 """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
2098 _sync_flush(f)
cc992d75d5b3 Four months of trunk changes (including a few releases...)
Thomas Wouters <thomas@python.org>
parents: 39575
diff changeset
2099 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
2100
4316
772828c1b1f6 Revamped test case and updated doc string.
Guido van Rossum <guido@python.org>
parents: 3360
diff changeset
2101
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2102 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
2103 """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
2104
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2105 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
2106 """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
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 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
2109 """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
2110
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2111 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
2112 """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
2113
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 33215
diff changeset
2114 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
2115 """A file appears to have an invalid format."""