PageRenderTime 28ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/examples/add_person.py

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