gh-66515: mailbox.MH now supports folders withou the ".mh_sequences" file (GH-804)

(for example Claws Mail IMAP-cache folders).
This commit is contained in:
Serhiy Storchaka 2023-12-26 11:15:14 +02:00 committed by GitHub
parent b5dc0f83ad
commit e87cadc1ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 3 deletions

View File

@ -644,6 +644,10 @@ Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF.
:class:`!MH` instances have all of the methods of :class:`Mailbox` in addition
to the following:
.. versionchanged:: 3.13
Supported folders that don't contain a :file:`.mh_sequences` file.
.. method:: list_folders()

View File

@ -1198,7 +1198,11 @@ class MH(Mailbox):
def get_sequences(self):
"""Return a name-to-key-list dictionary to define each sequence."""
results = {}
with open(os.path.join(self._path, '.mh_sequences'), 'r', encoding='ASCII') as f:
try:
f = open(os.path.join(self._path, '.mh_sequences'), 'r', encoding='ASCII')
except FileNotFoundError:
return results
with f:
all_keys = set(self.keys())
for line in f:
try:
@ -1221,9 +1225,8 @@ class MH(Mailbox):
def set_sequences(self, sequences):
"""Set sequences using the given name-to-key-list dictionary."""
f = open(os.path.join(self._path, '.mh_sequences'), 'r+', encoding='ASCII')
f = open(os.path.join(self._path, '.mh_sequences'), 'w', encoding='ASCII')
try:
os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC))
for name, keys in sequences.items():
if len(keys) == 0:
continue

View File

@ -1347,6 +1347,19 @@ class TestMH(TestMailbox, unittest.TestCase):
self._box.remove(key1)
self.assertEqual(self._box.get_sequences(), {'flagged':[key0]})
self._box.set_sequences({'foo':[key0]})
self.assertEqual(self._box.get_sequences(), {'foo':[key0]})
def test_no_dot_mh_sequences_file(self):
path = os.path.join(self._path, 'foo.bar')
os.mkdir(path)
box = self._factory(path)
self.assertEqual(os.listdir(path), [])
self.assertEqual(box.get_sequences(), {})
self.assertEqual(os.listdir(path), [])
box.set_sequences({})
self.assertEqual(os.listdir(path), ['.mh_sequences'])
def test_issue2625(self):
msg0 = mailbox.MHMessage(self._template % 0)
msg0.add_sequence('foo')

View File

@ -0,0 +1,3 @@
:class:`mailbox.MH` now supports folders that do not contain a
``.mh_sequences`` file (e.g. Claws Mail IMAP-cache folders). Patch by Serhiy
Storchaka.