-commands = {
- 'exit': exitcmd,
- 'help': helpcmd,
- 'open': sendcmd(tuerSock, 'open'),
- 'close': sendcmd(tuerSock, 'close'),
- 'buzz': sendcmd(tuerSock, 'buzz'),
-}
+def whocmd(c):
+ for n in grp.getgrnam("tuer").gr_mem:
+ p = pwd.getpwnam(n)
+ print (p.pw_name, " - ", p.pw_gecos)
+
+def alias (cmds, aliases):
+ for newname, oldname in aliases.items():
+ cmds[newname] = cmds[oldname]
+ return cmds
+
+CmdEntry = namedtuple('CmdEntry','function helpstring')
+
+commands = alias({
+ 'exit': CmdEntry(exitcmd, 'Quits this shell'),
+ 'help': CmdEntry(helpcmd, 'Helps you getting to know the available commands'),
+ 'unlock': CmdEntry(sendcmd(tuerSock, 'unlock'), 'Will try to unlock the apartment door'),
+ 'buzz': CmdEntry(sendcmd(tuerSock, 'buzz'), 'Will buzz the buzzer for the street door'),
+ 'who': CmdEntry(whocmd, 'Shows the list of people, who are allowed to control this system'),
+},{
+ # aliases
+ 'open': 'unlock',
+})
+
+def complete_command(cmd):
+ '''returns a list of commands (as strings) starting with cmd'''
+ return list(filter(lambda x: x.startswith(cmd), commands.keys()))
+readline.set_completer(lambda cmd, num: (complete_command(cmd)+[None])[num]) # wrap complete_command for readline's weird completer API
+readline.parse_and_bind("tab: complete") # run completion on tab