9 tuerSock = "/run/tuer.sock"
12 histfile = os.path.join(os.path.expanduser("~"), ".tyshellhist")
14 readline.read_history_file(histfile)
18 atexit.register(readline.write_history_file, histfile)
19 atexit.register(print, "Bye")
23 print("Available commands: %s" % ", ".join(sorted(commands.keys())))
27 ret = subprocess.call(cmd)
29 print("Command returned non-zero exit statis %d" % ret)
32 def sendcmd(addr, cmd):
34 print("Running %s..." % (cmd))
35 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
42 print("Received unexpected answer %s" % str(data))
51 'open': sendcmd(tuerSock, 'open'),
52 'close': sendcmd(tuerSock, 'close'),
53 'buzz': sendcmd(tuerSock, 'buzz'),
57 print("Welcome to tyshell. Use help to see what you can do.")
64 command = shlex.split(command)
65 if not len(command): continue
66 # find suiting commands
67 if command[0] in commands: # needed in case a complete command is a prefix of another one
68 cmdoptions = [command[0]]
70 cmdoptions = list(filter(lambda x: command[0] == x[:len(command[0])],commands.keys()))
71 if len(cmdoptions) == 0: # no commands fit prefix
72 print("Command %s not found. Use help." % command[0])
73 elif len(cmdoptions) == 1: # exactly one command fits (prefix)
75 commands[cmdoptions[0]](command)
76 except Exception as e:
77 print("Error while executing %s: %s" % (command[0], str(e)))
78 else: # multiple commands fit the prefix
79 print("\t", " ".join(cmdoptions))
80 # TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace