Example #1
0
    def test_construct_query_from_keywords(self):
        expect = "({from:[email protected] from:[email protected]} subject:meeting)"
        query_string = query.construct_query(
            sender=['*****@*****.**', '*****@*****.**'], subject='meeting'
        )
        assert query_string == expect

        expect = "(-is:starred (label:work label:HR))"
        query_string = query.construct_query(exclude_starred=True, 
                                             labels=['work', 'HR'])
        assert query_string == expect

        expect = "{(label:work label:HR) (label:wife label:house)}"
        query_string = query.construct_query(
            labels=[['work', 'HR'], ['wife', 'house']]
        )
        assert query_string == expect
Example #2
0
    def test_construct_query_from_dicts(self):
        expect = "{(from:[email protected] newer_than:1d {subject:meeting subject:HR}) (to:[email protected] CS AROUND 5 homework)}"

        query_string = query.construct_query(
            {
                'sender': '*****@*****.**',
                'newer_than': (1, 'day'),
                'subject': ['meeting', 'HR']
            }, {
                'recipient': '*****@*****.**',
                'near_words': ('CS', 'homework', 5)
            })

        assert query_string == expect
Example #3
0
 def test_construct_query_from_dicts(self):
     expect = "{(from:[email protected] newer_than:1d {subject:meeting subject:HR}) (to:[email protected] CS AROUND 5 homework)}"
     query_string = query.construct_query(
         dict(
             sender='*****@*****.**',
             newer_than=(1, 'day'),
             subject=['meeting', 'HR']
         ),
         dict(
             recipient='*****@*****.**',
             near_words=('CS', 'homework', 5)
         )
     )
     assert query_string == expect
Example #4
0
    def __init__(self, gmail, name: str, base_path: str = BASE_PATH, **kwrgs):
        self.name = name
        self.base_path = base_path

        # For all keywords whose values are not booleans, you can indicate you'd
        # like to "and" multiple values by placing them in a tuple (), or "or"
        # multiple values by placing them in a list [].
        query_params = {}
        for param in kwrgs.pop("query_params"):
            if "and" in param:
                param = list(param.values())[0]
                poped = param.popitem()
                query_params.setdefault(poped[0], tuple(poped[1]))
            elif "or" in param:
                param = list(param.values())[0]
                poped = param.popitem()
                query_params.setdefault(poped[0], poped[1])

            else:
                # remove a key value pair as 2-tuple and add to query_params:
                poped = param.popitem()
                query_params.setdefault(poped[0], poped[1])

        self.query_params = query_params
        # get the query params from kwrgs dictionary:
        # self.query_params = kwrgs.pop("query_params")
        self.messages = gmail.get_messages(query=construct_query(self.query_params))
        print(
            f"the query for {self.name} has resulted in {len(self.messages)} messages"
        )

        try:
            actions = kwrgs.pop("scenario")
        except:
            pass

        func_dic = {
            "download attachment": getattr(self, "download_attachments"),
            "mark as important": getattr(self, "mark_as_important"),
            "mark as not important": getattr(self, "mark_as_not_important"),
            "star messages": getattr(self, "star"),
            "unstar messages": getattr(self, "unstar"),
            "send to trash": getattr(self, "trash"),
            "remove from trash": getattr(self, "untrash"),
            "do nothing": getattr(self, "do_nothing"),
        }
        for action in actions:
            func_dic[action]()
def fetch_messages(gmail, query):
    return gmail.get_messages(query=construct_query(query))