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