diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/CMakeLists.txt | 17 | ||||
-rw-r--r-- | examples/Makefile | 2 | ||||
-rw-r--r-- | examples/README.txt | 7 | ||||
-rwxr-xr-x | examples/add_person.py | 13 | ||||
-rwxr-xr-x | examples/list_people.py | 19 |
5 files changed, 30 insertions, 28 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 2cd2acc0..3e8e6541 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -32,22 +32,6 @@ foreach(example add_person list_people) if(protobuf_MODULE_COMPATIBLE) #Legacy Support protobuf_generate_cpp(${example}_PROTO_SRCS ${example}_PROTO_HDRS ${${example}_PROTOS}) list(APPEND ${example}_SRCS ${${example}_PROTO_SRCS} ${${example}_PROTO_HDRS}) - else() - - foreach(proto_file ${${example}_PROTOS}) - get_filename_component(proto_file_abs ${proto_file} ABSOLUTE) - get_filename_component(basename ${proto_file} NAME_WE) - set(generated_files ${basename}.pb.cc ${basename}.pb.h) - list(APPEND ${example}_SRCS ${generated_files}) - - add_custom_command( - OUTPUT ${generated_files} - COMMAND protobuf::protoc - ARGS --cpp_out ${CMAKE_CURRENT_BINARY_DIR} -I ${CMAKE_CURRENT_SOURCE_DIR} ${proto_file_abs} - COMMENT "Generating ${generated_files} from ${proto_file}" - VERBATIM - ) - endforeach() endif() #Executable setup @@ -58,6 +42,7 @@ foreach(example add_person list_people) target_link_libraries(${executable_name} ${PROTOBUF_LIBRARIES}) else() target_link_libraries(${executable_name} protobuf::libprotobuf) + protobuf_generate(TARGET ${executable_name}) endif() endforeach() diff --git a/examples/Makefile b/examples/Makefile index 51f13426..d16bb56d 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -51,7 +51,7 @@ list_people_gotest: list_people.go list_people_go go test list_people.go list_people_test.go javac_middleman: AddPerson.java ListPeople.java protoc_middleman - javac AddPerson.java ListPeople.java com/example/tutorial/AddressBookProtos.java + javac -cp ../java/core/target/*.jar AddPerson.java ListPeople.java com/example/tutorial/AddressBookProtos.java @touch javac_middleman add_person_java: javac_middleman diff --git a/examples/README.txt b/examples/README.txt index b33f8414..2e4f6e4e 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -28,6 +28,13 @@ These examples are part of the Protocol Buffers tutorial, located at: "-lpthread" from the linker commands (perhaps replacing it with something else). We didn't do this automatically because we wanted to keep the example simple. +## Java ## + +protobuf-java-*.jar can be generated by: + cd ../java + mvn package +and will be used by "make java" + ## Go ## The Go example requires a plugin to the protocol buffer compiler, so it is not diff --git a/examples/add_person.py b/examples/add_person.py index 0b698579..aa0fbca7 100755 --- a/examples/add_person.py +++ b/examples/add_person.py @@ -5,6 +5,12 @@ import addressbook_pb2 import sys +try: + raw_input # Python 2 +except NameError: + raw_input = input # Python 3 + + # This function fills in a Person message based on user input. def PromptForAddress(person): person.id = int(raw_input("Enter person ID number: ")) @@ -30,13 +36,14 @@ def PromptForAddress(person): elif type == "work": phone_number.type = addressbook_pb2.Person.WORK else: - print "Unknown phone type; leaving as default value." + 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" + print("Usage:", sys.argv[0], "ADDRESS_BOOK_FILE") sys.exit(-1) address_book = addressbook_pb2.AddressBook() @@ -46,7 +53,7 @@ try: with open(sys.argv[1], "rb") as f: address_book.ParseFromString(f.read()) except IOError: - print sys.argv[1] + ": File not found. Creating a new file." + print(sys.argv[1] + ": File not found. Creating a new file.") # Add an address. PromptForAddress(address_book.people.add()) diff --git a/examples/list_people.py b/examples/list_people.py index f131872d..d2c294c6 100755 --- a/examples/list_people.py +++ b/examples/list_people.py @@ -2,30 +2,33 @@ # See README.txt for information and build instructions. +from __future__ import print_function 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.people: - print "Person ID:", person.id - print " Name:", person.name + print("Person ID:", person.id) + print(" Name:", person.name) if person.email != "": - print " E-mail address:", person.email + print(" E-mail address:", person.email) for phone_number in person.phones: if phone_number.type == addressbook_pb2.Person.MOBILE: - print " Mobile phone #:", + print(" Mobile phone #:", end=" ") elif phone_number.type == addressbook_pb2.Person.HOME: - print " Home phone #:", + print(" Home phone #:", end=" ") elif phone_number.type == addressbook_pb2.Person.WORK: - print " Work phone #:", - print phone_number.number + print(" Work phone #:", end=" ") + 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" + print("Usage:", sys.argv[0], "ADDRESS_BOOK_FILE") sys.exit(-1) address_book = addressbook_pb2.AddressBook() |