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)
22 print("Available commands: %s" % ", ".join(sorted(commands.keys())))
26 ret = subprocess.call(cmd)
28 print("Command returned non-zero exit statis %d" % ret)
31 def sendcmd(addr, cmd):
33 print("Running %s..." % (cmd))
34 s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
40 print(data.decode('utf-8'))
50 'open': sendcmd(tuerSock, 'unlock'),
51 'unlock': sendcmd(tuerSock, 'unlock'),
52 'buzz': sendcmd(tuerSock, 'buzz'),
56 print("Welcome to tyshell. Use help to see what you can do.")
63 command = shlex.split(command)
64 if not len(command): continue
65 # find suiting commands
66 if command[0] in commands: # needed in case a complete command is a prefix of another one
67 cmdoptions = [command[0]]
69 cmdoptions = list(filter(lambda x: command[0].startswith(x), commands.keys()))
70 # check how many we found
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 res = commands[cmdoptions[0]](command)
77 except Exception as e:
78 print("Error while executing %s: %s" % (command[0], str(e)))
79 else: # multiple commands fit the prefix
80 print("Ambiguous command prefix, please choose one of the following:")
81 print("\t", " ".join(cmdoptions))
82 # TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace