Example #1
0
class CLI:

    def __init__(self, db_file):
        self.db = DB(db_file)
        self.cli_re = re.compile(r'^\s*(?P<cmd>\S+)\s*(?P<nick>\S+)?\s*(?P<name>.+)?$')

    def help(self):
        print """
        new [nick [name]] Register new member with optional nick and name (will be prompted if not supplied)
        list              Show all members that are currently in the database
        quit              Exit the program
        help              Show this
        """
        
    def start(self):

        while True:
            try:
                inp = raw_input('[ Verdande ]$ ')

                match = self.cli_re.match(inp)
                if match:
                    cmd = match.group('cmd')
                    if cmd == 'new':
                        nick = match.group('nick') if match.group('nick') else raw_input('> Nick: ')
                        name = match.group('name') if match.group('name') else raw_input('> Full Name: ')
                        if not self.db.add_member(nick, name):
                            print "Member %s allready exist." % (nick)
                        else:
                            print "Successfully registered %s." % (nick)

                    elif cmd == 'list' or cmd == 'show':
                        print self.db

                    elif cmd == 'del':
                        if raw_input('are you root? ') == 'I always run as root!':
                            print "ok"
                            nick = match.group('nick') if match.group('nick') else raw_input('> Nick: ')
                            self.db.del_member(nick)
                        else:
                            print "No you're not!"
                            
                    elif cmd == 'quit':
                        answ = raw_input('Are you sure you want to quit [y/N]? ')
                        if answ == 'y' or answ == 'Y':
                            break

                    else:
                        self.help()
                        
            except EOFError:
                answ = raw_input('\nAre you sure you want to quit [y/N]? ')
                if answ == 'y' or answ == 'Y':
                    break

        self.db.close()