-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv_watcher_completion.py
More file actions
executable file
·83 lines (70 loc) · 2.08 KB
/
env_watcher_completion.py
File metadata and controls
executable file
·83 lines (70 loc) · 2.08 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
#!/usr/bin/env python
#
# This script has the auto-completion functionality of env-watcher.
#
# It is called from the shell when one presses TAP on a line that starts with env-watcher
#
# The intention is to first complete the possible actions. If an action has been selected,
# the available sessions and states are autocompleted.
import os
import sys
# some setup paths:
session_directory = os.environ["ENV_WATCHER_SESSION"]
main_directory = os.environ["ENV_WATCHER_DIR"]
if not session_directory or not main_directory:
print >>sys.stderr, "Your EnvWatcher environment was not properly set up. Please use the included setup.sh file."
sys.exit(127)
# if this fails then there is nothing I can do:
sys.path.append(os.environ["ENV_WATCHER_DIR"]+os.sep+"python")
def main():
from env_manager import env_manager
arguments = sys.argv
action_names = env_manager.action_names.keys()
if sys.argv[-1] in action_names:
# we may need to autocomplete a session name
import inspect
if "name" in inspect.getargspec(env_manager.action_names[sys.argv[-1]])[0]:
#The action takes the "name" argument:
manager = env_manager(shell=None, session_dir=session_directory, main_dir=main_directory)
sessions,states = manager.GetAllNames()
return auto_complete(sys.argv[-2], states+sessions)
else:
# There is nothing to autocomplete.
return
else:
# we need to auto_complete an action
return auto_complete(sys.argv[-2], action_names)
def auto_complete(inval, words):
possibles = []
for word in words:
if word.find(inval) == 0:
possibles.append(word)
if not possibles:
return
if len(possibles)==1:
print possibles[0]+" "
return
import os.path
lcp = os.path.commonprefix(possibles)
if lcp==inval:
for word in possibles:
print word
else:
print lcp
if __name__ == '__main__':
main()
# res=127
# try:
# res=main()
# except SystemExit:
# log()
# except Exception, e:
# print >>sys.stderr, "ERROR:",e
# log()
# except KeyboardInterrupt:
# log()
# except:
# import sys
# print >>sys.stderr, "Unknown error:",sys.exc_value,sys.exc_type
# log()
# sys.exit(res)