Skip to content

Commit

Permalink
Merge pull request googleapis#135 from O365/rewrite
Browse files Browse the repository at this point in the history
Complete rewrite courtesy of Janscas.
  • Loading branch information
Narcolapser authored Nov 6, 2018
2 parents a1b9f00 + 50616e1 commit a3d2b03
Show file tree
Hide file tree
Showing 64 changed files with 6,922 additions and 4,531 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,8 @@ target/
*/pid

.idea/

trail.py
sample_run.py


14 changes: 6 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
language: python
#python:
# - "2.7"
# - "3.4"
# - "3.5"
# - "3.6"
#install:
# - pip install -r requirements.txt
python:
- "3.5"
- "3.6"

install: pip install -r requirements-dev.txt

script: pytest

16 changes: 0 additions & 16 deletions MANIFEST

This file was deleted.

45 changes: 12 additions & 33 deletions O365/__init__.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,12 @@
# Copyright 2015 by Toben "Narcolapser" Archer. All Rights Reserved.
#
# Permission to use, copy, modify, and distribute this software and its documentation for any purpose
# and without fee is hereby granted, provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in supporting documentation, and
# that the name of Toben Archer not be used in advertising or publicity pertaining to distribution of
# the software without specific, written prior permission. TOBEN ARCHER DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT
# SHALL TOBEN ARCHER BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
# OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

'''
Python library for interfacing with the Microsoft Office 365 online.
'''
#__all__ = ['attachment','cal','contact','event','group','inbox','message','schedule']

# This imports all the libraries into the local namespace. This makes it easy to work with.

from .contact import Contact
from .group import Group
from .cal import Calendar
from .event import Event
from .attachment import Attachment
from .inbox import Inbox
from .message import Message
from .schedule import Schedule
from .connection import Connection
from .fluent_inbox import FluentInbox


#To the King!
"""
A simple python library to interact with Microsoft Graph and Office 365 API
"""

from .account import Account
from .connection import Connection, Protocol, MSGraphProtocol, MSOffice365Protocol, oauth_authentication_flow
from .mailbox import MailBox
from .message import Message, MessageAttachment, Recipient
from .address_book import AddressBook, Contact, RecipientType
from .calendar import Schedule, Calendar, Event, EventResponse, AttendeeType, EventSensitivity, EventShowAs, CalendarColors, EventAttachment
from .drive import Storage, Drive, Folder, File, Image, Photo
from .utils import OneDriveWellKnowFolderNames, OutlookWellKnowFolderNames, ImportanceLevel
96 changes: 96 additions & 0 deletions O365/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from O365.connection import Connection, Protocol, MSGraphProtocol, oauth_authentication_flow
from O365.drive import Storage
from O365.utils import ME_RESOURCE
from O365.message import Message
from O365.mailbox import MailBox
from O365.address_book import AddressBook, GlobalAddressList
from O365.calendar import Schedule


class Account(object):
""" Class helper to integrate all components into a single object """

def __init__(self, credentials, *, protocol=None, main_resource=ME_RESOURCE, **kwargs):
"""
Account constructor.
:param credentials: a tuple containing the client_id and client_secret
:param protocol: the protocol to be used in this account instance
:param main_resource: the resource to be used by this account
:param kwargs: any extra args to be passed to the Connection instance
"""

protocol = protocol or MSGraphProtocol # defaults to Graph protocol
self.protocol = protocol(default_resource=main_resource, **kwargs) if isinstance(protocol, type) else protocol

if not isinstance(self.protocol, Protocol):
raise ValueError("'protocol' must be a subclass of Protocol")

self.con = Connection(credentials, **kwargs)
self.main_resource = main_resource

def __repr__(self):
if self.con.auth:
return 'Account Client Id: {}'.format(self.con.auth[0])
else:
return 'Unidentified Account'

def authenticate(self, *, scopes, **kwargs):
"""
Performs the oauth authentication flow resulting in a stored token.
It uses the credentials passed on instantiation
:param scopes: a list of protocol user scopes to be converted by the protocol
:param kwargs: other configuration to be passed to the Connection instance
"""
kwargs.setdefault('token_file_name', self.con.token_path.name)

return oauth_authentication_flow(*self.con.auth, scopes=scopes, protocol=self.protocol, **kwargs)

@property
def connection(self):
""" Alias for self.con """
return self.con

def new_message(self, resource=None):
"""
Creates a new message to be send or stored
:param resource: Custom resource to be used in this message. Defaults to parent main_resource.
"""
return Message(parent=self, main_resource=resource, is_draft=True)

def mailbox(self, resource=None):
"""
Creates MailBox Folder instance
:param resource: Custom resource to be used in this mailbox. Defaults to parent main_resource.
"""
return MailBox(parent=self, main_resource=resource, name='MailBox')

def address_book(self, *, resource=None, address_book='personal'):
"""
Creates Address Book instance
:param resource: Custom resource to be used in this address book. Defaults to parent main_resource.
:param address_book: Choose from Personal or Gal (Global Address List)
"""
if address_book == 'personal':
return AddressBook(parent=self, main_resource=resource, name='Personal Address Book')
elif address_book == 'gal':
return GlobalAddressList(parent=self)
else:
raise RuntimeError('Addres_book must be either "personal" (resource address book) or "gal" (Global Address List)')

def schedule(self, *, resource=None):
"""
Creates Schedule instance to handle calendars
:param resource: Custom resource to be used in this schedule object. Defaults to parent main_resource.
"""
return Schedule(parent=self, main_resource=resource)

def storage(self, *, resource=None):
"""
Creates a Storage instance to handle file storage like OneDrive or Sharepoint document libraries
:param resource: Custom resource to be used in this drive object. Defaults to parent main_resource.
"""
if not isinstance(self.protocol, MSGraphProtocol):
# TODO: a custom protocol accessing OneDrive or Sharepoint Api will fail here.
raise RuntimeError('Drive options only works on Microsoft Graph API')

return Storage(parent=self, main_resource=resource)
Loading

0 comments on commit a3d2b03

Please sign in to comment.