MCPcopy Index your code
hub / github.com/O365/python-o365 / O365Account

Class O365Account

examples/onedrive_example.py:11–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9
10
11class O365Account():
12 def __init__(self, client_id=client_id,
13 client_secret=client_secret, scopes=scopes):
14 self.client_id = client_id
15 self.client_secret = client_secret
16 self.account = Account(credentials=(client_id, client_secret))
17 self.authenticate(scopes)
18
19 self.storage = self.account.storage()
20 self.drives = self.storage.get_drives()
21 self.my_drive = self.storage.get_default_drive() # or get_drive('drive-id')
22 self.root_folder = self.my_drive.get_root_folder()
23
24 def authenticate(self, scopes=scopes):
25 result = self.account.authenticate(scopes=scopes)
26
27 def get_drive(self):
28 return self.my_drive
29
30 def get_root_folder(self):
31 return self.root_folder
32
33 def get_folder_from_path(self, folder_path):
34 if folder_path is None:
35 return self.my_drive
36
37 subfolders = folder_path.split('/')
38 if len(subfolders) == 0:
39 return self.my_drive
40
41 items = self.my_drive.get_items()
42 for subfolder in subfolders:
43 try:
44 subfolder_drive = list(filter(lambda x: subfolder in x.name, items))[0]
45 items = subfolder_drive.get_items()
46 except:
47 raise ('Path {} not exist.'.format(folder_path))
48 return subfolder_drive
49
50 ''' Upload a file named $filename to onedrive folder named $destination. '''
51
52 def upload_file(self, filename, destination=None):
53 folder = self.get_child_folder(self.root_folder, destination)
54 print('Uploading file ' + filename)
55 folder.upload_file(item=filename)
56
57 ''' Download a file named $filename to local folder named $to_path. '''
58
59 def download_file(self, filename, to_path=None):
60 dirname = os.path.dirname(filename)
61 basename = os.path.basename(filename)
62 folder = self.get_folder_from_path(dirname)
63 items = folder.get_items()
64 if not os.path.exists(to_path):
65 os.makedirs(to_path)
66 try:
67 file = list(filter(lambda x: basename == x.name, items))[0]
68 print('Downloading file ' + filename)

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected