blob: d2c294c6f5cb8b5524835df1b7fa0f505b04819f [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
cclauss067543c2017-08-18 01:14:48 +02005from __future__ import print_function
temporal40ee5512008-07-10 02:12:20 +00006import addressbook_pb2
7import sys
8
cclauss067543c2017-08-18 01:14:48 +02009
temporal40ee5512008-07-10 02:12:20 +000010# Iterates though all people in the AddressBook and prints info about them.
11def ListPeople(address_book):
Jan Tattermuschb0e5ba62015-07-20 15:24:08 -070012 for person in address_book.people:
cclauss067543c2017-08-18 01:14:48 +020013 print("Person ID:", person.id)
14 print(" Name:", person.name)
Jan Tattermuschb95670f2015-07-20 14:34:27 -070015 if person.email != "":
cclauss067543c2017-08-18 01:14:48 +020016 print(" E-mail address:", person.email)
temporal40ee5512008-07-10 02:12:20 +000017
Jan Tattermuschb95670f2015-07-20 14:34:27 -070018 for phone_number in person.phones:
temporal40ee5512008-07-10 02:12:20 +000019 if phone_number.type == addressbook_pb2.Person.MOBILE:
cclauss067543c2017-08-18 01:14:48 +020020 print(" Mobile phone #:", end=" ")
temporal40ee5512008-07-10 02:12:20 +000021 elif phone_number.type == addressbook_pb2.Person.HOME:
cclauss067543c2017-08-18 01:14:48 +020022 print(" Home phone #:", end=" ")
temporal40ee5512008-07-10 02:12:20 +000023 elif phone_number.type == addressbook_pb2.Person.WORK:
cclauss067543c2017-08-18 01:14:48 +020024 print(" Work phone #:", end=" ")
25 print(phone_number.number)
26
temporal40ee5512008-07-10 02:12:20 +000027
28# Main procedure: Reads the entire address book from a file and prints all
29# the information inside.
30if len(sys.argv) != 2:
cclauss067543c2017-08-18 01:14:48 +020031 print("Usage:", sys.argv[0], "ADDRESS_BOOK_FILE")
temporal40ee5512008-07-10 02:12:20 +000032 sys.exit(-1)
33
34address_book = addressbook_pb2.AddressBook()
35
36# Read the existing address book.
Parth Kolekar21e1f1d2016-01-26 04:13:31 +053037with open(sys.argv[1], "rb") as f:
38 address_book.ParseFromString(f.read())
temporal40ee5512008-07-10 02:12:20 +000039
40ListPeople(address_book)