import readline
import logging
LOG_F= '/tmp/addressbook.log'
logging.basicConfig(filename=LOG_F,level=logging.DEBUG)
class Entry(object):
def __init__(self, name, email):
self.name=name
self.email=email
entries=[Entry("ahmed", "a@there.com"), Entry("John", "j@there.com"), Entry("Lora", "l@there.com")]
def add():
name=raw_input("Name: ")
email=raw_input("Email: ")
entry=Entry(name,email)
entries.append(entry)
def remove():
name=raw_input("Name: ")
for e in entries:
if e.name == name:
entries.remove(e)
break
def update():
oldname=raw_input("Name: ")
newname=raw_input("New name: ")
newemail=raw_input("New email: ")
newentry=Entry(newname,newemail)
for idx,e in enumerate(entries):
if e.name == oldname:
entries[idx]=newentry
break
def listentries():
print "Listing entries"
print "---------------"
if len(entries) > 0:
for idx, e in enumerate(entries):
print idx, " | ", e.name.ljust(5), "|", e.email.ljust(5)
else:
print "No entries found"
def usage():
print """
add: add new entry
update: update an existing entry
remove: remove an existing entry
list: listing entries
help: usage
"""
class Completer(object):
def __init__(self, options):
self.options = sorted(options)
def complete(self, text, state):
"""
text - start of completion text. This is the text since the last word break character.
state - 0 or positive int. This state is zero on the first call for a completion request and increments for each subsequent call until the end of choices is reached.
"""
matches=[opt for opt in self.options if opt.startswith(text)]
logging.debug("[+] text=%s, state=%d, matches=%s", text, state, str(matches))
try:
logging.debug("\t[+RETURN] state=%d, matches=%s, match=%s", state,str(matches), matches[state])
return matches[state]
except:
logging.debug("\tERROR:[!RETURN] state=%d, matches=%s", state, str(matches))
return None
string_to_cmd ={
'add' : add,
'remove': remove,
'update': update,
'list' : listentries,
'help' : usage,
'usage' : usage
}
words="add", "remove", "update", "list", "help", "usage"
completer=Completer(words)
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)
while True:
cmd=raw_input("[command]> ")
f=string_to_cmd[cmd]
f()
class Completer(object):
def __init__(self, options):
self.options = sorted(options)
def complete(self, text, state):
"""
text - start of completion text. This is the text since the last word break character.
state - 0 or positive int. This state is zero on the first call for a completion request and increments for each subsequent call until the end of choices is reached.
"""
matches=[opt for opt in self.options if opt.startswith(text)]
logging.debug("[+] text=%s, state=%d, matches=%s", text, state, str(matches))
try:
logging.debug("\t[+RETURN] state=%d, matches=%s, match=%s", state,str(matches), matches[state])
return matches[state]
except:
logging.debug("\tERROR:[!RETURN] state=%d, matches=%s", state, str(matches))
return Nonewords="add", "remove", "update", "list", "help", "usage" completer=Completer(words)
readline.parse_and_bind("tab: complete")
readline.set_completer(completer.complete)>>> dir(rlcompleter)
['Completer', '__all__', '__builtin__', '__builtins__', '__doc__', '__file__', '__main__', '__name__', '__package__', 'get_class_members', 'readline']
>>> import readline
>>> readline.parse_and_bind("tab: complete")easy_install rl
>>> readline.set_completer(readline.username_completion_function) >>> abrt deluge mongodb shutdown adm ftp mysql smmsp apache games nm-openconnect smolt avahi gdm nobody sshd avahi-autoipd gopher openvpn striky bin halt operator sync chrony linuxfreak pulse tcpdump colord lp root tss daemon mail rtkit usbmuxd dbus mailnull saslauth uucp >>> readline.set_completer(readline.filename_completion_function)
