blob: 0b6985792d7a5fcef545a44d085d929dab989c4e [file] [log] [blame]
Tamir Duberstein9d9d0b72015-04-11 20:23:45 -07001#! /usr/bin/env python
temporal40ee5512008-07-10 02:12:20 +00002
3# See README.txt for information and build instructions.
4
5import addressbook_pb2
6import sys
7
8# This function fills in a Person message based on user input.
9def PromptForAddress(person):
10 person.id = int(raw_input("Enter person ID number: "))
11 person.name = raw_input("Enter name: ")
12
13 email = raw_input("Enter email address (blank for none): ")
14 if email != "":
15 person.email = email
16
17 while True:
18 number = raw_input("Enter a phone number (or leave blank to finish): ")
19 if number == "":
20 break
21
Jan Tattermuschb95670f2015-07-20 14:34:27 -070022 phone_number = person.phones.add()
temporal40ee5512008-07-10 02:12:20 +000023 phone_number.number = number
24
25 type = raw_input("Is this a mobile, home, or work phone? ")
26 if type == "mobile":
27 phone_number.type = addressbook_pb2.Person.MOBILE
28 elif type == "home":
29 phone_number.type = addressbook_pb2.Person.HOME
30 elif type == "work":
31 phone_number.type = addressbook_pb2.Person.WORK
32 else:
33 print "Unknown phone type; leaving as default value."
34
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
37# file.
38if len(sys.argv) != 2:
39 print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
40 sys.exit(-1)
41
42address_book = addressbook_pb2.AddressBook()
43
44# Read the existing address book.
45try:
Parth Kolekar21e1f1d2016-01-26 04:13:31 +053046 with open(sys.argv[1], "rb") as f:
47 address_book.ParseFromString(f.read())
temporal40ee5512008-07-10 02:12:20 +000048except IOError:
49 print sys.argv[1] + ": File not found. Creating a new file."
50
51# Add an address.
Jan Tattermuschb0e5ba62015-07-20 15:24:08 -070052PromptForAddress(address_book.people.add())
temporal40ee5512008-07-10 02:12:20 +000053
54# Write the new address book back to disk.
Parth Kolekar21e1f1d2016-01-26 04:13:31 +053055with open(sys.argv[1], "wb") as f:
56 f.write(address_book.SerializeToString())