Skip to content

Commit

Permalink
Begining experimentation into a radically different API.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Toben Archer authored and Toben Archer committed Apr 2, 2018
1 parent 39bc0bf commit 78e5a5a
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 48 deletions.
25 changes: 25 additions & 0 deletions O365/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,28 @@


#To the King!

class O365 (object):
_inbox = None

def __init__(self,auth):
self.auth = auth
Connection.login(auth[0],auth[1])

def newMessage(self):
return Message(auth=self.auth)

def newEvent(self):
schedule = Schedule(self.auth)
schedule.getCalendars()
return Event(auth=self.auth,cal=schedule.calendars[0])

@property
def inbox(self):
if not self._inbox:
self._inbox = FluentInbox()
return self._inbox


def login(username,password):
return O365((username,password))
54 changes: 32 additions & 22 deletions O365/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ def __init__(self,json=None,auth=None,cal=None,verify=True):

self.verify = verify


def create(self,calendar=None):
self.save(calendar)

def save(self,calendar=None):
'''
this method creates an event on the calender passed.
Expand Down Expand Up @@ -201,31 +203,23 @@ def fullcalendarioJson(self):
ret['IsAllDay'] = self.json['IsAllDay']
return ret

def getSubject(self):
@property
def subject(self):
'''Gets event subject line.'''
return self.json['Subject']

def getBody(self):
'''Gets event body content.'''
return self.json['Body']['Content']

def getStart(self):
'''Gets event start struct_time'''
return time.strptime(self.json['Start'], self.time_string)

def getEnd(self):
'''Gets event end struct_time'''
return time.strptime(self.json['End'], self.time_string)

def getAttendees(self):
'''Gets list of event attendees.'''
return self.json['Attendees']

def setSubject(self,val):
@subject.setter
def subject(self,val):
'''sets event subject line.'''
self.json['Subject'] = val

def setBody(self,val,contentType='Text'):
@property
def body(self):
'''Gets event body content.'''
return self.json['Body']['Content']

@body.setter
def body(self,val,contentType='Text'):
'''
sets event body content:
Examples for ContentType could be 'Text' or 'HTML'
Expand All @@ -240,7 +234,13 @@ def setBody(self,val,contentType='Text'):
except:
self.json['Body'] = {}

def setStart(self,val):
@property
def start(self):
'''Gets event start struct_time'''
return time.strptime(self.json['Start'], self.time_string)

@start.setter
def start(self,val):
'''
sets event start time.
Expand All @@ -262,7 +262,13 @@ def setStart(self,val):
#your time string!
self.json['Start'] = val

def setEnd(self,val):
@property
def end(self):
'''Gets event end struct_time'''
return time.strptime(self.json['End'], self.time_string)

@end.setter
def end(self,val):
'''
sets event end time.
Expand Down Expand Up @@ -324,6 +330,10 @@ def setEndTimeZone(self,val):
'''sets event end timezone'''
self.json['EndTimeZone'] = val

def getAttendees(self):
'''Gets list of event attendees.'''
return self.json['Attendees']

def addAttendee(self,address,name=None):
'''
Adds a recipient to the attendee list.
Expand Down
19 changes: 19 additions & 0 deletions O365/fluent_inbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ def fetch_next(self, count=1):

return messages

def __getitem__(self,key):
if isinstance(key,int):
self.fetched_count = key
return self.fetch_next()[0]

if key.step:
messages = []
total = key.stop - key.start
for i in range(total/key.step):
self.fetched_count = key.start + key.step*i
messages += self.fetch_next()
return messages

if key.start == None:
return self.fetch_next(key.stop)

self.fetched_count = key.start
return self.fetch_next(key.stop-key.start)

@staticmethod
def _get_url(key):
""" Fetches the url for specified key as per the connection version configured
Expand Down
12 changes: 7 additions & 5 deletions O365/fluent_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,16 @@ def getSenderName(self):
except:
return ''

def getSubject(self):
@property
def subject(self):
'''get email subject line.'''
return self.json['Subject']

@subject.setter
def subject(self, val):
'''Sets the subect line of the email.'''
self.json['Subject'] = val

def getBody(self):
'''get email body.'''
try:
Expand Down Expand Up @@ -244,10 +250,6 @@ def addRecipient(self, address, name=None, r_type="To"):
self.json[r_type + 'Recipients'].append(
{'EmailAddress': {'Address': address, 'Name': name}})

def setSubject(self, val):
'''Sets the subect line of the email.'''
self.json['Subject'] = val

def setBody(self, val):
'''Sets the body content of the email.'''
cont = False
Expand Down
49 changes: 29 additions & 20 deletions O365/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def fetchAttachments(self):

return len(self.attachments)

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

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

def getSubject(self):
@property
def subject(self):
'''get email subject line.'''
return self.json['Subject']

def getBody(self):
@subject.setter
def subject(self,val):
'''Sets the subect line of the email.'''
self.json['Subject'] = val

@property
def body(self):
'''get email body.'''
return self.json['Body']['Content']

def setRecipients(self, val, r_type="To"):
@body.setter
def body(self, val):
'''Sets the body content of the email.'''
cont = False

while not cont:
try:
self.json['Body']['Content'] = val
self.json['Body']['ContentType'] = 'Text'
cont = True
except:
self.json['Body'] = {}

@property
def recipient(self, r_type='To'):
return self.json[r_type + 'Recipients']

@recipient.setter
def recipient(self, val, r_type='To'):
'''
set the recipient list.
Expand Down Expand Up @@ -220,22 +245,6 @@ def addRecipient(self, address, name=None, r_type="To"):
self.json[r_type + 'Recipients'].append(
{'EmailAddress': {'Address': address, 'Name': name}})

def setSubject(self, val):
'''Sets the subect line of the email.'''
self.json['Subject'] = val

def setBody(self, val):
'''Sets the body content of the email.'''
cont = False

while not cont:
try:
self.json['Body']['Content'] = val
self.json['Body']['ContentType'] = 'Text'
cont = True
except:
self.json['Body'] = {}

def setBodyHTML(self, val=None):
'''
Sets the body content type to HTML for your pretty emails.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ for veh in vj:

Events can be made relatively easily too. You just have to create a event class:
```python
e = Event(authentication,parentCalendar)
e = Event(auth=authentication,cal=parentCalendar)
```
and give it a few nesessary details:
```python
Expand Down
35 changes: 35 additions & 0 deletions pythonic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import O365
import time

username = 'your username'
password = 'your password'

#send a message
con = O365.login(username,password)
message = con.newMessage()
message.recipient = username
message.subject = 'I made an email script.'
message.body = 'Talk to the computer, cause the human does not want to hear it any more.'
#message.send()

#create a new event
event = con.newEvent()
event.subject = 'Coffee!'
event.start = time.gmtime(time.time()+3600) #start an hour from now.
event.end = time.gmtime(time.time()+7200) #end two hours from now.
#event.save()

print('print subject lines of first 10 messages:')
for message in con.inbox[0:10]:
print('\t'+message.subject)

print('print the subject line of the first 10 even messages:')
for message in con.inbox[1:20:2]:
print('\t'+message.subject)

print('print the subject line of the 7th message:')
print('\t'+con.inbox[6].subject)

print('printing the subject line of the first 5 messages:')
for message in con.inbox[:5]:
print('\t'+message.subject)

0 comments on commit 78e5a5a

Please sign in to comment.