Initial project structure with address book example from https://developers.google.com/protocol-buffers/docs/pythontutorial
This commit is contained in:
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "protobuf"]
|
||||||
|
path = protobuf
|
||||||
|
url = https://github.com/google/protobuf.git
|
||||||
13
Makefile
Normal file
13
Makefile
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
.PHONY = clean
|
||||||
|
|
||||||
|
PROTO_DIR = proto
|
||||||
|
GOOGLE_PROTOBUF_INCLUDES = protobuf\src
|
||||||
|
PROTO_INCLUDES = -I$(PROTO_DIR) -I$(GOOGLE_PROTOBUF_INCLUDES)
|
||||||
|
|
||||||
|
all: addressbook_pb2.py
|
||||||
|
|
||||||
|
%_pb2.py: $(PROTO_DIR)\%.proto
|
||||||
|
protoc --python_out=. $(PROTO_INCLUDES) $<
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf *_pb2.*
|
||||||
58
add_person.py
Normal file
58
add_person.py
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
#! /usr/bin/python
|
||||||
|
|
||||||
|
# See README.txt for information and build instructions.
|
||||||
|
|
||||||
|
import addressbook_pb2
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# This function fills in a Person message based on user input.
|
||||||
|
def PromptForAddress(person):
|
||||||
|
person.id = int(raw_input("Enter person ID number: "))
|
||||||
|
person.name = raw_input("Enter name: ")
|
||||||
|
|
||||||
|
email = raw_input("Enter email address (blank for none): ")
|
||||||
|
if email != "":
|
||||||
|
person.email = email
|
||||||
|
|
||||||
|
while True:
|
||||||
|
number = raw_input("Enter a phone number (or leave blank to finish): ")
|
||||||
|
if number == "":
|
||||||
|
break
|
||||||
|
|
||||||
|
phone_number = person.phone.add()
|
||||||
|
phone_number.number = number
|
||||||
|
|
||||||
|
type = raw_input("Is this a mobile, home, or work phone? ")
|
||||||
|
if type == "mobile":
|
||||||
|
phone_number.type = addressbook_pb2.Person.MOBILE
|
||||||
|
elif type == "home":
|
||||||
|
phone_number.type = addressbook_pb2.Person.HOME
|
||||||
|
elif type == "work":
|
||||||
|
phone_number.type = addressbook_pb2.Person.WORK
|
||||||
|
else:
|
||||||
|
print "Unknown phone type; leaving as default value."
|
||||||
|
|
||||||
|
# Main procedure: Reads the entire address book from a file,
|
||||||
|
# adds one person based on user input, then writes it back out to the same
|
||||||
|
# file.
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
|
||||||
|
sys.exit(-1)
|
||||||
|
|
||||||
|
address_book = addressbook_pb2.AddressBook()
|
||||||
|
|
||||||
|
# Read the existing address book.
|
||||||
|
try:
|
||||||
|
f = open(sys.argv[1], "rb")
|
||||||
|
address_book.ParseFromString(f.read())
|
||||||
|
f.close()
|
||||||
|
except IOError:
|
||||||
|
print sys.argv[1] + ": File not found. Creating a new file."
|
||||||
|
|
||||||
|
# Add an address.
|
||||||
|
PromptForAddress(address_book.person.add())
|
||||||
|
|
||||||
|
# Write the new address book back to disk.
|
||||||
|
f = open(sys.argv[1], "wb")
|
||||||
|
f.write(address_book.SerializeToString())
|
||||||
|
f.close()
|
||||||
204
addressbook_pb2.py
Normal file
204
addressbook_pb2.py
Normal file
@@ -0,0 +1,204 @@
|
|||||||
|
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||||
|
# source: addressbook.proto
|
||||||
|
|
||||||
|
import sys
|
||||||
|
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
|
||||||
|
from google.protobuf import descriptor as _descriptor
|
||||||
|
from google.protobuf import message as _message
|
||||||
|
from google.protobuf import reflection as _reflection
|
||||||
|
from google.protobuf import symbol_database as _symbol_database
|
||||||
|
from google.protobuf import descriptor_pb2
|
||||||
|
# @@protoc_insertion_point(imports)
|
||||||
|
|
||||||
|
_sym_db = _symbol_database.Default()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
DESCRIPTOR = _descriptor.FileDescriptor(
|
||||||
|
name='addressbook.proto',
|
||||||
|
package='tutorial',
|
||||||
|
serialized_pb=_b('\n\x11\x61\x64\x64ressbook.proto\x12\x08tutorial\"\xda\x01\n\x06Person\x12\x0c\n\x04name\x18\x01 \x02(\t\x12\n\n\x02id\x18\x02 \x02(\x05\x12\r\n\x05\x65mail\x18\x03 \x01(\t\x12+\n\x05phone\x18\x04 \x03(\x0b\x32\x1c.tutorial.Person.PhoneNumber\x1aM\n\x0bPhoneNumber\x12\x0e\n\x06number\x18\x01 \x02(\t\x12.\n\x04type\x18\x02 \x01(\x0e\x32\x1a.tutorial.Person.PhoneType:\x04HOME\"+\n\tPhoneType\x12\n\n\x06MOBILE\x10\x00\x12\x08\n\x04HOME\x10\x01\x12\x08\n\x04WORK\x10\x02\"/\n\x0b\x41\x64\x64ressBook\x12 \n\x06person\x18\x01 \x03(\x0b\x32\x10.tutorial.PersonB)\n\x14\x63om.example.tutorialB\x11\x41\x64\x64ressBookProtos')
|
||||||
|
)
|
||||||
|
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
_PERSON_PHONETYPE = _descriptor.EnumDescriptor(
|
||||||
|
name='PhoneType',
|
||||||
|
full_name='tutorial.Person.PhoneType',
|
||||||
|
filename=None,
|
||||||
|
file=DESCRIPTOR,
|
||||||
|
values=[
|
||||||
|
_descriptor.EnumValueDescriptor(
|
||||||
|
name='MOBILE', index=0, number=0,
|
||||||
|
options=None,
|
||||||
|
type=None),
|
||||||
|
_descriptor.EnumValueDescriptor(
|
||||||
|
name='HOME', index=1, number=1,
|
||||||
|
options=None,
|
||||||
|
type=None),
|
||||||
|
_descriptor.EnumValueDescriptor(
|
||||||
|
name='WORK', index=2, number=2,
|
||||||
|
options=None,
|
||||||
|
type=None),
|
||||||
|
],
|
||||||
|
containing_type=None,
|
||||||
|
options=None,
|
||||||
|
serialized_start=207,
|
||||||
|
serialized_end=250,
|
||||||
|
)
|
||||||
|
_sym_db.RegisterEnumDescriptor(_PERSON_PHONETYPE)
|
||||||
|
|
||||||
|
|
||||||
|
_PERSON_PHONENUMBER = _descriptor.Descriptor(
|
||||||
|
name='PhoneNumber',
|
||||||
|
full_name='tutorial.Person.PhoneNumber',
|
||||||
|
filename=None,
|
||||||
|
file=DESCRIPTOR,
|
||||||
|
containing_type=None,
|
||||||
|
fields=[
|
||||||
|
_descriptor.FieldDescriptor(
|
||||||
|
name='number', full_name='tutorial.Person.PhoneNumber.number', index=0,
|
||||||
|
number=1, type=9, cpp_type=9, label=2,
|
||||||
|
has_default_value=False, default_value=_b("").decode('utf-8'),
|
||||||
|
message_type=None, enum_type=None, containing_type=None,
|
||||||
|
is_extension=False, extension_scope=None,
|
||||||
|
options=None),
|
||||||
|
_descriptor.FieldDescriptor(
|
||||||
|
name='type', full_name='tutorial.Person.PhoneNumber.type', index=1,
|
||||||
|
number=2, type=14, cpp_type=8, label=1,
|
||||||
|
has_default_value=True, default_value=1,
|
||||||
|
message_type=None, enum_type=None, containing_type=None,
|
||||||
|
is_extension=False, extension_scope=None,
|
||||||
|
options=None),
|
||||||
|
],
|
||||||
|
extensions=[
|
||||||
|
],
|
||||||
|
nested_types=[],
|
||||||
|
enum_types=[
|
||||||
|
],
|
||||||
|
options=None,
|
||||||
|
is_extendable=False,
|
||||||
|
extension_ranges=[],
|
||||||
|
oneofs=[
|
||||||
|
],
|
||||||
|
serialized_start=128,
|
||||||
|
serialized_end=205,
|
||||||
|
)
|
||||||
|
|
||||||
|
_PERSON = _descriptor.Descriptor(
|
||||||
|
name='Person',
|
||||||
|
full_name='tutorial.Person',
|
||||||
|
filename=None,
|
||||||
|
file=DESCRIPTOR,
|
||||||
|
containing_type=None,
|
||||||
|
fields=[
|
||||||
|
_descriptor.FieldDescriptor(
|
||||||
|
name='name', full_name='tutorial.Person.name', index=0,
|
||||||
|
number=1, type=9, cpp_type=9, label=2,
|
||||||
|
has_default_value=False, default_value=_b("").decode('utf-8'),
|
||||||
|
message_type=None, enum_type=None, containing_type=None,
|
||||||
|
is_extension=False, extension_scope=None,
|
||||||
|
options=None),
|
||||||
|
_descriptor.FieldDescriptor(
|
||||||
|
name='id', full_name='tutorial.Person.id', index=1,
|
||||||
|
number=2, type=5, cpp_type=1, label=2,
|
||||||
|
has_default_value=False, default_value=0,
|
||||||
|
message_type=None, enum_type=None, containing_type=None,
|
||||||
|
is_extension=False, extension_scope=None,
|
||||||
|
options=None),
|
||||||
|
_descriptor.FieldDescriptor(
|
||||||
|
name='email', full_name='tutorial.Person.email', index=2,
|
||||||
|
number=3, type=9, cpp_type=9, label=1,
|
||||||
|
has_default_value=False, default_value=_b("").decode('utf-8'),
|
||||||
|
message_type=None, enum_type=None, containing_type=None,
|
||||||
|
is_extension=False, extension_scope=None,
|
||||||
|
options=None),
|
||||||
|
_descriptor.FieldDescriptor(
|
||||||
|
name='phone', full_name='tutorial.Person.phone', index=3,
|
||||||
|
number=4, type=11, cpp_type=10, label=3,
|
||||||
|
has_default_value=False, default_value=[],
|
||||||
|
message_type=None, enum_type=None, containing_type=None,
|
||||||
|
is_extension=False, extension_scope=None,
|
||||||
|
options=None),
|
||||||
|
],
|
||||||
|
extensions=[
|
||||||
|
],
|
||||||
|
nested_types=[_PERSON_PHONENUMBER, ],
|
||||||
|
enum_types=[
|
||||||
|
_PERSON_PHONETYPE,
|
||||||
|
],
|
||||||
|
options=None,
|
||||||
|
is_extendable=False,
|
||||||
|
extension_ranges=[],
|
||||||
|
oneofs=[
|
||||||
|
],
|
||||||
|
serialized_start=32,
|
||||||
|
serialized_end=250,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_ADDRESSBOOK = _descriptor.Descriptor(
|
||||||
|
name='AddressBook',
|
||||||
|
full_name='tutorial.AddressBook',
|
||||||
|
filename=None,
|
||||||
|
file=DESCRIPTOR,
|
||||||
|
containing_type=None,
|
||||||
|
fields=[
|
||||||
|
_descriptor.FieldDescriptor(
|
||||||
|
name='person', full_name='tutorial.AddressBook.person', index=0,
|
||||||
|
number=1, type=11, cpp_type=10, label=3,
|
||||||
|
has_default_value=False, default_value=[],
|
||||||
|
message_type=None, enum_type=None, containing_type=None,
|
||||||
|
is_extension=False, extension_scope=None,
|
||||||
|
options=None),
|
||||||
|
],
|
||||||
|
extensions=[
|
||||||
|
],
|
||||||
|
nested_types=[],
|
||||||
|
enum_types=[
|
||||||
|
],
|
||||||
|
options=None,
|
||||||
|
is_extendable=False,
|
||||||
|
extension_ranges=[],
|
||||||
|
oneofs=[
|
||||||
|
],
|
||||||
|
serialized_start=252,
|
||||||
|
serialized_end=299,
|
||||||
|
)
|
||||||
|
|
||||||
|
_PERSON_PHONENUMBER.fields_by_name['type'].enum_type = _PERSON_PHONETYPE
|
||||||
|
_PERSON_PHONENUMBER.containing_type = _PERSON
|
||||||
|
_PERSON.fields_by_name['phone'].message_type = _PERSON_PHONENUMBER
|
||||||
|
_PERSON_PHONETYPE.containing_type = _PERSON
|
||||||
|
_ADDRESSBOOK.fields_by_name['person'].message_type = _PERSON
|
||||||
|
DESCRIPTOR.message_types_by_name['Person'] = _PERSON
|
||||||
|
DESCRIPTOR.message_types_by_name['AddressBook'] = _ADDRESSBOOK
|
||||||
|
|
||||||
|
Person = _reflection.GeneratedProtocolMessageType('Person', (_message.Message,), dict(
|
||||||
|
|
||||||
|
PhoneNumber = _reflection.GeneratedProtocolMessageType('PhoneNumber', (_message.Message,), dict(
|
||||||
|
DESCRIPTOR = _PERSON_PHONENUMBER,
|
||||||
|
__module__ = 'addressbook_pb2'
|
||||||
|
# @@protoc_insertion_point(class_scope:tutorial.Person.PhoneNumber)
|
||||||
|
))
|
||||||
|
,
|
||||||
|
DESCRIPTOR = _PERSON,
|
||||||
|
__module__ = 'addressbook_pb2'
|
||||||
|
# @@protoc_insertion_point(class_scope:tutorial.Person)
|
||||||
|
))
|
||||||
|
_sym_db.RegisterMessage(Person)
|
||||||
|
_sym_db.RegisterMessage(Person.PhoneNumber)
|
||||||
|
|
||||||
|
AddressBook = _reflection.GeneratedProtocolMessageType('AddressBook', (_message.Message,), dict(
|
||||||
|
DESCRIPTOR = _ADDRESSBOOK,
|
||||||
|
__module__ = 'addressbook_pb2'
|
||||||
|
# @@protoc_insertion_point(class_scope:tutorial.AddressBook)
|
||||||
|
))
|
||||||
|
_sym_db.RegisterMessage(AddressBook)
|
||||||
|
|
||||||
|
|
||||||
|
DESCRIPTOR.has_options = True
|
||||||
|
DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), _b('\n\024com.example.tutorialB\021AddressBookProtos'))
|
||||||
|
# @@protoc_insertion_point(module_scope)
|
||||||
BIN
addressbook_pb2.pyc
Normal file
BIN
addressbook_pb2.pyc
Normal file
Binary file not shown.
38
list_people.py
Normal file
38
list_people.py
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
#! /usr/bin/python
|
||||||
|
|
||||||
|
# See README.txt for information and build instructions.
|
||||||
|
|
||||||
|
import addressbook_pb2
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# Iterates though all people in the AddressBook and prints info about them.
|
||||||
|
def ListPeople(address_book):
|
||||||
|
for person in address_book.person:
|
||||||
|
print "Person ID:", person.id
|
||||||
|
print " Name:", person.name
|
||||||
|
if person.HasField('email'):
|
||||||
|
print " E-mail address:", person.email
|
||||||
|
|
||||||
|
for phone_number in person.phone:
|
||||||
|
if phone_number.type == addressbook_pb2.Person.MOBILE:
|
||||||
|
print " Mobile phone #:",
|
||||||
|
elif phone_number.type == addressbook_pb2.Person.HOME:
|
||||||
|
print " Home phone #:",
|
||||||
|
elif phone_number.type == addressbook_pb2.Person.WORK:
|
||||||
|
print " Work phone #:",
|
||||||
|
print phone_number.number
|
||||||
|
|
||||||
|
# Main procedure: Reads the entire address book from a file and prints all
|
||||||
|
# the information inside.
|
||||||
|
if len(sys.argv) != 2:
|
||||||
|
print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
|
||||||
|
sys.exit(-1)
|
||||||
|
|
||||||
|
address_book = addressbook_pb2.AddressBook()
|
||||||
|
|
||||||
|
# Read the existing address book.
|
||||||
|
f = open(sys.argv[1], "rb")
|
||||||
|
address_book.ParseFromString(f.read())
|
||||||
|
f.close()
|
||||||
|
|
||||||
|
ListPeople(address_book)
|
||||||
30
proto/addressbook.proto
Normal file
30
proto/addressbook.proto
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
// See README.txt for information and build instructions.
|
||||||
|
|
||||||
|
package tutorial;
|
||||||
|
|
||||||
|
option java_package = "com.example.tutorial";
|
||||||
|
option java_outer_classname = "AddressBookProtos";
|
||||||
|
|
||||||
|
message Person {
|
||||||
|
required string name = 1;
|
||||||
|
required int32 id = 2; // Unique ID number for this person.
|
||||||
|
optional string email = 3;
|
||||||
|
|
||||||
|
enum PhoneType {
|
||||||
|
MOBILE = 0;
|
||||||
|
HOME = 1;
|
||||||
|
WORK = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
message PhoneNumber {
|
||||||
|
required string number = 1;
|
||||||
|
optional PhoneType type = 2 [default = HOME];
|
||||||
|
}
|
||||||
|
|
||||||
|
repeated PhoneNumber phone = 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Our address book file is just one of these.
|
||||||
|
message AddressBook {
|
||||||
|
repeated Person person = 1;
|
||||||
|
}
|
||||||
1
protobuf
Submodule
1
protobuf
Submodule
Submodule protobuf added at 9861c0d796
Reference in New Issue
Block a user