def do_add(self, arg): """Adds a new user to the phonebook, or appends data to an existing one if used with ID. All fields are optional. User name needs no prefix in command.""" tokenizer = kwtok.KeywordTokenizer(arg, "-id", "alias", "number") name = " ".join(tokenizer.rest) alias = " ".join(tokenizer.alias) number = tokenizer.number if tokenizer.id: user = self.book.findSingleUser(id=int(tokenizer.id)) if user[0]: user[1].addAlias([alias, name]) user[1].addNumber(number) print("Added to {0}.".format(user[1].firstName.upper())) else: print("User not found!") elif name: add = self.book.addUser([name, alias], number) if add[0]: print("Added {0}.".format(add[1].firstName.upper())) else: print("Can't add user with an existing number!") else: print("Can't add user without name or ID!")
def do_remove(self, arg): """Removes data from the user, number or alias.""" tokenizer = kwtok.KeywordTokenizer(arg, "number", "alias") self.user.removeAlias(tokenizer.alias) self.user.removeNumber(tokenizer.number) self.user.toString()
def do_add(self, arg): """Adds data to the user, number or alias.""" tokenizer = kwtok.KeywordTokenizer(arg, "number", "alias") self.user.addAlias(" ".join(tokenizer.alias)) self.user.addNumber(tokenizer.number) self.user.toString()
def do_number(self, arg): """Directly adds or removes numbers to a user, filtered by name, ID, or number.""" tokenizer = kwtok.KeywordTokenizer(arg, "-id", "number", "add", "remove") find = self.book.findUsers(" ".join(tokenizer.rest), tokenizer.number, tokenizer.id) if not find[0]: print("User not found!") elif len(find[1]) > 1: print( "Multiple users found! Please narrow your search using ID or number.\n" ) self.do_lookup(arg) else: if tokenizer.add and find[1][0].addNumber(tokenizer.add): print("Number(s) {0} added successfully.".format(", ".join( tokenizer.add))) if tokenizer.remove: if find[1][0].removeNumber(tokenizer.remove): print("Number(s) {0} removed successfully.".format( ", ".join(tokenizer.remove))) else: print("Number(s) {0} not found in user {1}.".format( ", ".join(tokenizer.remove), find[1][0].firstName.upper()))
def do_find(self, arg): """Finds matching users in the phonebook, and prints their information. Filtered using name, number, and ID.""" tokenizer = kwtok.KeywordTokenizer(arg, "-id", "number") find = self.book.findUsers(" ".join(tokenizer.rest), tokenizer.number, tokenizer.id) if find[0]: for user in find[1]: self.book.printSingleUser(user) else: print("No matches!")
def do_remove(self, arg): """Removes a user from the phonebook. Can be filtered using name, ID, and number.""" tokenizer = kwtok.KeywordTokenizer(arg, "-id", "number") find = self.book.findUsers(" ".join(tokenizer.rest), tokenizer.number, tokenizer.id) if not find[0]: print("User not found!") elif len(find[1]) > 1: print( "Multiple users found! Please narrow your search using ID or number.\n" ) self.do_lookup(arg) else: firstName = find[1][0].firstName self.book.removeUser(user=find[1][0]) print("Removed {}!".format(firstName.upper()))
def do_change(self, arg): """Enters 'change mode' for the specified user. Filtered with name, ID, and number.""" tokenizer = kwtok.KeywordTokenizer(arg, "-id", "number") find = self.book.findUsers(" ".join(tokenizer.rest), tokenizer.number, tokenizer.id) if not find[0]: print("User not found!") elif len(find[1]) > 1: print( "Multiple users found! Please narrow your search using ID or number.\n" ) self.do_find(arg) else: shell = PBShellChange(self.book, find[1][0]) shell.cmdloop()