-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·198 lines (180 loc) · 7.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/python3.7
import socket
import sys
from GDrive_storage_class import GDrive_Storage
from dropbox_storage_class import DropboxStorage
class CloudStorageManager:
def __init__(self):
"""Initialize the class object with auxiliary class object as attributes and bool
variables indicating connection status (whether or not the user's storage is linked)."""
self.google_drive_account = GDrive_Storage()
self.google_drive_account_status = 0
self.dropbox_account = DropboxStorage()
self.dropbox_account_status = 0
def connect_google_drive(self):
"""Link the user's Google Drive storage"""
self.google_drive_account.link_account()
self.google_drive_account_status = 1
def connect_dropbox_storage(self):
"""Link the user's Dropbox storage"""
self.dropbox_account.link_account()
self.dropbox_account_status = 1
def disconnect_google_drive(self):
"""Unlink the user's Google Drive storage"""
self.google_drive_account.unlink_account()
self.google_drive_account_status = 0
def disconnect_dropbox_storage(self):
"""Unlink the user's Dropbox storage"""
self.dropbox_account.unlink_account()
self.dropbox_account_status = 0
def _connected(self):
try:
socketInstance = socket.create_connection(("www.google.com", 80))
socketInstance.close()
return True
except:
return False
def _alert(self):
if not (self.google_drive_account_status or self.dropbox_account_status):
print("None of the storage account was connected.")
return True
else:
return False
def upload_file(self, local_path, backup_path, storage):
"""Upload file to one of the user's cloud storages"""
if storage and self.google_drive_account_status:
if backup_path:
backup_path = [i for i in backup_path.split('/') if i]
self.google_drive_account.upload_file(local_path, backup_path)
else:
self.google_drive_account.upload_file(local_path)
else:
if self.dropbox_account_status:
if '/' != backup_path[0]:
backup_path = '/' + backup_path
if '/' != backup_path[-1]:
backup_path += '/'
backup_path += local_path.split('/')[-1]
self.dropbox_account.upload_file(local_path, backup_path)
def download_file(self, filename, storage):
"""Download file from one of the user's cloud storages"""
if storage == 1 and self.google_drive_account_status:
self.google_drive_account.download_file(filename)
elif storage == 0 and self.dropbox_account_status:
self.dropbox_account.download_file(filename)
else:
if self.google_drive_account_status:
self.google_drive_account.download_file(filename)
if self.dropbox_account_status:
self.dropbox_account.download_file(filename)
def sync(self, filename, storage):
"""Update file's content in user's cloud storages"""
if storage == 1 and self.google_drive_account_status:
self.google_drive_account.sync(filename)
elif storage == 0 and self.dropbox_account_status:
self.dropbox_account.sync(filename)
else:
if self.google_drive_account_status:
self.google_drive_account.sync(filename)
if self.dropbox_account_status:
self.dropbox_account.sync(filename)
def delete_file(self, filename):
"""Delete file in user's cloud storages"""
if self.dropbox_account_status:
self.dropbox_account.delete_file(filename)
if self.google_drive_account_status:
self.google_drive_account.delete_file(filename)
def list_files(self):
"""Write to the std all files in user's cloud storages"""
if self.dropbox_account_status:
print("Dropbox files list:")
self.dropbox_account.list_files()
if self.google_drive_account_status:
print("Google Drive files list:")
self.google_drive_account.list_files()
def menu(self):
"""Present all the functionality available with the ability to choose an option."""
option_funct = {
1: self.connect_dropbox_storage,
2: self.connect_google_drive,
3: self.upload_file,
4: self.download_file,
5: self.sync,
6: self.delete_file,
7: self.disconnect_dropbox_storage,
8: self.disconnect_google_drive
}
print(
"""
Menu\n\n
1: Connect Dropbox account.\n
2: Connect GoogleDrive account.\n
3: Upload File.\n
4: Download Files.\n
5: Sync with local file.\n
6: Delete file.\n
7: Disconnect Dropbox account.\n
8: Disconnect Google Drive account.\n
9: List files.\n
10: Exit\n""")
try:
option = int(input("Enter: "))
except:
return 0
if not (1 <= option <= 10):
print('Invalid action chosen')
elif option == 10:
if self.google_drive_account_status:
self.disconnect_google_drive()
if self.dropbox_account_status:
self.disconnect_dropbox_storage()
sys.exit(0)
elif option in (1, 2):
option_funct[option]()
elif option == 7:
if not self.dropbox_account_status:
return print("Dropbox storage wasn't connected.")
option_funct[7]()
elif option == 8:
if not self.google_drive_account_status:
return print("Google Drive storage wasn't connected.")
option_funct[8]()
elif self._alert():
return
elif option == 6:
return self.delete_file(input('Enter the name of file to delete: '))
else:
if option == 3:
try:
storage = int(input("Google Drive - 1, Dropbox - 0 (skip if unimportant)? "))
except:
storage = -1
local_path = input("Enter the path to file: ")
backup_path = input("To which folder to upload (root directory by default)? ")
self.upload_file(local_path, backup_path, storage)
elif option == 4:
try:
storage = int(input("Google Drive - 1, Dropbox - 0 (skip if you forgot)? "))
except:
storage = -1
filename = input("Enter the filename: ")
self.download_file(filename, storage)
elif option == 5:
try:
storage = int(input("Google Drive - 1, Dropbox - 0 (skip if you forgot)? "))
except:
storage = -1
local_path = input("Enter the local path to file: ")
self.sync(local_path, storage)
else:
self.list_files()
def run(self):
"""Run the main routine"""
while True:
if self._connected():
self.menu()
else:
return print("No internet connection.")
if __name__ == "__main__":
app = CloudStorageManager()
app.run()