Skip to content

Commit 99f63f8

Browse files
author
Alejandro Casanovas
committed
Added type hinting to Account class
1 parent 2a392fe commit 99f63f8

File tree

1 file changed

+44
-43
lines changed

1 file changed

+44
-43
lines changed

O365/account.py

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1+
from typing import Type, Tuple, Optional, Callable
12
from .connection import Connection, Protocol, MSGraphProtocol, MSOffice365Protocol
23
from .utils import ME_RESOURCE, consent
34

45

56
class Account:
7+
connection_constructor: Type = Connection
68

7-
connection_constructor = Connection
9+
def __init__(self, credentials: Tuple[str, str], *,
10+
protocol: Optional[Protocol] = None,
11+
main_resource: Optional[str] = None, **kwargs):
12+
""" Creates an object which is used to access resources related to the specified credentials.
813
9-
def __init__(self, credentials, *, protocol=None, main_resource=None, **kwargs):
10-
""" Creates an object which is used to access resources related to the
11-
specified credentials
12-
13-
:param tuple credentials: a tuple containing the client_id
14-
and client_secret
15-
:param Protocol protocol: the protocol to be used in this account
16-
:param str main_resource: the resource to be used by this account
17-
('me' or 'users', etc.)
14+
:param credentials: a tuple containing the client_id and client_secret
15+
:param protocol: the protocol to be used in this account
16+
:param main_resource: the resource to be used by this account ('me' or 'users', etc.)
1817
:param kwargs: any extra args to be passed to the Connection instance
1918
:raises ValueError: if an invalid protocol is passed
2019
"""
2120

2221
protocol = protocol or MSGraphProtocol # Defaults to Graph protocol
23-
self.protocol = protocol(default_resource=main_resource,
24-
**kwargs) if isinstance(protocol,
25-
type) else protocol
22+
if isinstance(protocol, type):
23+
protocol = protocol(default_resource=main_resource, **kwargs)
24+
self.protocol: Protocol = protocol
2625

2726
if not isinstance(self.protocol, Protocol):
2827
raise ValueError("'protocol' must be a subclass of Protocol")
@@ -48,7 +47,8 @@ def __init__(self, credentials, *, protocol=None, main_resource=None, **kwargs):
4847
main_resource = ''
4948

5049
elif auth_flow_type == 'password':
51-
kwargs['scopes'] = self.protocol.get_scopes_for(scopes) if scopes else [self.protocol.prefix_scope('.default')]
50+
kwargs['scopes'] = self.protocol.get_scopes_for(scopes) if scopes else [
51+
self.protocol.prefix_scope('.default')]
5252

5353
# set main_resource to blank when it's the 'ME' resource
5454
if self.protocol.default_resource == ME_RESOURCE:
@@ -60,7 +60,7 @@ def __init__(self, credentials, *, protocol=None, main_resource=None, **kwargs):
6060
'"public"')
6161

6262
self.con = self.connection_constructor(credentials, **kwargs)
63-
self.main_resource = main_resource or self.protocol.default_resource
63+
self.main_resource: str = main_resource or self.protocol.default_resource
6464

6565
def __repr__(self):
6666
if self.con.auth:
@@ -69,34 +69,35 @@ def __repr__(self):
6969
return 'Unidentified Account'
7070

7171
@property
72-
def is_authenticated(self):
72+
def is_authenticated(self) -> bool:
7373
"""
74-
Checks whether the library has the authentication and that is not expired
75-
:return: True if authenticated, False otherwise
74+
Checks whether the library has the authentication and that is not expired.
75+
Return True if authenticated, False otherwise.
7676
"""
7777
token = self.con.token_backend.token
7878
if not token:
7979
token = self.con.token_backend.get_token()
8080

8181
return token is not None and not token.is_expired
8282

83-
def authenticate(self, *, scopes=None, handle_consent=consent.consent_input_token, **kwargs):
83+
def authenticate(self, *, scopes: Optional[list] = None,
84+
handle_consent: Callable = consent.consent_input_token, **kwargs) -> bool:
8485
""" Performs the oauth authentication flow using the console resulting in a stored token.
85-
It uses the credentials passed on instantiation
86+
It uses the credentials passed on instantiation.
87+
Returns True if succeded otherwise False.
8688
87-
:param list[str] or None scopes: list of protocol user scopes to be converted
89+
:param scopes: list of protocol user scopes to be converted
8890
by the protocol or scope helpers
8991
:param handle_consent: a function to handle the consent process by default just input for the token url
9092
:param kwargs: other configurations to be passed to the
9193
Connection.get_authorization_url and Connection.request_token methods
92-
:return: Success / Failure
93-
:rtype: bool
9494
"""
9595

9696
if self.con.auth_flow_type in ('authorization', 'public'):
9797
if scopes is not None:
9898
if self.con.scopes is not None:
99-
raise RuntimeError('The scopes must be set either at the Account instantiation or on the account.authenticate method.')
99+
raise RuntimeError('The scopes must be set either at the Account '
100+
'instantiation or on the account.authenticate method.')
100101
self.con.scopes = self.protocol.get_scopes_for(scopes)
101102
else:
102103
if self.con.scopes is None:
@@ -140,7 +141,7 @@ def connection(self):
140141
"""
141142
return self.con
142143

