tyshell: convenience command shortcuts
[saartuer.git] / tyshell
1 #!/usr/bin/python3
2 import os
3 import readline
4 import shlex
5 import sys
6 import subprocess
7 import socket
8
9 tuerSock = "/run/tuer.sock"
10
11 # use a histfile
12 histfile = os.path.join(os.path.expanduser("~"), ".tyshellhist")
13 try:
14     readline.read_history_file(histfile)
15 except IOError:
16     pass
17 import atexit
18 atexit.register(readline.write_history_file, histfile)
19 atexit.register(print, "Bye")
20
21 # available commands
22 def helpcmd(c):
23         print("Available commands: %s" % ", ".join(sorted(longcommands.keys())))
24
25 def extcmd(cmd):
26         def run(c):
27                 ret = subprocess.call(cmd)
28                 if ret != 0:
29                         print("Command returned non-zero exit statis %d" % ret)
30         return run
31
32 def sendcmd(addr, cmd):
33         def run(c):
34                 print("Running %s..." % (cmd))
35                 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
36                 s.connect(addr)
37                 s.send(cmd.encode())
38                 data = s.recv(4)
39                 s.close()
40                 print("...done")
41                 if data != b'1':
42                         print("Received unexpected answer %s" % str(data))
43         return run
44
45 def exitcmd(c):
46         sys.exit(0)
47
48 commands = {
49         'exit': exitcmd,
50         'help': helpcmd,
51         'open': sendcmd(tuerSock, 'open'),
52         'close': sendcmd(tuerSock, 'close'),
53         'buzz': sendcmd(tuerSock, 'buzz'),
54 }
55
56 # command convenience shortcuts
57 def filterCommonPrefix (strings, length):
58         # ignores duplicates in the string list "strings"
59         toremove=[]
60         for a in strings:
61                 for b in strings:
62                         if a != b:
63                                 if a[:length] == b[:length]:
64                                         toremove.append(a)
65                                         toremove.append(b)
66         ret = list(strings) # copy
67         for x in toremove:
68                 try:
69                         ret.remove(x)
70                 except ValueError:
71                         pass
72         return ret
73
74 def shortcutify (dic):
75         maxlen = 0
76         for x in dic.keys():
77                 if len(x) > maxlen:
78                         maxlen = len(x)
79         for i in range(maxlen):
80                 shortable = filterCommonPrefix (dic.keys(), i)
81                 for x in shortable:
82                         dic[x[:i]] = dic[x]
83         return dic # only for convenience, as dic is passed by reference
84
85 longcommands = commands.copy()
86 shortcutify (commands)
87
88
89 # input loop
90 print("Welcome to tyshell. Use help to see what you can do.")
91 while True:
92         try:
93                 command = input("$ ")
94         except EOFError:
95                 print()
96                 break
97         command = shlex.split(command)
98         if not len(command): continue
99         # execute command
100         if command[0] in commands:
101                 try:
102                         commands[command[0]](command)
103                 except Exception as e:
104                         print("Error while executing %s: %s" % (command[0], str(e)))
105         else:
106                 print("Command %s not found. Use help." % command[0])
107