-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from zevaverbach/vendors
fix substring search of subjects in Gmail, add support for some Gmail extended IMAP
- Loading branch information
Showing
11 changed files
with
118 additions
and
93 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,6 @@ example.py | |
|
||
# PyCharm | ||
.idea/ | ||
|
||
# Mac | ||
.DS_Store |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"python.pythonPath": "/usr/local/bin/python3" | ||
} |
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
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
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 |
---|---|---|
@@ -1,65 +1,17 @@ | ||
import datetime | ||
import logging | ||
# TODO - Validate query arguments | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def format_date(date): | ||
if isinstance(date, datetime.date): | ||
return date.strftime('%d-%b-%Y') | ||
return date | ||
|
||
|
||
def build_search_query(**kwargs): | ||
|
||
# Parse keyword arguments | ||
unread = kwargs.get('unread', False) | ||
unflagged = kwargs.get('unflagged', False) | ||
flagged = kwargs.get('flagged', False) | ||
sent_from = kwargs.get('sent_from', False) | ||
sent_to = kwargs.get('sent_to', False) | ||
date__gt = kwargs.get('date__gt', False) | ||
date__lt = kwargs.get('date__lt', False) | ||
date__on = kwargs.get('date__on', False) | ||
subject = kwargs.get('subject') | ||
uid__range = kwargs.get('uid__range') | ||
|
||
def build_search_query(imap_attribute_lookup, **kwargs): | ||
query = [] | ||
|
||
if unread: | ||
query.append("(UNSEEN)") | ||
|
||
if unflagged: | ||
query.append("(UNFLAGGED)") | ||
|
||
if flagged: | ||
query.append("(FLAGGED)") | ||
|
||
if sent_from: | ||
query.append('(FROM "%s")' % sent_from) | ||
|
||
if sent_to: | ||
query.append('(TO "%s")' % sent_to) | ||
|
||
if date__gt: | ||
query.append('(SINCE "%s")' % format_date(date__gt)) | ||
|
||
if date__lt: | ||
query.append('(BEFORE "%s")' % format_date(date__lt)) | ||
|
||
if date__on: | ||
query.append('(ON "%s")' % format_date(date__on)) | ||
|
||
if subject is not None: | ||
query.append('(SUBJECT "%s")' % subject) | ||
|
||
if uid__range: | ||
query.append('(UID %s)' % uid__range) | ||
for name, value in kwargs.items(): | ||
if value is not None: | ||
if isinstance(value, datetime.date): | ||
value = value.strftime('%d-%b-%Y') | ||
if isinstance(value, str) and '"' in value: | ||
value = value.replace('"', "'") | ||
query.append(imap_attribute_lookup[name].format(value)) | ||
|
||
if query: | ||
logger.debug("IMAP query: {}".format(" ".join(query))) | ||
return " ".join(query) | ||
|
||
logger.debug("IMAP query: {}".format("(ALL)")) | ||
return "(ALL)" |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
def merge_two_dicts(x, y): | ||
"""from https://stackoverflow.com/a/26853961/4386191""" | ||
z = x.copy() # start with x's keys and values | ||
z.update(y) # modifies z with y's keys and values & returns None | ||
return z |
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 |
---|---|---|
@@ -1,54 +1,83 @@ | ||
from datetime import date | ||
import unittest | ||
|
||
from imbox.query import build_search_query | ||
from datetime import date | ||
from imbox.messages import Messages | ||
from imbox.vendors.helpers import merge_two_dicts | ||
from imbox.vendors.gmail import GmailMessages | ||
|
||
IMAP_ATTRIBUTE_LOOKUP = Messages.IMAP_ATTRIBUTE_LOOKUP | ||
GMAIL_ATTRIBUTE_LOOKUP = merge_two_dicts(IMAP_ATTRIBUTE_LOOKUP, | ||
GmailMessages.GMAIL_IMAP_ATTRIBUTE_LOOKUP_DIFF) | ||
|
||
|
||
class TestQuery(unittest.TestCase): | ||
|
||
def test_all(self): | ||
|
||
res = build_search_query() | ||
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP) | ||
self.assertEqual(res, "(ALL)") | ||
|
||
def test_subject(self): | ||
|
||
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, subject='hi') | ||
self.assertEqual(res, '(SUBJECT "hi")') | ||
|
||
res = build_search_query(GMAIL_ATTRIBUTE_LOOKUP, subject='hi') | ||
self.assertEqual(res, '(X-GM-RAW "subject:\'hi\'")') | ||
|
||
def test_unread(self): | ||
|
||
res = build_search_query(unread=True) | ||
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, unread=True) | ||
self.assertEqual(res, "(UNSEEN)") | ||
|
||
def test_unflagged(self): | ||
|
||
res = build_search_query(unflagged=True) | ||
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, unflagged=True) | ||
self.assertEqual(res, "(UNFLAGGED)") | ||
|
||
def test_flagged(self): | ||
|
||
res = build_search_query(flagged=True) | ||
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, flagged=True) | ||
self.assertEqual(res, "(FLAGGED)") | ||
|
||
def test_sent_from(self): | ||
|
||
res = build_search_query(sent_from='test@example.com') | ||
res = build_search_query( | ||
IMAP_ATTRIBUTE_LOOKUP, sent_from='test@example.com') | ||
self.assertEqual(res, '(FROM "test@example.com")') | ||
|
||
def test_sent_to(self): | ||
|
||
res = build_search_query(sent_to='test@example.com') | ||
res = build_search_query( | ||
IMAP_ATTRIBUTE_LOOKUP, sent_to='test@example.com') | ||
self.assertEqual(res, '(TO "test@example.com")') | ||
|
||
def test_date__gt(self): | ||
|
||
res = build_search_query(date__gt=date(2014, 12, 31)) | ||
res = build_search_query( | ||
IMAP_ATTRIBUTE_LOOKUP, date__gt=date(2014, 12, 31)) | ||
self.assertEqual(res, '(SINCE "31-Dec-2014")') | ||
|
||
def test_date__lt(self): | ||
|
||
res = build_search_query(date__lt=date(2014, 1, 1)) | ||
res = build_search_query( | ||
IMAP_ATTRIBUTE_LOOKUP, date__lt=date(2014, 1, 1)) | ||
self.assertEqual(res, '(BEFORE "01-Jan-2014")') | ||
|
||
def test_date__on(self): | ||
res = build_search_query(date__on=date(2014, 1, 1)) | ||
res = build_search_query( | ||
IMAP_ATTRIBUTE_LOOKUP, date__on=date(2014, 1, 1)) | ||
self.assertEqual(res, '(ON "01-Jan-2014")') | ||
|
||
def test_uid__range(self): | ||
res = build_search_query(uid__range='1000:*') | ||
res = build_search_query(IMAP_ATTRIBUTE_LOOKUP, uid__range='1000:*') | ||
self.assertEqual(res, '(UID 1000:*)') | ||
|
||
def test_gmail_raw(self): | ||
res = build_search_query(GMAIL_ATTRIBUTE_LOOKUP, raw='has:attachment subject:"hey"') | ||
self.assertEqual(res, '(X-GM-RAW "has:attachment subject:\'hey\'")') | ||
|
||
def test_gmail_label(self): | ||
res = build_search_query(GMAIL_ATTRIBUTE_LOOKUP, label='finance') | ||
self.assertEqual(res, '(X-GM-LABELS "finance")') |