• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env python
2
3# See README.txt for information and build instructions.
4
5from __future__ import print_function
6import addressbook_pb2
7import sys
8
9
10# Iterates though all people in the AddressBook and prints info about them.
11def ListPeople(address_book):
12  for person in address_book.people:
13    print("Person ID:", person.id)
14    print("  Name:", person.name)
15    if person.email != "":
16      print("  E-mail address:", person.email)
17
18    for phone_number in person.phones:
19      if phone_number.type == addressbook_pb2.Person.MOBILE:
20        print("  Mobile phone #:", end=" ")
21      elif phone_number.type == addressbook_pb2.Person.HOME:
22        print("  Home phone #:", end=" ")
23      elif phone_number.type == addressbook_pb2.Person.WORK:
24        print("  Work phone #:", end=" ")
25      print(phone_number.number)
26
27
28# Main procedure:  Reads the entire address book from a file and prints all
29#   the information inside.
30if len(sys.argv) != 2:
31  print("Usage:", sys.argv[0], "ADDRESS_BOOK_FILE")
32  sys.exit(-1)
33
34address_book = addressbook_pb2.AddressBook()
35
36# Read the existing address book.
37with open(sys.argv[1], "rb") as f:
38  address_book.ParseFromString(f.read())
39
40ListPeople(address_book)
41