forked from GiR-Zippo/birdcage-hive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_CLI.py
More file actions
executable file
·651 lines (587 loc) · 21.6 KB
/
mod_CLI.py
File metadata and controls
executable file
·651 lines (587 loc) · 21.6 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
# encoding: iso-8859-1
#
# Copyright (C) 20011-2013 by Booksize
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
import CP
# Our Console
import commands
import os
import sys
import threading
import time
import string
IDENTCHARS = string.ascii_letters + string.digits + '_'
## define Checking-Thread
class Master(threading.Thread):
check = True;
interval = 1;
CP #Pointer for the CP
mod_dict = None
prompt = "Command:"
identchars = IDENTCHARS
ruler = '='
lastcmd = ''
intro = None
doc_leader = ""
doc_header = "Documented commands (type help <command>):"
misc_header = "Miscellaneous help command:"
undoc_header = "Undocumented commands:"
nohelp = "No help on %s"
use_rawinput = 1
def __init__(self,CP, completekey='tab', stdin=None, stdout=None):
## Set the CP
self.CP = CP
# Do initialization what you have to do
threading.Thread.__init__(self)
self.active_module = None
#Zum idend. woher die Anfrage kam
self.ID = "CLI"
if stdin is not None:
self.stdin = stdin
else:
self.stdin = sys.stdin
if stdout is not None:
self.stdout = stdout
else:
self.stdout = sys.stdout
self.cmdqueue = []
self.completekey = completekey
return
##If any drones sends an init, this routine would be called
def initfromdrone(self, args, handler):
return
def run(self):
if self.use_rawinput and self.completekey:
try:
import readline
self.old_completer = readline.get_completer()
readline.set_completer(self.complete)
readline.parse_and_bind(self.completekey+": complete")
except ImportError:
pass
try:
while self.check:
if self.cmdqueue:
line = self.cmdqueue.pop(0)
else:
if self.use_rawinput:
try:
if(self.active_module == None):
line = raw_input(self.prompt)
else:
line = raw_input(self.active_module + "/" + self.prompt)
except EOFError:
line = 'EOF'
else:
if(self.active_module == None):
self.stdout.write(self.prompt)
else:
self.stdout.write(self.active_module + "/" + self.prompt)
self.stdout.flush()
line = self.stdin.readline()
if not len(line):
line = 'EOF'
else:
line = line.rstrip('\r\n')
if self.check :
output = self.modulcmd(line)
if (output):
self.CP.command (output, self)
elif not (output == False):
self.onecmd(line)
self.stdout.flush()
finally:
self.stdout.flush()
def clicommand(self, args):
if (args == ""):
return;
#Activate our "new-style" Console
if (self.internalCommands(args) == True and self.active_module == ""):
return
if (self.active_module == ""):
self.writeline("Ya have to select a Module via use")
return
self.output = self.CP.GetDictionary(self.active_module, args)
if (self.output):
self.CP.command (self.output, self)
return
def parseline(self, line):
"""Parse the line into a command name and a string containing
the arguments. Returns a tuple containing (command, args, line).
'command' and 'args' may be None if the line couldn't be parsed.
"""
line = line.strip()
if not line:
return None, None, line
elif line[0] == '?':
line = 'help ' + line[1:]
#elif line[0] == '!':
#if hasattr(self, 'do_shell'):
#line = 'shell ' + line[1:]
#else:
#return None, None, line
i, n = 0, len(line)
while i < n and line[i] in self.identchars: i = i+1
cmd, arg = line[:i], line[i:].strip()
return cmd, arg, line
def modulcmd(self, line):
cmd, arg, line = self.parseline(line)
if not line:
return self.emptyline()
if cmd is None:
return self.default(line)
self.lastcmd = line
if line == 'EOF' :
self.lastcmd = ''
if cmd == '':
return self.default(line)
else:
if(self.mod_dict):
try:
func = getattr(self.mod_dict, 'do_' + cmd)
return func(arg)
except AttributeError:
return self.mod_dict.get(line)
else:
return None
return None
def onecmd(self, line):
"""Interpret the argument as though it had been typed in response
to the prompt.
This may be overridden, but should not normally need to be;
see the precmd() and postcmd() methods for useful execution hooks.
The return value is a flag indicating whether interpretation of
commands by the interpreter should stop.
"""
cmd, arg, line = self.parseline(line)
if not line:
return self.emptyline()
if cmd is None:
return self.default(line)
self.lastcmd = line
if line == 'EOF' :
self.lastcmd = ''
if cmd == '':
return self.default(line)
else:
try:
func = getattr(self, 'do_' + cmd)
except AttributeError:
return self.default(line)
return func(arg)
def emptyline(self):
"""Called when an empty line is entered in response to the prompt.
If this method is not overridden, it repeats the last nonempty
command entered.
"""
if self.lastcmd:
return self.onecmd(self.lastcmd)
def default(self, line):
self.stdout.write('Unknown syntax: %s\n'%line)
def completedefault(self, *ignored):
return []
def completenames(self, text, *ignored):
dotext = 'do_'+text
if(self.mod_dict):
try:
names = self.mod_dict.get_names()
if(names):
ret = [a[3:] for a in self.get_names() if a.startswith(dotext)]
ret = ret + [b[3:] for b in names if b.startswith(dotext)]
return ret
else:
return [a[3:] for a in self.get_names() if a.startswith(dotext)]
except AttributeError:
return [a[3:] for a in self.get_names() if a.startswith(dotext)]
else:
return [a[3:] for a in self.get_names() if a.startswith(dotext)]
def complete(self, text, state):
"""Return the next possible completion for 'text'.
If a command has not been entered, then complete against command list.
Otherwise try to call complete_<command> to get list of completions.
"""
if state == 0:
import readline
origline = readline.get_line_buffer()
line = origline.lstrip()
stripped = len(origline) - len(line)
begidx = readline.get_begidx() - stripped
endidx = readline.get_endidx() - stripped
if begidx>0:
cmd, args, foo = self.parseline(line)
if cmd == '':
compfunc = self.completedefault
else:
try:
if(self.mod_dict):
try:
compfunc = getattr(self.mod_dict, 'complete_' + cmd)
except AttributeError:
compfunc = getattr(self, 'complete_' + cmd)
else:
compfunc = getattr(self, 'complete_' + cmd)
except AttributeError:
compfunc = self.completedefault
else:
compfunc = self.completenames
self.completion_matches = compfunc(text, line, begidx, endidx)
try:
return self.completion_matches[state]
except IndexError:
return None
def get_names(self):
# This method used to pull in base class attributes
# at a time dir() didn't do it yet.
return dir(self.__class__)
def complete_help(self, *args):
commands = set(self.completenames(*args))
topics = set(a[5:] for a in self.get_names()
if a.startswith('help_' + args[0]))
return list(commands | topics)
#default commands
def help_quit(self):
print '\n'.join([ 'quit',
'Shut Up And Take My Money',
])
def do_quit(self, arg):
self.CP.command("TP",self)
self.writeline("terminating...")
self.check = False
return
def help_print(self):
print '\n'.join([ 'print',
'',
])
def do_print(self, arg):
print '\n'.join([ arg,
])
return
def help_exit(self):
print '\n'.join([ 'exit',
'Exit the Active Modul',
])
def do_exit(self, arg):
if(self.active_module == None):
print '\n'.join([ 'None Active Modul',])
self.mod_dict = None
else:
print '\n'.join([ 'Exit ' + self.active_module,])
self.active_module = None
self.mod_dict = None
return
LIST = [ 'modules', 'drones']
def complete_list(self, text, line, begidx, endidx):
if not text:
completions = self.LIST[:]
else:
completions = [ f
for f in self.LIST
if f.startswith(text)
]
return completions
def help_list(self):
print '\n'.join([ 'list [modules/drones]',
'List what? Would ya like to see the modules?',
])
def do_list(self, arg):
if arg:
# XXX check arg syntax
if(arg == "modules"):
for f in self.CP.installed_mods:
if not (f[3] == "mod_CLI"):
self.stdout.write('%s\n'%f[3])
elif (arg == "drones"):
for f in self.CP.m_Sock.DroneUpdater.KnownDronesName:
self.stdout.write('%s\n'%f)
return
else:
func = getattr(self, 'help_list')
func()
return
MOPTS = ['start','stop','export','export-all']
def complete_module(self, text, line, begidx, endidx):
completions = []
try:
l = line.split(" ")[2]
t = True
except IndexError:
t = False
if(t):
if not text:
completions = self.MOPTS[:]
else:
for t in self.MOPTS:
if(t.startswith(text)):
completions.append(t)
else:
if not text:
for f in self.CP.installed_mods:
if not (f[3] == "mod_CLI"):
completions.append(f[3])
else:
for f in self.CP.installed_mods:
if not (f[3] == "mod_CLI"):
if(f[3].startswith(text)):
completions.append(f[3])
return completions
def help_module(self):
print '\n'.join([ 'module [name] [option=start/stop/export/export-all]',
'name= Of window',
'option)',
' - start Start Screen',
' - stop Stop Screen',
' - export Restart Screen',
' - export-all Sow Running State',
])
def do_module(self,arg):
if not (self.active_module):
b = arg.split(" ")
maxlen = len(b) - 1
if(maxlen >= 1):
out = ""
try:
if (b[1].strip() == "stop"):
out = "001 2 " + b[0];
elif (b[1].strip() == "start"):
out = "001 3 " + b[0];
elif (b[1].strip() == "export"):
out = "001 4 " + b[0];
elif (b[1].strip() == "export-all"):
out = "001 6"
self.CP.command(out,self)
return
except IndexError:
func = getattr(self, 'help_module')
func()
return
func = getattr(self, 'help_module')
func()
return
else:
func = getattr(self, 'help_exit')
func()
return
def complete_use(self, text, line, begidx, endidx):
completions = []
if not text:
for f in self.CP.installed_mods:
if not (f[3] == "mod_CLI"):
completions.append(f[3])
else:
for f in self.CP.installed_mods:
if not (f[3] == "mod_CLI"):
if(f[3].startswith(text)):
completions.append(f[3])
return completions
def help_use(self):
print '\n'.join([ 'use [module]',
'Ya have to select a Module via use',
])
def do_use(self, arg):
if arg:
modul = None
handler = None
for f in self.CP.installed_mods:
if(f[3] == arg):
modul = f[3]
try:
handler = f[1].getInstance()
except AttributeError:
handler = None
if(modul == None or modul == "mod_CLI"):
print '\n'.join([ 'Modul doesn´t exist!',
'use list modules to find installed Moduls',
])
else:
self.active_module = arg
dict = self.CP.GetDictionary(self.active_module)
if(dict):
try:
dict.initIT(self.CP,self,handler)
self.mod_dict = dict
except AttributeError:
self.mod_dict = dict
else:
self.mod_dict = None
return
else:
func = getattr(self, 'help_use')
func()
return
def do_help(self, arg):
'List available commands with "help" or detailed help with "help cmd".'
if arg:
# XXX check arg syntax
try:
if(self.mod_dict):
try:
func = getattr(self.mod_dict, 'help_' + arg)
func()
return
except AttributeError:
func = getattr(self, 'help_' + arg)
except AttributeError:
try:
doc=getattr(self, 'do_' + arg).__doc__
if doc:
self.stdout.write("%s\n"%str(doc))
return
except AttributeError:
pass
self.stdout.write("%s\n"%str(self.nohelp % (arg,)))
return
func()
else:
names = self.get_names()
names_mod = []
cmds_doc = []
cmds_undoc = []
help = {}
for name in names:
if name[:5] == 'help_':
help[name[5:]]=1
if(self.mod_dict):
try:
names_mod = self.mod_dict.get_names()
for name in names_mod:
if name[:5] == 'help_':
help[name[5:]]=1
except AttributeError:
names_mod = []
names.sort()
names_mod.sort()
# There can be duplicates if routines overridden
prevname = ''
for name in names:
if name[:3] == 'do_':
if name == prevname:
continue
prevname = name
cmd=name[3:]
if cmd in help:
cmds_doc.append(cmd)
del help[cmd]
elif getattr(self, name).__doc__:
cmds_doc.append(cmd)
else:
cmds_undoc.append(cmd)
if(self.mod_dict):
for name in names_mod:
if name[:3] == 'do_':
if name == prevname:
continue
prevname = name
cmd=name[3:]
if cmd in help:
cmds_doc.append(cmd)
del help[cmd]
elif getattr(self, name).__doc__:
cmds_doc.append(cmd)
else:
cmds_undoc.append(cmd)
self.stdout.write("%s\n"%str(self.doc_leader))
self.print_topics(self.doc_header, cmds_doc, 15,80)
self.print_topics(self.misc_header, help.keys(),15,80)
self.print_topics(self.undoc_header, cmds_undoc, 15,80)
def print_topics(self, header, cmds, cmdlen, maxcol):
if cmds:
self.stdout.write("%s\n"%str(header))
if self.ruler:
self.stdout.write("%s\n"%str(self.ruler * len(header)))
self.columnize(cmds, maxcol-1)
self.stdout.write("\n")
def columnize(self, list, displaywidth=80):
"""Display a list of strings as a compact set of columns.
Each column is only as wide as necessary.
Columns are separated by two spaces (one was not legible enough).
"""
if not list:
self.stdout.write("<empty>\n")
return
nonstrings = [i for i in range(len(list))
if not isinstance(list[i], str)]
if nonstrings:
raise TypeError, ("list[i] not a string for i in %s" %
", ".join(map(str, nonstrings)))
size = len(list)
if size == 1:
self.stdout.write('%s\n'%str(list[0]))
return
# Try every row count from 1 upwards
for nrows in range(1, len(list)):
ncols = (size+nrows-1) // nrows
colwidths = []
totwidth = -2
for col in range(ncols):
colwidth = 0
for row in range(nrows):
i = row + nrows*col
if i >= size:
break
x = list[i]
colwidth = max(colwidth, len(x))
colwidths.append(colwidth)
totwidth += colwidth + 2
if totwidth > displaywidth:
break
if totwidth <= displaywidth:
break
else:
nrows = len(list)
ncols = 1
colwidths = [0]
for row in range(nrows):
texts = []
for col in range(ncols):
i = row + nrows*col
if i >= size:
x = ""
else:
x = list[i]
texts.append(x)
while texts and not texts[-1]:
del texts[-1]
for col in range(len(texts)):
texts[col] = texts[col].ljust(colwidths[col])
self.stdout.write("%s\n"%str(" ".join(texts)))
#write a line to our CLI
def writeline(self,args,endt = False):
cmd, arg, line = self.parseline(args)
self.stdout.write("\n")
if(cmd == "PRINT"):
self.stdout.write("%s"%str("".join(arg)))
else:
self.stdout.write("%s"%str("".join(line)))
if(endt):
self.stdout.write("\n")
if(self.active_module == None):
self.stdout.write(self.prompt)
else:
self.stdout.write(self.active_module + "/" + self.prompt)
self.stdout.flush()
def config(self, args, CP):
return
def command(self,args, handler):
return
def update(self):
return
def stop(self):
Master.check = False
return True
def pause(self):
Master.wait = True
def unpause(self):
Master.wait = False