Skip to content

Commit a3d2b03

Browse files
authored
Merge pull request O365#135 from O365/rewrite
Complete rewrite courtesy of Janscas.
2 parents a1b9f00 + 50616e1 commit a3d2b03

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+6922
-4531
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,8 @@ target/
5959
*/pid
6060

6161
.idea/
62+
6263
trail.py
6364
sample_run.py
65+
66+

.travis.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
language: python
2-
#python:
3-
# - "2.7"
4-
# - "3.4"
5-
# - "3.5"
6-
# - "3.6"
7-
#install:
8-
# - pip install -r requirements.txt
2+
python:
3+
- "3.5"
4+
- "3.6"
5+
6+
install: pip install -r requirements-dev.txt
97

108
script: pytest
11-
9+

MANIFEST

Lines changed: 0 additions & 16 deletions
This file was deleted.

O365/__init__.py

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,12 @@
1-
# Copyright 2015 by Toben "Narcolapser" Archer. All Rights Reserved.
2-
#
3-
# Permission to use, copy, modify, and distribute this software and its documentation for any purpose
4-
# and without fee is hereby granted, provided that the above copyright notice appear in all copies and
5-
# that both that copyright notice and this permission notice appear in supporting documentation, and
6-
# that the name of Toben Archer not be used in advertising or publicity pertaining to distribution of
7-
# the software without specific, written prior permission. TOBEN ARCHER DISCLAIMS ALL WARRANTIES WITH
8-
# REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
9-
# SHALL TOBEN ARCHER BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
10-
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
11-
# OR OTHER TORTIOUS ACTION, ARISING OUT
12-
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
13-
14-
'''
15-
Python library for interfacing with the Microsoft Office 365 online.
16-
'''
17-
#__all__ = ['attachment','cal','contact','event','group','inbox','message','schedule']
18-
19-
# This imports all the libraries into the local namespace. This makes it easy to work with.
20-
21-
from .contact import Contact
22-
from .group import Group
23-
from .cal import Calendar
24-
from .event import Event
25-
from .attachment import Attachment
26-
from .inbox import Inbox
27-
from .message import Message
28-
from .schedule import Schedule
29-
from .connection import Connection
30-
from .fluent_inbox import FluentInbox
31-
32-
33-
#To the King!
1+
"""
2+
A simple python library to interact with Microsoft Graph and Office 365 API
3+
"""
4+
5+
from .account import Account
6+
from .connection import Connection, Protocol, MSGraphProtocol, MSOffice365Protocol, oauth_authentication_flow
7+
from .mailbox import MailBox
8+
from .message import Message, MessageAttachment, Recipient
9+
from .address_book import AddressBook, Contact, RecipientType
10+
from .calendar import Schedule, Calendar, Event, EventResponse, AttendeeType, EventSensitivity, EventShowAs, CalendarColors, EventAttachment
11+
from .drive import Storage, Drive, Folder, File, Image, Photo
12+
from .utils import OneDriveWellKnowFolderNames, OutlookWellKnowFolderNames, ImportanceLevel

O365/account.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from O365.connection import Connection, Protocol, MSGraphProtocol, oauth_authentication_flow
2+
from O365.drive import Storage
3+
from O365.utils import ME_RESOURCE
4+
from O365.message import Message
5+
from O365.mailbox import MailBox
6+
from O365.address_book import AddressBook, GlobalAddressList
7+
from O365.calendar import Schedule
8+
9+
10+
class Account(object):
11+
""" Class helper to integrate all components into a single object """
12+
13+
def __init__(self, credentials, *, protocol=None, main_resource=ME_RESOURCE, **kwargs):
14+
"""
15+
Account constructor.
16+
:param credentials: a tuple containing the client_id and client_secret
17+
:param protocol: the protocol to be used in this account instance
18+
:param main_resource: the resource to be used by this account
19+
:param kwargs: any extra args to be passed to the Connection instance
20+
"""
21+
22+
protocol = protocol or MSGraphProtocol # defaults to Graph protocol
23+
self.protocol = protocol(default_resource=main_resource, **kwargs) if isinstance(protocol, type) else protocol
24+
25+
if not isinstance(self.protocol, Protocol):
26+
raise ValueError("'protocol' must be a subclass of Protocol")
27+
28+
self.con = Connection(credentials, **kwargs)
29+
self.main_resource = main_resource
30+
31+
def __repr__(self):
32+
if self.con.auth:
33+
return 'Account Client Id: {}'.format(self.con.auth[0])
34+
else:
35+
return 'Unidentified Account'
36+
37+
def authenticate(self, *, scopes, **kwargs):
38+
"""
39+
Performs the oauth authentication flow resulting in a stored token.
40+
It uses the credentials passed on instantiation
41+
:param scopes: a list of protocol user scopes to be converted by the protocol
42+
:param kwargs: other configuration to be passed to the Connection instance
43+
"""
44+
kwargs.setdefault('token_file_name', self.con.token_path.name)
45+
46+
return oauth_authentication_flow(*self.con.auth, scopes=scopes, protocol=self.protocol, **kwargs)
47+
48+
@property
49+
def connection(self):
50+
""" Alias for self.con """
51+
return self.con
52+
53+
def new_message(self, resource=None):
54+
"""
55+
Creates a new message to be send or stored
56+
:param resource: Custom resource to be used in this message. Defaults to parent main_resource.
57+
"""
58+
return Message(parent=self, main_resource=resource, is_draft=True)
59+
60+
def mailbox(self, resource=None):
61+
"""
62+
Creates MailBox Folder instance
63+
:param resource: Custom resource to be used in this mailbox. Defaults to parent main_resource.
64+
"""
65+
return MailBox(parent=self, main_resource=resource, name='MailBox')
66+
67+
def address_book(self, *, resource=None, address_book='personal'):
68+
"""
69+
Creates Address Book instance
70+
:param resource: Custom resource to be used in this address book. Defaults to parent main_resource.
71+
:param address_book: Choose from Personal or Gal (Global Address List)
72+
"""
73+
if address_book == 'personal':
74+
return AddressBook(parent=self, main_resource=resource, name='Personal Address Book')
75+
elif address_book == 'gal':
76+
return GlobalAddressList(parent=self)
77+
else:
78+
raise RuntimeError('Addres_book must be either "personal" (resource address book) or "gal" (Global Address List)')
79+
80+
def schedule(self, *, resource=None):
81+
"""
82+
Creates Schedule instance to handle calendars
83+
:param resource: Custom resource to be used in this schedule object. Defaults to parent main_resource.
84+
"""
85+
return Schedule(parent=self, main_resource=resource)
86+
87+
def storage(self, *, resource=None):
88+
"""
89+
Creates a Storage instance to handle file storage like OneDrive or Sharepoint document libraries
90+
:param resource: Custom resource to be used in this drive object. Defaults to parent main_resource.
91+
"""
92+
if not isinstance(self.protocol, MSGraphProtocol):
93+
# TODO: a custom protocol accessing OneDrive or Sharepoint Api will fail here.
94+
raise RuntimeError('Drive options only works on Microsoft Graph API')
95+
96+
return Storage(parent=self, main_resource=resource)

0 commit comments

Comments
 (0)