Tamir Duberstein | 9d9d0b7 | 2015-04-11 20:23:45 -0700 | [diff] [blame] | 1 | #! /usr/bin/env python |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2 | |
| 3 | # See README.txt for information and build instructions. |
| 4 | |
| 5 | import addressbook_pb2 |
| 6 | import sys |
| 7 | |
cclauss | 7daedbd | 2017-08-18 01:09:14 +0200 | [diff] [blame] | 8 | try: |
| 9 | raw_input # Python 2 |
| 10 | except NameError: |
| 11 | raw_input = input # Python 3 |
| 12 | |
| 13 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 14 | # This function fills in a Person message based on user input. |
| 15 | def PromptForAddress(person): |
| 16 | person.id = int(raw_input("Enter person ID number: ")) |
| 17 | person.name = raw_input("Enter name: ") |
| 18 | |
| 19 | email = raw_input("Enter email address (blank for none): ") |
| 20 | if email != "": |
| 21 | person.email = email |
| 22 | |
| 23 | while True: |
| 24 | number = raw_input("Enter a phone number (or leave blank to finish): ") |
| 25 | if number == "": |
| 26 | break |
| 27 | |
Jan Tattermusch | b95670f | 2015-07-20 14:34:27 -0700 | [diff] [blame] | 28 | phone_number = person.phones.add() |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 29 | phone_number.number = number |
| 30 | |
| 31 | type = raw_input("Is this a mobile, home, or work phone? ") |
| 32 | if type == "mobile": |
| 33 | phone_number.type = addressbook_pb2.Person.MOBILE |
| 34 | elif type == "home": |
| 35 | phone_number.type = addressbook_pb2.Person.HOME |
| 36 | elif type == "work": |
| 37 | phone_number.type = addressbook_pb2.Person.WORK |
| 38 | else: |
cclauss | 7daedbd | 2017-08-18 01:09:14 +0200 | [diff] [blame] | 39 | print("Unknown phone type; leaving as default value.") |
| 40 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 41 | |
| 42 | # Main procedure: Reads the entire address book from a file, |
| 43 | # adds one person based on user input, then writes it back out to the same |
| 44 | # file. |
| 45 | if len(sys.argv) != 2: |
cclauss | 7daedbd | 2017-08-18 01:09:14 +0200 | [diff] [blame] | 46 | print("Usage:", sys.argv[0], "ADDRESS_BOOK_FILE") |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 47 | sys.exit(-1) |
| 48 | |
| 49 | address_book = addressbook_pb2.AddressBook() |
| 50 | |
| 51 | # Read the existing address book. |
| 52 | try: |
Parth Kolekar | 21e1f1d | 2016-01-26 04:13:31 +0530 | [diff] [blame] | 53 | with open(sys.argv[1], "rb") as f: |
| 54 | address_book.ParseFromString(f.read()) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 55 | except IOError: |
cclauss | 7daedbd | 2017-08-18 01:09:14 +0200 | [diff] [blame] | 56 | print(sys.argv[1] + ": File not found. Creating a new file.") |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 57 | |
| 58 | # Add an address. |
Jan Tattermusch | b0e5ba6 | 2015-07-20 15:24:08 -0700 | [diff] [blame] | 59 | PromptForAddress(address_book.people.add()) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 60 | |
| 61 | # Write the new address book back to disk. |
Parth Kolekar | 21e1f1d | 2016-01-26 04:13:31 +0530 | [diff] [blame] | 62 | with open(sys.argv[1], "wb") as f: |
| 63 | f.write(address_book.SerializeToString()) |