3
# See README.txt for information and build instructions.
8
# This function fills in a Person message based on user input.
9
def PromptForAddress(person):
10
person.id = int(raw_input("Enter person ID number: "))
11
person.name = raw_input("Enter name: ")
13
email = raw_input("Enter email address (blank for none): ")
18
number = raw_input("Enter a phone number (or leave blank to finish): ")
22
phone_number = person.phone.add()
23
phone_number.number = number
25
type = raw_input("Is this a mobile, home, or work phone? ")
27
phone_number.type = addressbook_pb2.Person.MOBILE
29
phone_number.type = addressbook_pb2.Person.HOME
31
phone_number.type = addressbook_pb2.Person.WORK
33
print "Unknown phone type; leaving as default value."
35
# Main procedure: Reads the entire address book from a file,
36
# adds one person based on user input, then writes it back out to the same
38
if len(sys.argv) != 2:
39
print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
42
address_book = addressbook_pb2.AddressBook()
44
# Read the existing address book.
46
f = open(sys.argv[1], "rb")
47
address_book.ParseFromString(f.read())
50
print sys.argv[1] + ": File not found. Creating a new file."
53
PromptForAddress(address_book.person.add())
55
# Write the new address book back to disk.
56
f = open(sys.argv[1], "wb")
57
f.write(address_book.SerializeToString())