Skip to content

Commit 78e5a5a

Browse files
Toben ArcherToben Archer
authored andcommitted
Begining experimentation into a radically different API.
I mused on github to @roycem90 about my idea of making O365 more pythonic. I finally had a chance when I wasn't doing a lot else to fiddle around with it and see how it would pan out. The results I have managed so far are quite satifying. This library could be made a lot more pythonic in a lot of ways. In terms of the code itself I want to leverage properties more and remove the need for methods like "getRecipient" and "setRecipient" and instead just use the property "recipient". Apply that to all of the objects and their data members and it should result in a pretty clear and consise API. The other way I want to make this a batteries included kind of library is inspired largely by roycem90's fluent inbox and message. Also inspiration for libraries like pandas. I've setup in this commit fluent_inbox with a __getitem__ method that allows for accessing the messages in the inbox super easily. I'm going to talk with roycem and @janscas about this and see if I can get their input on how to move forward.
1 parent 39bc0bf commit 78e5a5a

File tree

7 files changed

+148
-48
lines changed

7 files changed

+148
-48
lines changed

O365/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,28 @@
3131

3232

3333
#To the King!
34+
35+
class O365 (object):
36+
_inbox = None
37+
38+
def __init__(self,auth):
39+
self.auth = auth
40+
Connection.login(auth[0],auth[1])
41+
42+
def newMessage(self):
43+
return Message(auth=self.auth)
44+
45+
def newEvent(self):
46+
schedule = Schedule(self.auth)
47+
schedule.getCalendars()
48+
return Event(auth=self.auth,cal=schedule.calendars[0])
49+
50+
@property
51+
def inbox(self):
52+
if not self._inbox:
53+
self._inbox = FluentInbox()
54+
return self._inbox
55+
56+
57+
def login(username,password):
58+
return O365((username,password))

O365/event.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,10 @@ def __init__(self,json=None,auth=None,cal=None,verify=True):
7070

7171
self.verify = verify
7272

73-
7473
def create(self,calendar=None):
74+
self.save(calendar)
75+
76+
def save(self,calendar=None):
7577
'''
7678
this method creates an event on the calender passed.
7779
@@ -201,31 +203,23 @@ def fullcalendarioJson(self):
201203
ret['IsAllDay'] = self.json['IsAllDay']
202204
return ret
203205

204-
def getSubject(self):
206+
@property
207+
def subject(self):
205208
'''Gets event subject line.'''
206209
return self.json['Subject']
207210

208-
def getBody(self):
209-
'''Gets event body content.'''
210-
return self.json['Body']['Content']
211-
212-
def getStart(self):
213-
'''Gets event start struct_time'''
214-
return time.strptime(self.json['Start'], self.time_string)
215-
216-
def getEnd(self):
217-
'''Gets event end struct_time'''
218-
return time.strptime(self.json['End'], self.time_string)
219-
220-
def getAttendees(self):
221-
'''Gets list of event attendees.'''
222-
return self.json['Attendees']
223-
224-
def setSubject(self,val):
211+
@subject.setter
212+
def subject(self,val):
225213
'''sets event subject line.'''
226214
self.json['Subject'] = val
227215

228-
def setBody(self,val,contentType='Text'):
216+
@property
217+
def body(self):
218+
'''Gets event body content.'''
219+
return self.json['Body']['Content']
220+
221+
@body.setter
222+
def body(self,val,contentType='Text'):
229223
'''
230224
sets event body content:
231225
Examples for ContentType could be 'Text' or 'HTML'
@@ -240,7 +234,13 @@ def setBody(self,val,contentType='Text'):
240234
except:
241235
self.json['Body'] = {}
242236

243-
def setStart(self,val):
237+
@property
238+
def start(self):
239+
'''Gets event start struct_time'''
240+
return time.strptime(self.json['Start'], self.time_string)
241+
242+
@start.setter
243+
def start(self,val):
244244
'''
245245
sets event start time.
246246
@@ -262,7 +262,13 @@ def setStart(self,val):
262262
#your time string!
263263
self.json['Start'] = val
264264

265-
def setEnd(self,val):
265+
@property
266+
def end(self):
267+
'''Gets event end struct_time'''
268+
return time.strptime(self.json['End'], self.time_string)
269+
270+
@end.setter
271+
def end(self,val):
266272
'''
267273
sets event end time.
268274
@@ -324,6 +330,10 @@ def setEndTimeZone(self,val):
324330
'''sets event end timezone'''
325331
self.json['EndTimeZone'] = val
326332

