Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/protobuf/internal/builder.py: 80%
51 statements
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:37 +0000
« prev ^ index » next coverage.py v7.3.1, created at 2023-09-25 06:37 +0000
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.
31"""Builds descriptors, message classes and services for generated _pb2.py.
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"""
38__author__ = 'jieluo@google.com (Jie Luo)'
40from google.protobuf.internal import enum_type_wrapper
41from google.protobuf.internal import python_message
42from google.protobuf import message as _message
43from google.protobuf import reflection as _reflection
44from google.protobuf import symbol_database as _symbol_database
46_sym_db = _symbol_database.Default()
49def BuildMessageAndEnumDescriptors(file_des, module):
50 """Builds message and enum descriptors.
52 Args:
53 file_des: FileDescriptor of the .proto file
54 module: Generated _pb2 module
55 """
57 def BuildNestedDescriptors(msg_des, prefix):
58 for (name, nested_msg) in msg_des.nested_types_by_name.items():
59 module_name = prefix + name.upper()
60 module[module_name] = nested_msg
61 BuildNestedDescriptors(nested_msg, module_name + '_')
62 for enum_des in msg_des.enum_types:
63 module[prefix + enum_des.name.upper()] = enum_des
65 for (name, msg_des) in file_des.message_types_by_name.items():
66 module_name = '_' + name.upper()
67 module[module_name] = msg_des
68 BuildNestedDescriptors(msg_des, module_name + '_')
71def BuildTopDescriptorsAndMessages(file_des, module_name, module):
72 """Builds top level descriptors and message classes.
74 Args:
75 file_des: FileDescriptor of the .proto file
76 module_name: str, the name of generated _pb2 module
77 module: Generated _pb2 module
78 """
80 def BuildMessage(msg_des):
81 create_dict = {}
82 for (name, nested_msg) in msg_des.nested_types_by_name.items():
83 create_dict[name] = BuildMessage(nested_msg)
84 create_dict['DESCRIPTOR'] = msg_des
85 create_dict['__module__'] = module_name
86 message_class = _reflection.GeneratedProtocolMessageType(
87 msg_des.name, (_message.Message,), create_dict)
88 _sym_db.RegisterMessage(message_class)
89 return message_class
91 # top level enums
92 for (name, enum_des) in file_des.enum_types_by_name.items():
93 module['_' + name.upper()] = enum_des
94 module[name] = enum_type_wrapper.EnumTypeWrapper(enum_des)
95 for enum_value in enum_des.values:
96 module[enum_value.name] = enum_value.number
98 # top level extensions
99 for (name, extension_des) in file_des.extensions_by_name.items():
100 module[name.upper() + '_FIELD_NUMBER'] = extension_des.number
101 module[name] = extension_des
103 # services
104 for (name, service) in file_des.services_by_name.items():
105 module['_' + name.upper()] = service
107 # Build messages.
108 for (name, msg_des) in file_des.message_types_by_name.items():
109 module[name] = BuildMessage(msg_des)
112def AddHelpersToExtensions(file_des):
113 """no-op to keep old generated code work with new runtime.
115 Args:
116 file_des: FileDescriptor of the .proto file
117 """
118 # TODO(b/279930766): Remove this on-op
119 return
122def BuildServices(file_des, module_name, module):
123 """Builds services classes and services stub class.
125 Args:
126 file_des: FileDescriptor of the .proto file
127 module_name: str, the name of generated _pb2 module
128 module: Generated _pb2 module
129 """
130 # pylint: disable=g-import-not-at-top
131 from google.protobuf import service as _service
132 from google.protobuf import service_reflection
133 # pylint: enable=g-import-not-at-top
134 for (name, service) in file_des.services_by_name.items():
135 module[name] = service_reflection.GeneratedServiceType(
136 name, (_service.Service,),
137 dict(DESCRIPTOR=service, __module__=module_name))
138 stub_name = name + '_Stub'
139 module[stub_name] = service_reflection.GeneratedServiceStubType(
140 stub_name, (module[name],),
141 dict(DESCRIPTOR=service, __module__=module_name))