1# Protocol Buffers - Google's data interchange format 2# Copyright 2008 Google Inc. All rights reserved. 3# https://developers.google.com/protocol-buffers/ 4# 5# Redistribution and use in source and binary forms, with or without 6# modification, are permitted provided that the following conditions are 7# met: 8# 9# * Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above 12# copyright notice, this list of conditions and the following disclaimer 13# in the documentation and/or other materials provided with the 14# distribution. 15# * Neither the name of Google Inc. nor the names of its 16# contributors may be used to endorse or promote products derived from 17# this software without specific prior written permission. 18# 19# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 31"""Builds descriptors, message classes and services for generated _pb2.py. 32 33This file is only called in python generated _pb2.py files. It builds 34descriptors, message classes and services that users can directly use 35in generated code. 36""" 37 38__author__ = '[email protected] (Jie Luo)' 39 40from google.protobuf.internal import enum_type_wrapper 41from google.protobuf import message as _message 42from google.protobuf import reflection as _reflection 43from google.protobuf import symbol_database as _symbol_database 44 45_sym_db = _symbol_database.Default() 46 47 48def BuildMessageAndEnumDescriptors(file_des, module): 49 """Builds message and enum descriptors. 50 51 Args: 52 file_des: FileDescriptor of the .proto file 53 module: Generated _pb2 module 54 """ 55 56 def BuildNestedDescriptors(msg_des, prefix): 57 for (name, nested_msg) in msg_des.nested_types_by_name.items(): 58 module_name = prefix + name.upper() 59 module[module_name] = nested_msg 60 BuildNestedDescriptors(nested_msg, module_name + '_') 61 for enum_des in msg_des.enum_types: 62 module[prefix + enum_des.name.upper()] = enum_des 63 64 for (name, msg_des) in file_des.message_types_by_name.items(): 65 module_name = '_' + name.upper() 66 module[module_name] = msg_des 67 BuildNestedDescriptors(msg_des, module_name + '_') 68 69 70def BuildTopDescriptorsAndMessages(file_des, module_name, module): 71 """Builds top level descriptors and message classes. 72 73 Args: 74 file_des: FileDescriptor of the .proto file 75 module_name: str, the name of generated _pb2 module 76 module: Generated _pb2 module 77 """ 78 79 def BuildMessage(msg_des): 80 create_dict = {} 81 for (name, nested_msg) in msg_des.nested_types_by_name.items(): 82 create_dict[name] = BuildMessage(nested_msg) 83 create_dict['DESCRIPTOR'] = msg_des 84 create_dict['__module__'] = module_name 85 message_class = _reflection.GeneratedProtocolMessageType( 86 msg_des.name, (_message.Message,), create_dict) 87 _sym_db.RegisterMessage(message_class) 88 return message_class 89 90 # top level enums 91 for (name, enum_des) in file_des.enum_types_by_name.items(): 92 module['_' + name.upper()] = enum_des 93 module[name] = enum_type_wrapper.EnumTypeWrapper(enum_des) 94 for enum_value in enum_des.values: 95 module[enum_value.name] = enum_value.number 96 97 # top level extensions 98 for (name, extension_des) in file_des.extensions_by_name.items(): 99 module[name.upper() + '_FIELD_NUMBER'] = extension_des.number 100 module[name] = extension_des 101 102 # services 103 for (name, service) in file_des.services_by_name.items(): 104 module['_' + name.upper()] = service 105 106 # Build messages. 107 for (name, msg_des) in file_des.message_types_by_name.items(): 108 module[name] = BuildMessage(msg_des) 109 110 111def BuildServices(file_des, module_name, module): 112 """Builds services classes and services stub class. 113 114 Args: 115 file_des: FileDescriptor of the .proto file 116 module_name: str, the name of generated _pb2 module 117 module: Generated _pb2 module 118 """ 119 # pylint: disable=g-import-not-at-top 120 from google.protobuf import service as _service 121 from google.protobuf import service_reflection 122 # pylint: enable=g-import-not-at-top 123 for (name, service) in file_des.services_by_name.items(): 124 module[name] = service_reflection.GeneratedServiceType( 125 name, (_service.Service,), 126 dict(DESCRIPTOR=service, __module__=module_name)) 127 stub_name = name + '_Stub' 128 module[stub_name] = service_reflection.GeneratedServiceStubType( 129 stub_name, (module[name],), 130 dict(DESCRIPTOR=service, __module__=module_name)) 131