-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ESORT/ESEARCH support. #382
Open
jap
wants to merge
4
commits into
mjs:master
Choose a base branch
from
startmail:esearch-esort
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,6 +152,69 @@ def parse_fetch_response(text, normalise_times=True, uid_is_key=True): | |
return parsed_response | ||
|
||
|
||
def parse_esearch_response(data): | ||
"""Parses the IMAP ESEARCH responses as returned by imaplib. | ||
|
||
These are generated by ESORT and ESEARCH queries. This function will return | ||
a dictionary, with the keys matching the *returns* value. | ||
|
||
See :py:meth:`ImapClient.esearch` for more info. | ||
""" | ||
retval = {} | ||
it = iter(parse_response(data)) | ||
try: | ||
while 1: | ||
bite = six.next(it) | ||
if isinstance(bite, tuple) and len(bite) == 2 and bite[0] == b'TAG': | ||
# FIXME: should verify we only consume messages matching our tag | ||
continue | ||
elif bite == b'UID': # this is just a marker that we are using UIDs, ignore it. | ||
continue | ||
elif bite == b'ALL': | ||
message_bite = six.next(it) | ||
retval[bite + b'_RAW'] = _raw_as_bytes(message_bite) | ||
retval[bite] = _parse_compact_message_list(message_bite) | ||
elif bite == b'PARTIAL': | ||
message_bite = six.next(it)[1] | ||
retval[bite + b'_RAW'] = _raw_as_bytes(message_bite) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and again |
||
retval[bite] = _parse_compact_message_list(message_bite) | ||
else: | ||
retval[bite] = six.next(it) | ||
except StopIteration: | ||
pass | ||
|
||
return retval | ||
|
||
|
||
def _raw_as_bytes(raw): | ||
if raw is None: | ||
return None | ||
elif isinstance(raw, int): | ||
return str(raw).encode('ascii') | ||
else: | ||
return raw | ||
|
||
|
||
def _parse_compact_message_list(message_bite): | ||
if message_bite is None: | ||
return [] | ||
if isinstance(message_bite, int): | ||
return [message_bite] | ||
messages = [] | ||
for message_atom in message_bite.split(b','): | ||
first_b, sep, last_b = message_atom.partition(b':') | ||
first = _int_or_error(first_b, 'invalid ID') | ||
if sep: | ||
last = _int_or_error(last_b, 'invalid ID') | ||
if last < first: # 10:12 is equivalent to 12:10 (!) | ||
first, last = last, first | ||
messages.extend(range(first, last+1)) | ||
else: | ||
messages.append(first) | ||
return messages | ||
|
||
|
||
|
||
def _int_or_error(value, error_text): | ||
try: | ||
return int(value) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the raw value being returned along with the parsed value? Is the parsed version not more useful?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be in the docstring - the raw version allows for a small optimisation, as it can be passed on to a subsequent fetch as well (removing the need to serialise again, and the raw version might contain ranges, making for a small size).
I'm not sure if it has any real life benefits, as the serialisation and transport costs will probably be dwarfed by the time it takes the server to construct and send the response, so if you feel it makes the interface too complicated, I can remove it as well. Real life data point: we skipped using the raw version in the end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about making each of these values a (to be implemented) "SequenceSet" object which implement
__iter__
to yield the expanded message ids and__str__
to provide the original raw set string?This makes the raw and parsed versions easily available without requiring separate dictionary values for the raw and parsed variants. Yielding message ids instead of generating a list helps to avoid unnecessary memory consumption in the case of large message ranges.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That sounds like a better way to do this, let me find some time to implement that. Besides
__str__
I'd prefer__bytes__
as well, except that is a python3 thing, but I can do both. I'll also just fix theimapclient.util.to_bytes
to check if__bytes__
is available and use it.