Skip to content

Commit 7a93e8c

Browse files
committed
Delint imported gmail snippets
1 parent 7bd49c2 commit 7a93e8c

9 files changed

Lines changed: 669 additions & 654 deletions

gmail/snippet/base_test.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
import unittest
3+
4+
from apiclient import discovery
35
from oauth2client.service_account import ServiceAccountCredentials
4-
import apiclient
6+
57

68
class BaseTest(unittest.TestCase):
79

@@ -27,9 +29,8 @@ def create_credentials(cls):
2729
@classmethod
2830
def create_service(cls):
2931
credentials = cls.create_credentials()
30-
with open('rest.json', 'r') as document:
31-
return discovery.build_from_document(document.read(),
32-
credentials=credentials)
32+
return discovery.build('gmail', 'v1', credentials=credentials)
33+
3334

3435
if __name__ == '__main__':
3536
unittest.main()

gmail/snippet/send_mail.py

Lines changed: 111 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,135 +2,141 @@
22
"""
33

44
import base64
5+
import mimetypes
6+
import os
57
from email.mime.audio import MIMEAudio
68
from email.mime.base import MIMEBase
79
from email.mime.image import MIMEImage
810
from email.mime.multipart import MIMEMultipart
911
from email.mime.text import MIMEText
10-
import mimetypes
11-
import os
1212

1313
from apiclient import errors
1414

1515

1616
# [START create_draft]
1717
def create_draft(service, user_id, message_body):
18-
"""Create and insert a draft email. Print the returned draft's message and id.
19-
20-
Args:
21-
service: Authorized Gmail API service instance.
22-
user_id: User's email address. The special value "me"
23-
can be used to indicate the authenticated user.
24-
message_body: The body of the email message, including headers.
25-
26-
Returns:
27-
Draft object, including draft id and message meta data.
28-
"""
29-
try:
30-
message = {'message': message_body}
31-
draft = service.users().drafts().create(userId=user_id, body=message).execute()
32-
33-
print('Draft id: %s\nDraft message: %s' % (draft['id'], draft['message']))
34-
35-
return draft
36-
except errors.HttpError as error:
37-
print('An error occurred: %s' % error)
38-
return None
18+
"""Create and insert a draft email. Print the returned draft's message and id.
19+
20+
Args:
21+
service: Authorized Gmail API service instance.
22+
user_id: User's email address. The special value "me"
23+
can be used to indicate the authenticated user.
24+
message_body: The body of the email message, including headers.
25+
26+
Returns:
27+
Draft object, including draft id and message meta data.
28+
"""
29+
try:
30+
message = {'message': message_body}
31+
draft = service.users().drafts().create(userId=user_id, body=message).execute()
32+
33+
print('Draft id: %s\nDraft message: %s' % (draft['id'], draft['message']))
34+
35+
return draft
36+
except errors.HttpError as error:
37+
print('An error occurred: %s' % error)
38+
return None
39+
40+
3941
# [END create_draft]
4042

4143

4244
# [START send_email]
4345
def send_message(service, user_id, message):
44-
"""Send an email message.
45-
46-
Args:
47-
service: Authorized Gmail API service instance.
48-
user_id: User's email address. The special value "me"
49-
can be used to indicate the authenticated user.
50-
message: Message to be sent.
51-
52-
Returns:
53-
Sent Message.
54-
"""
55-
try:
56-
message = (service.users().messages().send(userId=user_id, body=message)
57-
.execute())
58-
print('Message Id: %s' % message['id'])
59-
return message
60-
except errors.HttpError as error:
61-
print('An error occurred: %s' % error)
46+
"""Send an email message.
47+
48+
Args:
49+
service: Authorized Gmail API service instance.
50+
user_id: User's email address. The special value "me"
51+
can be used to indicate the authenticated user.
52+
message: Message to be sent.
53+
54+
Returns:
55+
Sent Message.
56+
"""
57+
try:
58+
message = (service.users().messages().send(userId=user_id, body=message)
59+
.execute())
60+
print('Message Id: %s' % message['id'])
61+
return message
62+
except errors.HttpError as error:
63+
print('An error occurred: %s' % error)
64+
65+
6266
# [END send_email]
6367

6468

6569
# [START create_message]
6670
def create_message(sender, to, subject, message_text):
67-
"""Create a message for an email.
68-
69-
Args:
70-
sender: Email address of the sender.
71-
to: Email address of the receiver.
72-
subject: The subject of the email message.
73-
message_text: The text of the email message.
74-
75-
Returns:
76-
An object containing a base64url encoded email object.
77-
"""
78-
message = MIMEText(message_text)
79-
message['to'] = to
80-
message['from'] = sender
81-
message['subject'] = subject
82-
return {'raw': base64.urlsafe_b64encode(message.as_string())}
71+
"""Create a message for an email.
72+
73+
Args:
74+
sender: Email address of the sender.
75+
to: Email address of the receiver.
76+
subject: The subject of the email message.
77+
message_text: The text of the email message.
78+
79+
Returns:
80+
An object containing a base64url encoded email object.
81+
"""
82+
message = MIMEText(message_text)
83+
message['to'] = to
84+
message['from'] = sender
85+
message['subject'] = subject
86+
return {'raw': base64.urlsafe_b64encode(message.as_string())}
87+
88+
8389
# [END create_message]
8490

8591

8692
# [START create_message_attachment]
8793
def create_message_with_attachment(
88-
sender, to, subject, message_text, file):
89-
"""Create a message for an email.
90-
91-
Args:
92-
sender: Email address of the sender.
93-
to: Email address of the receiver.
94-
subject: The subject of the email message.
95-
message_text: The text of the email message.
96-
file: The path to the file to be attached.
97-
98-
Returns:
99-
An object containing a base64url encoded email object.
100-
"""
101-
message = MIMEMultipart()
102-
message['to'] = to
103-
message['from'] = sender
104-
message['subject'] = subject
105-
106-
msg = MIMEText(message_text)
107-
message.attach(msg)
108-
109-
content_type, encoding = mimetypes.guess_type(file)
110-
111-
if content_type is None or encoding is not None:
112-
content_type = 'application/octet-stream'
113-
main_type, sub_type = content_type.split('/', 1)
114-
if main_type == 'text':
115-
fp = open(file, 'rb')
116-
msg = MIMEText(fp.read(), _subtype=sub_type)
117-
fp.close()
118-
elif main_type == 'image':
119-
fp = open(file, 'rb')
120-
msg = MIMEImage(fp.read(), _subtype=sub_type)
121-
fp.close()
122-
elif main_type == 'audio':
123-
fp = open(file, 'rb')
124-
msg = MIMEAudio(fp.read(), _subtype=sub_type)
125-
fp.close()
126-
else:
127-
fp = open(file, 'rb')
128-
msg = MIMEBase(main_type, sub_type)
129-
msg.set_payload(fp.read())
130-
fp.close()
131-
filename = os.path.basename(file)
132-
msg.add_header('Content-Disposition', 'attachment', filename=filename)
133-
message.attach(msg)
134-
135-
return {'raw': base64.urlsafe_b64encode(message.as_string())}
94+
sender, to, subject, message_text, file):
95+
"""Create a message for an email.
96+
97+
Args:
98+
sender: Email address of the sender.
99+
to: Email address of the receiver.
100+
subject: The subject of the email message.
101+
message_text: The text of the email message.
102+
file: The path to the file to be attached.
103+
104+
Returns:
105+
An object containing a base64url encoded email object.
106+
"""
107+
message = MIMEMultipart()
108+
message['to'] = to
109+
message['from'] = sender
110+
message['subject'] = subject
111+
112+
msg = MIMEText(message_text)
113+
message.attach(msg)
114+
115+
content_type, encoding = mimetypes.guess_type(file)
116+
117+
if content_type is None or encoding is not None:
118+
content_type = 'application/octet-stream'
119+
main_type, sub_type = content_type.split('/', 1)
120+
if main_type == 'text':
121+
fp = open(file, 'rb')
122+
msg = MIMEText(fp.read(), _subtype=sub_type)
123+
fp.close()
124+
elif main_type == 'image':
125+
fp = open(file, 'rb')
126+
msg = MIMEImage(fp.read(), _subtype=sub_type)
127+
fp.close()
128+
elif main_type == 'audio':
129+
fp = open(file, 'rb')
130+
msg = MIMEAudio(fp.read(), _subtype=sub_type)
131+
fp.close()
132+
else:
133+
fp = open(file, 'rb')
134+
msg = MIMEBase(main_type, sub_type)
135+
msg.set_payload(fp.read())
136+
fp.close()
137+
filename = os.path.basename(file)
138+
msg.add_header('Content-Disposition', 'attachment', filename=filename)
139+
message.attach(msg)
140+
141+
return {'raw': base64.urlsafe_b64encode(message.as_string())}
136142
# [END create_message_attachment]

gmail/snippet/settings_snippets.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime, timedelta
2+
23
from numpy import long
34

45

@@ -11,7 +12,7 @@ def update_signature(self):
1112
gmail_service = self.service
1213
# [START updateSignature]
1314
primary_alias = None
14-
aliases = gmail_service.users().settings().sendAs().\
15+
aliases = gmail_service.users().settings().sendAs(). \
1516
list(userId='me').execute()
1617
for alias in aliases.get('sendAs'):
1718
if alias.get('isPrimary'):
@@ -21,7 +22,7 @@ def update_signature(self):
2122
sendAsConfiguration = {
2223
'signature': 'I heart cats'
2324
}
24-
result = gmail_service.users().settings().sendAs().\
25+
result = gmail_service.users().settings().sendAs(). \
2526
patch(userId='me',
2627
sendAsEmail=primary_alias.get('sendAsEmail'),
2728
body=sendAsConfiguration).execute()
@@ -32,7 +33,7 @@ def update_signature(self):
3233
def create_filter(self, real_label_id):
3334
gmail_service = self.service
3435
# [START createFilter]
35-
label_id = 'Label_14' # ID of user label to add
36+
label_id = 'Label_14' # ID of user label to add
3637
# [START_EXCLUDE silent]
3738
label_id = real_label_id
3839
# [END_EXCLUDE]
@@ -45,7 +46,7 @@ def create_filter(self, real_label_id):
4546
'removeLabelIds': ['INBOX']
4647
}
4748
}
48-
result = gmail_service.users().settings().filters().\
49+
result = gmail_service.users().settings().filters(). \
4950
create(userId='me', body=filter).execute()
5051
print('Created filter: %s' % result.get('id'))
5152
# [END createFilter]
@@ -54,19 +55,23 @@ def create_filter(self, real_label_id):
5455
def enable_forwarding(self, real_forwarding_address):
5556
gmail_service = self.service
5657
# [START enableForwarding]
57-
address = { 'forwardingEmail': '[email protected]' }
58+
address = {
59+
'forwardingEmail': '[email protected]'
60+
}
5861
# [START_EXCLUDE silent]
59-
address = { 'forwardingEmail': real_forwarding_address }
62+
address = {
63+
'forwardingEmail': real_forwarding_address
64+
}
6065
# [END_EXCLUDE]
61-
result = gmail_service.users().settings().forwardingAddresses().\
66+
result = gmail_service.users().settings().forwardingAddresses(). \
6267
create(userId='me', body=address).execute()
6368
if result.get('verificationStatus') == 'accepted':
6469
body = {
6570
'emailAddress': result.get('forwardingEmail'),
6671
'enabled': True,
6772
'disposition': 'trash'
6873
}
69-
result = gmail_service.users().settings().\
74+
result = gmail_service.users().settings(). \
7075
updateAutoForwarding(userId='me', body=body).execute()
7176
# [START_EXCLUDE silent]
7277
return result
@@ -90,7 +95,7 @@ def enable_auto_reply(self):
9095
'startTime': long(start_time),
9196
'endTime': long(end_time)
9297
}
93-
response = gmail_service.users().settings().\
98+
response = gmail_service.users().settings(). \
9499
updateVacation(userId='me', body=vacation_settings).execute()
95100
# [END enableAutoReply]
96-
return response
101+
return response

0 commit comments

Comments
 (0)