From 6a1a88cfdf9804f87c714eb9e4410249891dcb6d Mon Sep 17 00:00:00 2001 From: Nathan Thorpe Date: Thu, 21 Apr 2022 16:46:01 -0700 Subject: [PATCH] gmail pep8 fixes --- README.rst | 6 ++---- parsedmarc/mail/__init__.py | 1 + parsedmarc/mail/gmail.py | 40 ++++++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/README.rst b/README.rst index 7a9cbd3..52a0e56 100644 --- a/README.rst +++ b/README.rst @@ -199,8 +199,8 @@ The full set of configuration options are: Setting this to a number larger than one can improve performance when processing thousands of files - ``mailbox`` - - ``reports_folder`` - str: The mailbox folder where the incoming reports can be found (Default: INBOX) - - ``archive_folder`` - str: The mailbox folder to sort processed emails into (Default: Archive) + - ``reports_folder`` - str: The mailbox folder (or label for Gmail) where the incoming reports can be found (Default: INBOX) + - ``archive_folder`` - str: The mailbox folder (or label for Gmail) to sort processed emails into (Default: Archive) - ``watch`` - bool: Use the IMAP ``IDLE`` command to process messages as they arrive or poll MS Graph for new messages - ``delete`` - bool: Delete messages after processing them, instead of archiving them - ``test`` - bool: Do not move or delete messages @@ -278,8 +278,6 @@ The full set of configuration options are: - ``gmail_api_archive_file`` - str: Label to apply to processed reports (Default: DMARC Archive) - ``gmail_api_include_spam_trash`` - bool: Include messages in Spam and Trash when searching reports (Default: False) - ``gmail_api_scopes`` - str: Comma separated list of scopes to use when acquiring credentials (Default: https://www.googleapis.com/auth/gmail.modify) - - ``gmail_api_delete`` - bool: Delete messages after processing them, instead of archiving them (Default: False) - - ``gmail_api_test`` - bool: Do not move or delete messages (Default: False) .. warning:: diff --git a/parsedmarc/mail/__init__.py b/parsedmarc/mail/__init__.py index 87ef360..c376024 100644 --- a/parsedmarc/mail/__init__.py +++ b/parsedmarc/mail/__init__.py @@ -1,3 +1,4 @@ from parsedmarc.mail.mailbox_connection import MailboxConnection from parsedmarc.mail.graph import MSGraphConnection +from parsedmarc.mail.gmail import GmailConnection from parsedmarc.mail.imap import IMAPConnection diff --git a/parsedmarc/mail/gmail.py b/parsedmarc/mail/gmail.py index 0c84733..51491d5 100644 --- a/parsedmarc/mail/gmail.py +++ b/parsedmarc/mail/gmail.py @@ -24,7 +24,8 @@ def _get_creds(token_file, credentials_file, scopes): if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: - flow = InstalledAppFlow.from_client_secrets_file(credentials_file, scopes) + flow = InstalledAppFlow.from_client_secrets_file( + credentials_file, scopes) creds = flow.run_console() # Save the credentials for the next run with open(token_file, 'w') as token: @@ -33,7 +34,11 @@ def _get_creds(token_file, credentials_file, scopes): class GmailConnection(MailboxConnection): - def __init__(self, token_file, credentials_file, scopes, include_spam_trash): + def __init__(self, + token_file: str, + credentials_file: str, + scopes: List[str], + include_spam_trash: bool): creds = _get_creds(token_file, credentials_file, scopes) self.service = build('gmail', 'v1', credentials=creds) self.include_spam_trash = include_spam_trash @@ -41,19 +46,27 @@ class GmailConnection(MailboxConnection): def create_folder(self, folder_name: str): logger.debug("Creating label {0}".format(folder_name)) request_body = {'name': folder_name, 'messageListVisibility': 'show'} - label = self.service.users().labels().create(userId='me', body=request_body).execute() + self.service.users().labels()\ + .create(userId='me', body=request_body).execute() def fetch_messages(self, reports_folder: str) -> List[str]: reports_label_id = self._find_label_id_for_label(reports_folder) - results = self.service.users().messages().list(userId='me', - includeSpamTrash=self.include_spam_trash, - labelIds=[reports_label_id] - ).execute() + results = self.service.users().messages()\ + .list(userId='me', + includeSpamTrash=self.include_spam_trash, + labelIds=[reports_label_id] + )\ + .execute() messages = results.get('messages', []) return [message['id'] for message in messages] def fetch_message(self, message_id): - msg = self.service.users().messages().get(userId='me', id=message_id, format="raw").execute() + msg = self.service.users().messages()\ + .get(userId='me', + id=message_id, + format="raw" + )\ + .execute() return urlsafe_b64decode(msg['raw']) def delete_message(self, message_id: str): @@ -61,10 +74,13 @@ class GmailConnection(MailboxConnection): def move_message(self, message_id: str, folder_name: str): label_id = self._find_label_id_for_label(folder_name) - logger.debug("Moving message UID {0} to {1}".format(message_id, folder_name)) - request_body = {'addLabelIds': [label_id], "removeLabelIds": [folder_name]} - self.service.users().messages().modify(userId='me', id=message_id, - body=request_body).execute() + logger.debug(f"Moving message UID {message_id} to {folder_name}") + request_body = {'addLabelIds': [label_id]} + self.service.users().messages()\ + .modify(userId='me', + id=message_id, + body=request_body)\ + .execute() def keepalive(self): # Not needed