143-
def new_message(self, resource=None):
144+
def new_message(self, resource: Optional[str] = None):
144145
""" Creates a new message to be sent or stored
145146
146147
:param str resource: Custom resource to be used in this message
@@ -151,24 +152,24 @@ def new_message(self, resource=None):
151152
from .message import Message
152153
return Message(parent=self, main_resource=resource, is_draft=True)
153154

154-
def mailbox(self, resource=None):
155+
def mailbox(self, resource: Optional[str] = None):
155156
""" Get an instance to the mailbox for the specified account resource
156157
157-
:param str resource: Custom resource to be used in this mailbox
158+
:param resource: Custom resource to be used in this mailbox
158159
(Defaults to parent main_resource)
159160
:return: a representation of account mailbox
160161
:rtype: O365.mailbox.MailBox
161162
"""
162163
from .mailbox import MailBox
163164
return MailBox(parent=self, main_resource=resource, name='MailBox')
164165

165-
def address_book(self, *, resource=None, address_book='personal'):
166+
def address_book(self, *, resource: Optional[str] = None, address_book: str = 'personal'):
166167
""" Get an instance to the specified address book for the
167168
specified account resource
168169
169-
:param str resource: Custom resource to be used in this address book
170+
:param resource: Custom resource to be used in this address book
170171
(Defaults to parent main_resource)
171-
:param str address_book: Choose from 'Personal' or 'Directory'
172+
:param address_book: Choose from 'Personal' or 'Directory'
172173
:return: a representation of the specified address book
173174
:rtype: AddressBook or GlobalAddressList
174175
:raises RuntimeError: if invalid address_book is specified
@@ -188,29 +189,29 @@ def address_book(self, *, resource=None, address_book='personal'):
188189
'address_book must be either "Personal" '
189190
'(resource address book) or "Directory" (Active Directory)')
190191

191-
def directory(self, resource=None):
192+
def directory(self, resource: Optional[str] = None):
192193
""" Returns the active directory instance"""
193194
from .directory import Directory, USERS_RESOURCE
194195

195196
return Directory(parent=self, main_resource=resource or USERS_RESOURCE)
196197

197-
def schedule(self, *, resource=None):
198+
def schedule(self, *, resource: Optional[str] = None):
198199
""" Get an instance to work with calendar events for the
199200
specified account resource
200201
201-
:param str resource: Custom resource to be used in this schedule object
202+
:param resource: Custom resource to be used in this schedule object
202203
(Defaults to parent main_resource)
203204
:return: a representation of calendar events
204205
:rtype: Schedule
205206
"""
206207
from .calendar import Schedule
207208
return Schedule(parent=self, main_resource=resource)
208209

209-
def storage(self, *, resource=None):
210+
def storage(self, *, resource: Optional[str] = None):
210211
""" Get an instance to handle file storage (OneDrive / Sharepoint)
211212
for the specified account resource
212213
213-
:param str resource: Custom resource to be used in this drive object
214+
:param resource: Custom resource to be used in this drive object
214215
(Defaults to parent main_resource)
215216
:return: a representation of OneDrive File Storage
216217
:rtype: Storage
@@ -223,11 +224,11 @@ def storage(self, *, resource=None):
223224
from .drive import Storage
224225
return Storage(parent=self, main_resource=resource)
225226

226-
def sharepoint(self, *, resource=''):
227+
def sharepoint(self, *, resource: str = ''):
227228
""" Get an instance to read information from Sharepoint sites for the
228229
specified account resource
229230
230-
:param str resource: Custom resource to be used in this sharepoint
231+
:param resource: Custom resource to be used in this sharepoint
231232
object (Defaults to parent main_resource)
232233
:return: a representation of Sharepoint Sites
233234
:rtype: Sharepoint
@@ -242,7 +243,7 @@ def sharepoint(self, *, resource=''):
242243
from .sharepoint import Sharepoint
243244
return Sharepoint(parent=self, main_resource=resource)
244245

245-
def planner(self, *, resource=''):
246+
def planner(self, *, resource: str = ''):
246247
""" Get an instance to read information from Microsoft planner """
247248

248249
if not isinstance(self.protocol, MSGraphProtocol):
@@ -253,7 +254,7 @@ def planner(self, *, resource=''):
253254
from .planner import Planner
254255
return Planner(parent=self, main_resource=resource)
255256

256-
def tasks(self, *, resource=''):
257+
def tasks(self, *, resource: str = ''):
257258
""" Get an instance to read information from Microsoft ToDo """
258259

259260
if isinstance(self.protocol, MSOffice365Protocol):
@@ -262,8 +263,8 @@ def tasks(self, *, resource=''):
262263
from .tasks_graph import ToDo as ToDo
263264

264265
return ToDo(parent=self, main_resource=resource)
265-
266-
def teams(self, *, resource=''):
266+
267+
def teams(self, *, resource: str = ''):
267268
""" Get an instance to read information from Microsoft Teams """
268269

269270
if not isinstance(self.protocol, MSGraphProtocol):
@@ -273,13 +274,13 @@ def teams(self, *, resource=''):
273274
from .teams import Teams
274275
return Teams(parent=self, main_resource=resource)
275276

276-
def outlook_categories(self, *, resource=''):
277+
def outlook_categories(self, *, resource: str = ''):
277278
""" Returns a Categories object to handle the available Outlook Categories """
278279
from .category import Categories
279280

280281
return Categories(parent=self, main_resource=resource)
281282

282-
def groups(self, *, resource=''):
283+
def groups(self, *, resource: str = ''):
283284
""" Get an instance to read information from Microsoft Groups """
284285

285286
if not isinstance(self.protocol, MSGraphProtocol):

0 commit comments

Comments
 (0)