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 | |
cclauss | 067543c | 2017-08-18 01:14:48 +0200 | [diff] [blame] | 5 | from __future__ import print_function |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 6 | import addressbook_pb2 |
| 7 | import sys |
| 8 | |
cclauss | 067543c | 2017-08-18 01:14:48 +0200 | [diff] [blame] | 9 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 10 | # Iterates though all people in the AddressBook and prints info about them. |
| 11 | def ListPeople(address_book): |
Jan Tattermusch | b0e5ba6 | 2015-07-20 15:24:08 -0700 | [diff] [blame] | 12 | for person in address_book.people: |
cclauss | 067543c | 2017-08-18 01:14:48 +0200 | [diff] [blame] | 13 | print("Person ID:", person.id) |
| 14 | print(" Name:", person.name) |
Jan Tattermusch | b95670f | 2015-07-20 14:34:27 -0700 | [diff] [blame] | 15 | if person.email != "": |
cclauss | 067543c | 2017-08-18 01:14:48 +0200 | [diff] [blame] | 16 | print(" E-mail address:", person.email) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 17 | |
Jan Tattermusch | b95670f | 2015-07-20 14:34:27 -0700 | [diff] [blame] | 18 | for phone_number in person.phones: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 19 | if phone_number.type == addressbook_pb2.Person.MOBILE: |
cclauss | 067543c | 2017-08-18 01:14:48 +0200 | [diff] [blame] | 20 | print(" Mobile phone #:", end=" ") |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 21 | elif phone_number.type == addressbook_pb2.Person.HOME: |
cclauss | 067543c | 2017-08-18 01:14:48 +0200 | [diff] [blame] | 22 | print(" Home phone #:", end=" ") |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 23 | elif phone_number.type == addressbook_pb2.Person.WORK: |
cclauss | 067543c | 2017-08-18 01:14:48 +0200 | [diff] [blame] | 24 | print(" Work phone #:", end=" ") |
| 25 | print(phone_number.number) |
| 26 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 27 | |
| 28 | # Main procedure: Reads the entire address book from a file and prints all |
| 29 | # the information inside. |
| 30 | if len(sys.argv) != 2: |
cclauss | 067543c | 2017-08-18 01:14:48 +0200 | [diff] [blame] | 31 | print("Usage:", sys.argv[0], "ADDRESS_BOOK_FILE") |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 32 | sys.exit(-1) |
| 33 | |
| 34 | address_book = addressbook_pb2.AddressBook() |
| 35 | |
| 36 | # Read the existing address book. |
Parth Kolekar | 21e1f1d | 2016-01-26 04:13:31 +0530 | [diff] [blame] | 37 | with open(sys.argv[1], "rb") as f: |
| 38 | address_book.ParseFromString(f.read()) |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 39 | |
| 40 | ListPeople(address_book) |