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