-commands = {
- 'exit': None, # catched below, quits the loop
- 'help': help,
- 'open': sendcmd(tuerSock, 'open'),
- 'close': sendcmd(tuerSock, 'close'),
- 'buzz': sendcmd(tuerSock, 'buzz'),
-}
+def exitcmd(c):
+ print("Bye")
+ return True
+
+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
+
+commands = alias({
+ 'exit': (exitcmd, 'Quits this shell'),
+ 'help': (helpcmd, 'Helps you getting to know the available commands'),
+ 'open': (sendcmd(tuerSock, 'unlock'), 'Will try to unlock the apartment door'),
+ 'buzz': (sendcmd(tuerSock, 'buzz'), 'Will buzz the buzzer for the street door'),
+ 'who': (whocmd, 'Shows the list of people, who are allowed to control this system'),
+},{
+ # aliases
+ 'unlock': 'open',
+})
+
+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