Skip to content

Commit

Permalink
+BaseMailBox.numbers_to_uids
Browse files Browse the repository at this point in the history
  • Loading branch information
ikvk committed Dec 11, 2024
1 parent be0a617 commit 02d8c51
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ BaseMailBox.idle - `idle manager <#idle-workflow>`_

BaseMailBox.numbers - search mailbox for matching message numbers in current folder, returns [str]

BaseMailBox.numbers_to_uids - Get message uids by message numbers, returns [str]

BaseMailBox.client - imaplib.IMAP4/IMAP4_SSL client instance.

Email attributes
Expand Down Expand Up @@ -422,7 +424,8 @@ Big thanks to people who helped develop this library:
`thomwiggers <https://github.com/thomwiggers>`_,
`histogal <https://github.com/histogal>`_,
`K900 <https://github.com/K900>`_,
`homoLudenus <https://github.com/homoLudenus>`_
`homoLudenus <https://github.com/homoLudenus>`_,
`sphh <https://github.com/sphh>`_

Help the project
----------------
Expand Down
4 changes: 4 additions & 0 deletions docs/release_notes.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.8.0
=====
* Added: BaseMailBox.numbers_to_uids - Get message uids by message numbers

1.7.4
=====
* Fixed: encoding bug at MailAttachment.content_id
Expand Down
2 changes: 1 addition & 1 deletion imap_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
from .utils import EmailAddress
from .errors import *

__version__ = '1.7.4'
__version__ = '1.8.0'
15 changes: 15 additions & 0 deletions imap_tools/mailbox.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import re
import sys
import imaplib
import datetime
Expand All @@ -7,6 +8,7 @@
from .message import MailMessage
from .folder import MailBoxFolderManager
from .idle import IdleManager
from .consts import UID_PATTERN
from .utils import clean_uids, check_command_status, chunks, encode_folder, clean_flags, check_timeout_arg_support, \
chunks_crop
from .errors import MailboxStarttlsError, MailboxLoginError, MailboxLogoutError, MailboxNumbersError, \
Expand Down Expand Up @@ -109,6 +111,19 @@ def numbers(self, criteria: Criteria = 'ALL', charset: str = 'US-ASCII') -> List
check_command_status(search_result, MailboxNumbersError)
return search_result[1][0].decode().split() if search_result[1][0] else []

def numbers_to_uids(self, numbers: List[str]) -> List[str]:
"""Get message uids by message numbers"""
if not numbers:
return []
fetch_result = self.client.fetch(','.join(numbers), "(UID)")
check_command_status(fetch_result, MailboxFetchError)
result = []
for raw_uid_item in fetch_result[1]:
uid_match = re.search(UID_PATTERN, (raw_uid_item or b'').decode())
if uid_match:
result.append(uid_match.group('uid'))
return result

def uids(self, criteria: Criteria = 'ALL', charset: str = 'US-ASCII',
sort: Optional[Union[str, Iterable[str]]] = None) -> List[str]:
"""
Expand Down
5 changes: 5 additions & 0 deletions tests/test_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ def test_action(self):
self.assertTrue(bool(found_nums))
self.assertTrue(all(type(i) is str for i in found_nums))

# NUMBERS_TO_UIDS
found_uids = mailbox.numbers_to_uids(found_nums)
self.assertTrue(bool(found_uids))
self.assertTrue(all(type(i) is str for i in found_uids))

# UIDS
found_uids = mailbox.uids()
self.assertTrue(bool(found_uids))
Expand Down

0 comments on commit 02d8c51

Please sign in to comment.