This repository has been archived by the owner on Apr 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
214 lines (167 loc) · 6.64 KB
/
cli.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""
Google drive python cli
initial instrucitons from
https://developers.google.com/drive/v3/web/quickstart/python
classes:
File_object - handler for a file both on drive and on local
Drive - handler for the connection to google drive
Local - handler for the connection to local files
"""
from drive import Drive
import thread
import code
import traceback
import sys
class CLI():
def __init__(self, drive):
self.options = {
'': [self.do_nothing, ''],
'q': [self.end_run, ''],
'exit': [self.end_run, 'quit the cli'],
'pwd': [self.show_pwd, 'print the working directory'],
'ls': [self.show_ls, 'list the file in this dir'],
'cd': [self.change_dir, '[search option] change directory'],
'details': [self.details, '[search option] print the file details'],
'check': [self.background_check, 'Recursivly check for new files'],
'pull': [self.background_pull, 'Recursivly download new files'],
'stop': [self.stop, 'stop check or pull'],
'report': [self.report, 'report on background check and download'],
'log': [self.log, 'print check and pull log'],
'help': [self.help, 'print this list'],
}
self.auto_check = True
self.prompt = " H> "
self.drive = drive
self.root = drive.get_root()
self.pwd = self.root
self.ui = []
self.show_ls()
print("type help to see command list.")
self.background_check()
def help(self):
string = ""
for o,v in self.options.iteritems():
text = v[1]
if o:
string += "\t"
if not text:
string += ("%s, " % o)
else:
string += "%s :\t%s\n" % (o,text)
string += "\nhint: Run python -i cli with and type exit to get access to the drive object, \n"
string += "\tthe main check thread will keep going and build up the found file lists in \n"
string += "\tie: try len(drive.files) to see how many files are collected\n"
string += "\tmost cli commands can be accessed directly with cli.<commmand>() like cli.help()\n"
string += "\ttype cli.run() to get back to this cli, or exit() to quit completely\n"
print (string)
def report(self):
failed = "failed" in self.ui
print(self.drive.as_string(failed))
def do_nothing(self):
pass
def log(self):
last = None
for i in self.ui:
if isinstance(i,int): last = i
self.drive.print_log(last = last)
self.drive.echo(True)
def echo_on(self):
self.drive.echo(True)
def stop(self):
self.background_check(stop=True)
def background_pull(self):
dirs = ("dirs" in self.ui) or ("directories" in self.ui)
after = "after" in self.ui
self.background_check(pull=True, dirs_only = dirs, )
self.drive.echo(True)
"""
runs check on the drive as a seperate thread that that builds up the file structure in the back ground
"""
def background_check(self, stop = None, depth = None, force = None, pull=False, dirs_only=False):
if depth is None:
for i in self.ui:
if isinstance(i,int): depth = i
if stop is None: stop = "stop" in self.ui
if force is None: force = "force" in self.ui
if "auto_off" in self.ui: self.auto_check = False
if "auto_on" in self.ui: self.auto_check = True
def threadded_check():
try:
self.drive.check( self.pwd, depth = depth, pull=pull,dirs_only=dirs_only)
except KeyboardInterrupt: #abort with contorl C
pass
except Exception, err:
exc_info = sys.exc_info()
traceback.print_exception(*exc_info)
self.drive.check_ready = True
self.drive.wait_for_ready()
if not stop and self.auto_check:
print("starting background check")
thread.start_new_thread(threadded_check, ())
def details(self):
self.background_check(stop=True)
f = self.select_file()
if f: print(f.as_string(details = True))
self.background_check()
def change_dir(self):
self.background_check(stop=True)
new_dir = self.select_file()
if new_dir is not None:
self.pwd = new_dir
print("changing to: %s" % self.pwd)
self.show_ls(tab=1,new_line = 1)
else:
print("no dir matched")
self.background_check()
def select_file(self):
if len(self.ui) < 2: return None
else: next_dir = "%s" % self.ui[1]
if "/" in next_dir: return self.root
if ".." in next_dir:
if self.pwd.parent is None: print("no parent")
return self.pwd.parent
if next_dir[0] == '.':
return self.pwd
ls = self.pwd.ls()
if ls is None:
print("No sub files or folders")
return None
for i in ls:
if next_dir.lower() in i.name.lower():
#print("found %s (matched %s)" % (i.name,next_dir))
return i
return None
def show_ls(self, tab=1, *args, **kargs):
force = "force" in self.ui
self.background_check(stop=True)
print(self.pwd.ls_string(force = force, tab = tab, *args, **kargs))
def show_pwd(self):
print("%s" % self.pwd.pwd_string())
def end_run(self):
self.end = True
def run(self):
self.end = False
while not self.end :
self.ui = raw_input(self.prompt).split()
self.drive.echo(False)
if not self.ui: continue
# make ints out of int arguements
if self.ui[0] != "cd":
for i,v in enumerate(self.ui):
try:
self.ui[i] = int(v)
except:
pass
#print(self.ui)
try:
#get function by name, then pull out first element in the self.options.
#This is where the function is
function = self.options[self.ui[0]][0]
except:
print("command unknown")
function = None
if function: function()
if __name__ == '__main__':
drive = Drive()
cli = CLI(drive)
cli.run()