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'),
55 def complete_command(cmd):
56 '''returns a list of commands (as strings) starting with cmd'''
57 return list(filter(lambda x: x.startswith(cmd), commands.keys()))
58 readline.set_completer(lambda cmd, num: (complete_command(cmd)+[None])[num]) # wrap complete_command for readline's weird completer API
59 readline.parse_and_bind("tab: complete") # run completion on tab
62 print("Welcome to tyshell. Use help to see what you can do.")
69 command = shlex.split(command)
70 if not len(command): continue
71 # find suiting commands
72 if command[0] in commands: # needed in case a complete command is a prefix of another one
73 cmdoptions = [command[0]]
75 cmdoptions = complete_command(command[0])
76 # check how many we found
77 if len(cmdoptions) == 0: # no commands fit prefix
78 print("Command %s not found. Use help." % command[0])
79 elif len(cmdoptions) == 1: # exactly one command fits (prefix)
81 res = commands[cmdoptions[0]](command)
83 except Exception as e:
84 print("Error while executing %s: %s" % (command[0], str(e)))
85 else: # multiple commands fit the prefix
86 print("Ambiguous command prefix, please choose one of the following:")
87 print("\t", " ".join(cmdoptions))
88 # TODO: put current "command[0]" into the shell for the next command, but such that it is deletable with backspace