-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
155 lines (121 loc) · 6.17 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
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 23 21:52:40 2018
@author: TonyY
"""
import googledrive
import dbapi
import onedrive_operation as onedrive
import splitfile
import os
import queue
from threading import Thread
from tkinter import Tk
from tkinter.filedialog import askopenfilename
def main():
googledrive.buildService()
dbapi.Login()
od = onedrive.str_operation()
isRunning = True
try:
while isRunning:
action = input('list, search, upload, ' +
'download, update, delete, exit: ')
if action == 'list':
googledrive.listFiles()
#dbapi.ShowFiles()
#od.search_all()
elif action == 'search':
filename = input('enter file name: ')
if filename == '':
print('please enter a file name')
continue
googledrive.searchFiles(filename=filename, mode = 'contains')
#dbapi.SearchFile(file=filename)
elif action == 'upload':
try:
root = Tk()
root.withdraw
filepath = askopenfilename(initialdir = 'upload/')
filename = os.path.basename(filepath)
root.destroy()
if filepath == '':
continue
tempfilepaths = splitfile.splitFile(filepath)
threads = []
threads.append(Thread(googledrive.uploadFile(tempfilepaths[0])))
threads.append(Thread(dbapi.UploadFile(file=tempfilepaths[1])))
threads.append(Thread(od.upload(tempfilepaths[2])))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print("Upload " + filename +" successfully")
for tempfilepath in tempfilepaths:
os.remove(tempfilepath)
except:
print ('Upload Failed.')
elif action == 'download':
try:
filename = input('enter file name: ')
que = queue.Queue()
threads = []
threads.append(Thread(target=lambda q: q.put(googledrive.downloadFile(filename+'.000')),args=(que,)))
threads.append(Thread(target=lambda q: q.put(dbapi.DownloadFile(file=filename+'.001')),args=(que,)))
threads.append(Thread(target=lambda q: q.put(od.download(filename+'.002')),args=(que,)))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print("Download " + filename +" successfully")
tempfilepaths =[]
while not que.empty():
tempfilepaths.append(que.get())
splitfile.joinFiles(tempfilepaths)
with que.mutex:
que.queue.clear()
for tempfilepath in tempfilepaths:
os.remove(tempfilepath)
except:
print ('Download Failed.')
elif action == 'delete':
filename = input('enter file name: ')
threads = []
threads.append(Thread(googledrive.deleteFile(filename+'.000')))
threads.append(Thread(dbapi.DeleteFile(file=filename+'.001')))
threads.append(Thread(od.delete(filename+'.002')))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print("Delete " + filename + " successfully")
elif action == 'update':
root = Tk()
root.withdraw
filepath = askopenfilename(initialdir = 'upload/')
filename = os.path.basename(filepath)
root.destroy()
if not filepath:
continue
tempfilepaths = splitfile.splitFile(filepath)
threads = []
threads.append(Thread(googledrive.updateFile(filepath = tempfilepaths[0])))
threads.append(Thread(dbapi.UploadFile(file=tempfilepaths[1])))
threads.append(Thread(od.update(tempfilepaths[2])))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
print("Update " + filename + " successfully")
for tempfilepath in tempfilepaths:
os.remove(tempfilepath)
elif action == 'exit':
print ('Exiting the program')
isRunning = False
else:
print('ONLY support list, search, upload,' +
'download, update, delete& exit')
except:
print ('An error occured.')
if __name__ == '__main__':
main()