333+
def getAttendees(self):
334+
'''Gets list of event attendees.'''
335+
return self.json['Attendees']
336+
327337
def addAttendee(self,address,name=None):
328338
'''
329339
Adds a recipient to the attendee list.

O365/fluent_inbox.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,25 @@ def fetch_next(self, count=1):
205205

206206
return messages
207207

208+
def __getitem__(self,key):
209+
if isinstance(key,int):
210+
self.fetched_count = key
211+
return self.fetch_next()[0]
212+
213+
if key.step:
214+
messages = []
215+
total = key.stop - key.start
216+
for i in range(total/key.step):
217+
self.fetched_count = key.start + key.step*i
218+
messages += self.fetch_next()
219+
return messages
220+
221+
if key.start == None:
222+
return self.fetch_next(key.stop)
223+
224+
self.fetched_count = key.start
225+
return self.fetch_next(key.stop-key.start)
226+
208227
@staticmethod
209228
def _get_url(key):
210229
""" Fetches the url for specified key as per the connection version configured

O365/fluent_message.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,16 @@ def getSenderName(self):
163163
except:
164164
return ''
165165

166-
def getSubject(self):
166+
@property
167+
def subject(self):
167168
'''get email subject line.'''
168169
return self.json['Subject']
169170

171+
@subject.setter
172+
def subject(self, val):
173+
'''Sets the subect line of the email.'''
174+
self.json['Subject'] = val
175+
170176
def getBody(self):
171177
'''get email body.'''
172178
try:
@@ -244,10 +250,6 @@ def addRecipient(self, address, name=None, r_type="To"):
244250
self.json[r_type + 'Recipients'].append(
245251
{'EmailAddress': {'Address': address, 'Name': name}})
246252

247-
def setSubject(self, val):
248-
'''Sets the subect line of the email.'''
249-
self.json['Subject'] = val
250-
251253
def setBody(self, val):
252254
'''Sets the body content of the email.'''
253255
cont = False

O365/message.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def fetchAttachments(self):
8888

8989
return len(self.attachments)
9090

91-
def sendMessage(self):
91+
def send(self):
9292
'''takes local variabls and forms them into a message to be sent.'''
9393

9494
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
@@ -143,15 +143,40 @@ def getSenderName(self):
143143
except:
144144
return ''
145145

146-
def getSubject(self):
146+
@property
147+
def subject(self):
147148
'''get email subject line.'''
148149
return self.json['Subject']
149150

150-
def getBody(self):
151+
@subject.setter
152+
def subject(self,val):
153+
'''Sets the subect line of the email.'''
154+
self.json['Subject'] = val
155+
156+
@property
157+
def body(self):
151158
'''get email body.'''
152159
return self.json['Body']['Content']
153160

154-
def setRecipients(self, val, r_type="To"):
161+
@body.setter
162+
def body(self, val):
163+
'''Sets the body content of the email.'''
164+
cont = False
165+
166+
while not cont:
167+
try:
168+
self.json['Body']['Content'] = val
169+
self.json['Body']['ContentType'] = 'Text'
170+
cont = True
171+
except:
172+
self.json['Body'] = {}
173+
174+
@property
175+
def recipient(self, r_type='To'):
176+
return self.json[r_type + 'Recipients']
177+
178+
@recipient.setter
179+
def recipient(self, val, r_type='To'):
155180
'''
156181
set the recipient list.
157182
@@ -220,22 +245,6 @@ def addRecipient(self, address, name=None, r_type="To"):
220245
self.json[r_type + 'Recipients'].append(
221246
{'EmailAddress': {'Address': address, 'Name': name}})
222247

223-
def setSubject(self, val):
224-
'''Sets the subect line of the email.'''
225-
self.json['Subject'] = val
226-
227-
def setBody(self, val):
228-
'''Sets the body content of the email.'''
229-
cont = False
230-
231-
while not cont:
232-
try:
233-
self.json['Body']['Content'] = val
234-
self.json['Body']['ContentType'] = 'Text'
235-
cont = True
236-
except:
237-
self.json['Body'] = {}
238-
239248
def setBodyHTML(self, val=None):
240249
'''
241250
Sets the body content type to HTML for your pretty emails.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ for veh in vj:
120120

121121
Events can be made relatively easily too. You just have to create a event class:
122122
```python
123-
e = Event(authentication,parentCalendar)
123+
e = Event(auth=authentication,cal=parentCalendar)
124124
```
125125
and give it a few nesessary details:
126126
```python

pythonic.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import O365
2+
import time
3+
4+
username = 'your username'
5+
password = 'your password'
6+
7+
#send a message
8+
con = O365.login(username,password)
9+
message = con.newMessage()
10+
message.recipient = username
11+
message.subject = 'I made an email script.'
12+
message.body = 'Talk to the computer, cause the human does not want to hear it any more.'
13+
#message.send()
14+
15+
#create a new event
16+
event = con.newEvent()
17+
event.subject = 'Coffee!'
18+
event.start = time.gmtime(time.time()+3600) #start an hour from now.
19+
event.end = time.gmtime(time.time()+7200) #end two hours from now.
20+
#event.save()
21+
22+
print('print subject lines of first 10 messages:')
23+
for message in con.inbox[0:10]:
24+
print('\t'+message.subject)
25+
26+
print('print the subject line of the first 10 even messages:')
27+
for message in con.inbox[1:20:2]:
28+
print('\t'+message.subject)
29+
30+
print('print the subject line of the 7th message:')
31+
print('\t'+con.inbox[6].subject)
32+
33+
print('printing the subject line of the first 5 messages:')
34+
for message in con.inbox[:5]:
35+
print('\t'+message.subject)

0 commit comments

Comments
 (0)