-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathstart.py
More file actions
168 lines (152 loc) · 5.34 KB
/
start.py
File metadata and controls
168 lines (152 loc) · 5.34 KB
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
import webbrowser
from pathlib import Path, PurePath
from UniDrive import UniDrive
from exceptions import *
def prepare_unidrive():
prjdir = Path('.').resolve()
unidrive = UniDrive(prjdir)
# google
try:
auth_url = unidrive.register_store('GoogleDriveStore', 'test-googledrive')
webbrowser.open_new(auth_url)
res = input('response url for test-googledrive :')
if unidrive.activate_store('test-googledrive', res):
print('success test-googledrive')
else:
print('fail test-googledrive')
except Exception:
print('test-googledrive is already registered')
# dropbox
try:
auth_url = unidrive.register_store('DropboxStore', 'test-dropbox')
webbrowser.open_new(auth_url)
res = input('response url for test-dropbox :')
if unidrive.activate_store('test-dropbox', res):
print('success test-dropbox')
else:
print('fail test-dropbox')
except Exception:
print('test-dropbox is already registered')
return unidrive
def run(unidrive):
while True:
cmd = input('>> ')
cmd = cmd.split()
args = cmd[1:]
cmd = cmd[0]
if cmd == 'q':
print('quit!')
break
if cmd == 'ls':
if len(args) == 1:
detail = False
arg = args[0]
elif len(args) == 2 and args[0] == '-v':
detail = True
arg = args[1]
else:
print('please check arguments. ls [-v] path')
continue
try:
res = unidrive.get_list(arg)
except BaseStoreException as e:
print('error :', e.message)
else:
for entry in res:
if entry.is_dir:
print('[{0}]'.format(entry.name))
else:
print(entry.name)
if detail is True:
for name, store in unidrive.stores.items():
print('store name: ', name)
ls = store.get_list(PurePath(arg))
for entry in ls:
entry_line = 'name: {0} '.format(entry.name)
if entry.is_recipe:
entry_line += '[recipe]'
if entry.is_chunk:
entry_line += '[chunk]'
print(entry_line)
continue
if cmd == 'up':
if len(args) != 2:
print('please check arguments. up [from] [to]')
continue
src_path = Path(args[0])
if src_path.is_file() is False:
print('file does not exists')
continue
with open(src_path, 'rb') as src_file:
src_data = src_file.read()
dest_path = args[1]
try:
unidrive.upload_file(dest_path, src_data)
except BaseStoreException as e:
print('error :', e.message)
else:
print('Done.')
continue
if cmd == 'down':
if len(args) != 2:
print('please check arguments. down [from] [to]')
continue
src_path = args[0]
dest_path = Path(args[1])
if dest_path.exists() is True or dest_path.parent.is_dir() is False:
print('please check destination path :', dest_path)
continue
try:
tmp_data = unidrive.download_file(src_path)
except BaseStoreException as e:
print('error :', e.message)
else:
with open(dest_path, 'wb') as outfile:
outfile.write(tmp_data)
print('Done.')
continue
if cmd == 'mkdir':
if len(args) != 2:
print('please check arguments. mkdir [path] [dir name]')
continue
dest_path = args[0]
dir_name = args[1]
try:
unidrive.make_directory(dest_path, dir_name)
except BaseStoreException as e:
print('error :', e.message)
else:
print('Done.')
continue
if cmd == 'rm':
if len(args) != 1:
print('please check arguments. rm [path]')
continue
dest_path = args[0]
try:
unidrive.remove_file(dest_path)
except BaseStoreException as e:
print('error :', e.message)
else:
print('Done.')
continue
if cmd == 'rmdir':
if len(args) != 1:
print('please check arguments. rm [path]')
continue
dest_path = args[0]
try:
unidrive.remove_directory(dest_path)
except BaseStoreException as e:
print('error :', e.message)
else:
print('Done.')
continue
if cmd == 'df':
res = unidrive.get_usage()
for x in res:
print(x)
print('Command does not exist')
if __name__ == '__main__':
tmp = prepare_unidrive()
run(tmp)