31 """Descriptors essentially contain exactly the information found in a .proto
32 file, in types that make this information accessible in Python.
35 __author__ =
'robinson@google.com (Will Robinson)'
42 _USE_C_DESCRIPTORS =
False
43 if api_implementation.Type() ==
'cpp':
48 _USE_C_DESCRIPTORS =
True
51 class Error(Exception):
52 """Base error for this module."""
55 class TypeTransformationError(Error):
56 """Error transforming between python proto type and corresponding C++ type."""
59 if _USE_C_DESCRIPTORS:
68 if isinstance(obj, cls._C_DESCRIPTOR_CLASS):
73 DescriptorMetaclass = type
77 """Wrapper class of threading.Lock(), which is allowed by 'with'."""
80 self = object.__new__(cls)
81 self.
_lock = threading.Lock()
87 def __exit__(self, exc_type, exc_value, exc_tb):
91 _lock = threading.Lock()
95 if _Deprecated.count > 0:
96 _Deprecated.count -= 1
98 'Call to deprecated create function %s(). Note: Create unlinked '
99 'descriptors is going to go away. Please use get/find descriptors from '
100 'generated code or query the descriptor_pool.'
102 category=DeprecationWarning, stacklevel=3)
107 _Deprecated.count = 100
110 _internal_create_key = object()
115 """Descriptors base class.
117 This class is the base of all descriptor classes. It provides common options
118 related functionality.
121 has_options: True if the descriptor has non-default options. Usually it
122 is not necessary to read this -- just call GetOptions() which will
123 happily return the default instance. However, it's sometimes useful
124 for efficiency, and also useful inside the protobuf implementation to
125 avoid some bootstrapping issues.
128 if _USE_C_DESCRIPTORS:
131 _C_DESCRIPTOR_CLASS = ()
133 def __init__(self, options, serialized_options, options_class_name):
134 """Initialize the descriptor given its options message and the name of the
135 class of the options message. The name of the class is required in case
136 the options message is None and has to be created.
143 self.
has_options = (options
is not None)
or (serialized_options
is not None)
146 """Sets the descriptor's options
148 This function is used in generated proto2 files to update descriptor
149 options. It must not be used outside proto2.
158 """Retrieves descriptor options.
160 This method returns the options set or creates the default options for the
168 options_class = getattr(descriptor_pb2,
170 except AttributeError:
171 raise RuntimeError(
'Unknown options class name %s!' %
185 """Common class for descriptors that can be nested."""
187 def __init__(self, options, options_class_name, name, full_name,
188 file, containing_type, serialized_start=None,
189 serialized_end=None, serialized_options=None):
193 options: Protocol message options or None
194 to use default message options.
195 options_class_name (str): The class name of the above options.
196 name (str): Name of this protocol message type.
197 full_name (str): Fully-qualified name of this protocol message type,
198 which will include protocol "package" name and the name of any
200 file (FileDescriptor): Reference to file info.
201 containing_type: if provided, this is a nested descriptor, with this
202 descriptor as parent, otherwise None.
203 serialized_start: The start index (inclusive) in block in the
204 file.serialized_pb that describes this descriptor.
205 serialized_end: The end index (exclusive) in block in the
206 file.serialized_pb that describes this descriptor.
207 serialized_options: Protocol message serialized options or None.
209 super(_NestedDescriptorBase, self).
__init__(
210 options, serialized_options, options_class_name)
223 """Copies this to the matching proto in descriptor_pb2.
226 proto: An empty proto instance from descriptor_pb2.
229 Error: If self couldn't be serialized, due to to few constructor
232 if (self.
file is not None and
235 proto.ParseFromString(self.
file.serialized_pb[
238 raise Error(
'Descriptor does not contain serialization.')
243 """Descriptor for a protocol message type.
246 name (str): Name of this protocol message type.
247 full_name (str): Fully-qualified name of this protocol message type,
248 which will include protocol "package" name and the name of any
250 containing_type (Descriptor): Reference to the descriptor of the type
251 containing us, or None if this is top-level.
252 fields (list[FieldDescriptor]): Field descriptors for all fields in
254 fields_by_number (dict(int, FieldDescriptor)): Same
255 :class:`FieldDescriptor` objects as in :attr:`fields`, but indexed
256 by "number" attribute in each FieldDescriptor.
257 fields_by_name (dict(str, FieldDescriptor)): Same
258 :class:`FieldDescriptor` objects as in :attr:`fields`, but indexed by
259 "name" attribute in each :class:`FieldDescriptor`.
260 nested_types (list[Descriptor]): Descriptor references
261 for all protocol message types nested within this one.
262 nested_types_by_name (dict(str, Descriptor)): Same Descriptor
263 objects as in :attr:`nested_types`, but indexed by "name" attribute
265 enum_types (list[EnumDescriptor]): :class:`EnumDescriptor` references
266 for all enums contained within this type.
267 enum_types_by_name (dict(str, EnumDescriptor)): Same
268 :class:`EnumDescriptor` objects as in :attr:`enum_types`, but
269 indexed by "name" attribute in each EnumDescriptor.
270 enum_values_by_name (dict(str, EnumValueDescriptor)): Dict mapping
271 from enum value name to :class:`EnumValueDescriptor` for that value.
272 extensions (list[FieldDescriptor]): All extensions defined directly
273 within this message type (NOT within a nested type).
274 extensions_by_name (dict(str, FieldDescriptor)): Same FieldDescriptor
275 objects as :attr:`extensions`, but indexed by "name" attribute of each
277 is_extendable (bool): Does this type define any extension ranges?
278 oneofs (list[OneofDescriptor]): The list of descriptors for oneof fields
280 oneofs_by_name (dict(str, OneofDescriptor)): Same objects as in
281 :attr:`oneofs`, but indexed by "name" attribute.
282 file (FileDescriptor): Reference to file descriptor.
286 if _USE_C_DESCRIPTORS:
287 _C_DESCRIPTOR_CLASS = _message.Descriptor
294 containing_type=None,
300 serialized_options=None,
302 extension_ranges=None,
305 serialized_start=None,
309 _message.Message._CheckCalledFromGeneratedFile()
310 return _message.default_pool.FindMessageTypeByName(full_name)
315 def __init__(self, name, full_name, filename, containing_type, fields,
316 nested_types, enum_types, extensions, options=None,
317 serialized_options=None,
318 is_extendable=True, extension_ranges=None, oneofs=None,
319 file=None, serialized_start=None, serialized_end=None,
320 syntax=None, create_key=None):
321 """Arguments to __init__() are as described in the description
322 of Descriptor fields above.
324 Note that filename is an obsolete argument, that is not used anymore.
325 Please use file.name to access this as an attribute.
327 if create_key
is not _internal_create_key:
331 options,
'MessageOptions', name, full_name, file,
332 containing_type, serialized_start=serialized_start,
333 serialized_end=serialized_end, serialized_options=serialized_options)
342 field.containing_type = self
348 for nested_type
in nested_types:
349 nested_type.containing_type = self
354 enum_type.containing_type = self
357 (v.name, v)
for t
in enum_types
for v
in t.values)
361 extension.extension_scope = self
365 self.
oneofs = oneofs
if oneofs
is not None else []
368 oneof.containing_type = self
369 self.
syntax = syntax
or "proto2"
373 """Same FieldDescriptor objects as in :attr:`fields`, but indexed by
374 :attr:`FieldDescriptor.camelcase_name`.
378 (f.camelcase_name, f)
for f
in self.
fields)
382 """Returns the string name of an enum value.
384 This is just a small helper method to simplify a common operation.
387 enum: string name of the Enum.
388 value: int, value of the enum.
391 string name of the enum value.
394 KeyError if either the Enum doesn't exist or the value is not a valid
400 """Copies this to a descriptor_pb2.DescriptorProto.
403 proto: An empty descriptor_pb2.DescriptorProto.
422 """Descriptor for a single field in a .proto file.
425 name (str): Name of this field, exactly as it appears in .proto.
426 full_name (str): Name of this field, including containing scope. This is
427 particularly relevant for extensions.
428 index (int): Dense, 0-indexed index giving the order that this
429 field textually appears within its message in the .proto file.
430 number (int): Tag number declared for this field in the .proto file.
432 type (int): (One of the TYPE_* constants below) Declared type.
433 cpp_type (int): (One of the CPPTYPE_* constants below) C++ type used to
434 represent this field.
436 label (int): (One of the LABEL_* constants below) Tells whether this
437 field is optional, required, or repeated.
438 has_default_value (bool): True if this field has a default value defined,
440 default_value (Varies): Default value of this field. Only
441 meaningful for non-repeated scalar fields. Repeated fields
442 should always set this to [], and non-repeated composite
443 fields should always set this to None.
445 containing_type (Descriptor): Descriptor of the protocol message
446 type that contains this field. Set by the Descriptor constructor
447 if we're passed into one.
448 Somewhat confusingly, for extension fields, this is the
449 descriptor of the EXTENDED message, not the descriptor
450 of the message containing this field. (See is_extension and
451 extension_scope below).
452 message_type (Descriptor): If a composite field, a descriptor
453 of the message type contained in this field. Otherwise, this is None.
454 enum_type (EnumDescriptor): If this field contains an enum, a
455 descriptor of that enum. Otherwise, this is None.
457 is_extension: True iff this describes an extension field.
458 extension_scope (Descriptor): Only meaningful if is_extension is True.
459 Gives the message that immediately contains this extension field.
460 Will be None iff we're a top-level (file-level) extension field.
462 options (descriptor_pb2.FieldOptions): Protocol message field options or
463 None to use default field options.
465 containing_oneof (OneofDescriptor): If the field is a member of a oneof
466 union, contains its descriptor. Otherwise, None.
468 file (FileDescriptor): Reference to file descriptor.
511 _PYTHON_TO_CPP_PROTO_TYPE_MAP = {
512 TYPE_DOUBLE: CPPTYPE_DOUBLE,
513 TYPE_FLOAT: CPPTYPE_FLOAT,
514 TYPE_ENUM: CPPTYPE_ENUM,
515 TYPE_INT64: CPPTYPE_INT64,
516 TYPE_SINT64: CPPTYPE_INT64,
517 TYPE_SFIXED64: CPPTYPE_INT64,
518 TYPE_UINT64: CPPTYPE_UINT64,
519 TYPE_FIXED64: CPPTYPE_UINT64,
520 TYPE_INT32: CPPTYPE_INT32,
521 TYPE_SFIXED32: CPPTYPE_INT32,
522 TYPE_SINT32: CPPTYPE_INT32,
523 TYPE_UINT32: CPPTYPE_UINT32,
524 TYPE_FIXED32: CPPTYPE_UINT32,
525 TYPE_BYTES: CPPTYPE_STRING,
526 TYPE_STRING: CPPTYPE_STRING,
527 TYPE_BOOL: CPPTYPE_BOOL,
528 TYPE_MESSAGE: CPPTYPE_MESSAGE,
529 TYPE_GROUP: CPPTYPE_MESSAGE
543 MAX_FIELD_NUMBER = (1 << 29) - 1
544 FIRST_RESERVED_FIELD_NUMBER = 19000
545 LAST_RESERVED_FIELD_NUMBER = 19999
547 if _USE_C_DESCRIPTORS:
548 _C_DESCRIPTOR_CLASS = _message.FieldDescriptor
550 def __new__(cls, name, full_name, index, number, type, cpp_type, label,
551 default_value, message_type, enum_type, containing_type,
552 is_extension, extension_scope, options=None,
553 serialized_options=None,
554 has_default_value=True, containing_oneof=None, json_name=None,
555 file=None, create_key=None):
556 _message.Message._CheckCalledFromGeneratedFile()
558 return _message.default_pool.FindExtensionByName(full_name)
560 return _message.default_pool.FindFieldByName(full_name)
562 def __init__(self, name, full_name, index, number, type, cpp_type, label,
563 default_value, message_type, enum_type, containing_type,
564 is_extension, extension_scope, options=None,
565 serialized_options=None,
566 has_default_value=True, containing_oneof=None, json_name=None,
567 file=None, create_key=None):
568 """The arguments are as described in the description of FieldDescriptor
571 Note that containing_type may be None, and may be set later if necessary
572 (to deal with circular references between message types, for example).
573 Likewise for extension_scope.
575 if create_key
is not _internal_create_key:
578 super(FieldDescriptor, self).
__init__(
579 options, serialized_options,
'FieldOptions')
584 if json_name
is None:
601 if api_implementation.Type() ==
'cpp':
603 self.
_cdescriptor = _message.default_pool.FindExtensionByName(full_name)
605 self.
_cdescriptor = _message.default_pool.FindFieldByName(full_name)
611 """Camelcase name of this field.
614 str: the name in CamelCase.
622 """Converts from a Python proto type to a C++ Proto Type.
624 The Python ProtocolBuffer classes specify both the 'Python' datatype and the
625 'C++' datatype - and they're not the same. This helper method should
626 translate from one to another.
629 proto_type: the Python proto type (descriptor.FieldDescriptor.TYPE_*)
631 int: descriptor.FieldDescriptor.CPPTYPE_*, the C++ type.
633 TypeTransformationError: when the Python proto type isn't known.
636 return FieldDescriptor._PYTHON_TO_CPP_PROTO_TYPE_MAP[proto_type]
643 """Descriptor for an enum defined in a .proto file.
646 name (str): Name of the enum type.
647 full_name (str): Full name of the type, including package name
648 and any enclosing type(s).
650 values (list[EnumValueDescriptors]): List of the values
652 values_by_name (dict(str, EnumValueDescriptor)): Same as :attr:`values`,
653 but indexed by the "name" field of each EnumValueDescriptor.
654 values_by_number (dict(int, EnumValueDescriptor)): Same as :attr:`values`,
655 but indexed by the "number" field of each EnumValueDescriptor.
656 containing_type (Descriptor): Descriptor of the immediate containing
657 type of this enum, or None if this is an enum defined at the
658 top level in a .proto file. Set by Descriptor's constructor
659 if we're passed into one.
660 file (FileDescriptor): Reference to file descriptor.
661 options (descriptor_pb2.EnumOptions): Enum options message or
662 None to use default enum options.
665 if _USE_C_DESCRIPTORS:
666 _C_DESCRIPTOR_CLASS = _message.EnumDescriptor
668 def __new__(cls, name, full_name, filename, values,
669 containing_type=None, options=None,
670 serialized_options=None, file=None,
671 serialized_start=None, serialized_end=None, create_key=None):
672 _message.Message._CheckCalledFromGeneratedFile()
673 return _message.default_pool.FindEnumTypeByName(full_name)
675 def __init__(self, name, full_name, filename, values,
676 containing_type=None, options=None,
677 serialized_options=None, file=None,
678 serialized_start=None, serialized_end=None, create_key=None):
679 """Arguments are as described in the attribute description above.
681 Note that filename is an obsolete argument, that is not used anymore.
682 Please use file.name to access this as an attribute.
684 if create_key
is not _internal_create_key:
687 super(EnumDescriptor, self).
__init__(
688 options,
'EnumOptions', name, full_name, file,
689 containing_type, serialized_start=serialized_start,
690 serialized_end=serialized_end, serialized_options=serialized_options)
700 """Copies this to a descriptor_pb2.EnumDescriptorProto.
703 proto (descriptor_pb2.EnumDescriptorProto): An empty descriptor proto.
711 """Descriptor for a single value within an enum.
714 name (str): Name of this value.
715 index (int): Dense, 0-indexed index giving the order that this
716 value appears textually within its enum in the .proto file.
717 number (int): Actual number assigned to this enum value.
718 type (EnumDescriptor): :class:`EnumDescriptor` to which this value
719 belongs. Set by :class:`EnumDescriptor`'s constructor if we're
721 options (descriptor_pb2.EnumValueOptions): Enum value options message or
722 None to use default enum value options options.
725 if _USE_C_DESCRIPTORS:
726 _C_DESCRIPTOR_CLASS = _message.EnumValueDescriptor
730 options=None, serialized_options=None, create_key=None):
731 _message.Message._CheckCalledFromGeneratedFile()
740 options=None, serialized_options=None, create_key=None):
741 """Arguments are as described in the attribute description above."""
742 if create_key
is not _internal_create_key:
745 super(EnumValueDescriptor, self).
__init__(
746 options, serialized_options,
'EnumValueOptions')
754 """Descriptor for a oneof field.
757 name (str): Name of the oneof field.
758 full_name (str): Full name of the oneof field, including package name.
759 index (int): 0-based index giving the order of the oneof field inside
761 containing_type (Descriptor): :class:`Descriptor` of the protocol message
762 type that contains this field. Set by the :class:`Descriptor` constructor
763 if we're passed into one.
764 fields (list[FieldDescriptor]): The list of field descriptors this
768 if _USE_C_DESCRIPTORS:
769 _C_DESCRIPTOR_CLASS = _message.OneofDescriptor
772 cls, name, full_name, index, containing_type, fields, options=None,
773 serialized_options=None, create_key=None):
774 _message.Message._CheckCalledFromGeneratedFile()
775 return _message.default_pool.FindOneofByName(full_name)
778 self, name, full_name, index, containing_type, fields, options=None,
779 serialized_options=None, create_key=None):
780 """Arguments are as described in the attribute description above."""
781 if create_key
is not _internal_create_key:
784 super(OneofDescriptor, self).
__init__(
785 options, serialized_options,
'OneofOptions')
795 """Descriptor for a service.
798 name (str): Name of the service.
799 full_name (str): Full name of the service, including package name.
800 index (int): 0-indexed index giving the order that this services
801 definition appears within the .proto file.
802 methods (list[MethodDescriptor]): List of methods provided by this
804 methods_by_name (dict(str, MethodDescriptor)): Same
805 :class:`MethodDescriptor` objects as in :attr:`methods_by_name`, but
806 indexed by "name" attribute in each :class:`MethodDescriptor`.
807 options (descriptor_pb2.ServiceOptions): Service options message or
808 None to use default service options.
809 file (FileDescriptor): Reference to file info.
812 if _USE_C_DESCRIPTORS:
813 _C_DESCRIPTOR_CLASS = _message.ServiceDescriptor
822 serialized_options=None,
824 serialized_start=None,
827 _message.Message._CheckCalledFromGeneratedFile()
828 return _message.default_pool.FindServiceByName(full_name)
830 def __init__(self, name, full_name, index, methods, options=None,
831 serialized_options=None, file=None,
832 serialized_start=None, serialized_end=None, create_key=None):
833 if create_key
is not _internal_create_key:
836 super(ServiceDescriptor, self).
__init__(
837 options,
'ServiceOptions', name, full_name, file,
838 None, serialized_start=serialized_start,
839 serialized_end=serialized_end, serialized_options=serialized_options)
845 method.containing_service = self
848 """Searches for the specified method, and returns its descriptor.
851 name (str): Name of the method.
853 MethodDescriptor or None: the descriptor for the requested method, if
859 """Copies this to a descriptor_pb2.ServiceDescriptorProto.
862 proto (descriptor_pb2.ServiceDescriptorProto): An empty descriptor proto.
870 """Descriptor for a method in a service.
873 name (str): Name of the method within the service.
874 full_name (str): Full name of method.
875 index (int): 0-indexed index of the method inside the service.
876 containing_service (ServiceDescriptor): The service that contains this
878 input_type (Descriptor): The descriptor of the message that this method
880 output_type (Descriptor): The descriptor of the message that this method
882 options (descriptor_pb2.MethodOptions or None): Method options message, or
883 None to use default method options.
886 if _USE_C_DESCRIPTORS:
887 _C_DESCRIPTOR_CLASS = _message.MethodDescriptor
889 def __new__(cls, name, full_name, index, containing_service,
890 input_type, output_type, options=None, serialized_options=None,
892 _message.Message._CheckCalledFromGeneratedFile()
893 return _message.default_pool.FindMethodByName(full_name)
895 def __init__(self, name, full_name, index, containing_service,
896 input_type, output_type, options=None, serialized_options=None,
898 """The arguments are as described in the description of MethodDescriptor
901 Note that containing_service may be None, and may be set later if necessary.
903 if create_key
is not _internal_create_key:
906 super(MethodDescriptor, self).
__init__(
907 options, serialized_options,
'MethodOptions')
916 """Copies this to a descriptor_pb2.MethodDescriptorProto.
919 proto (descriptor_pb2.MethodDescriptorProto): An empty descriptor proto.
922 Error: If self couldn't be serialized, due to too few constructor
927 service_proto = descriptor_pb2.ServiceDescriptorProto()
929 proto.CopyFrom(service_proto.method[self.
index])
931 raise Error(
'Descriptor does not contain a service.')
935 """Descriptor for a file. Mimics the descriptor_pb2.FileDescriptorProto.
937 Note that :attr:`enum_types_by_name`, :attr:`extensions_by_name`, and
938 :attr:`dependencies` fields are only set by the
939 :py:mod:`google.protobuf.message_factory` module, and not by the generated
943 name (str): Name of file, relative to root of source tree.
944 package (str): Name of the package
945 syntax (str): string indicating syntax of the file (can be "proto2" or
947 serialized_pb (bytes): Byte string of serialized
948 :class:`descriptor_pb2.FileDescriptorProto`.
949 dependencies (list[FileDescriptor]): List of other :class:`FileDescriptor`
950 objects this :class:`FileDescriptor` depends on.
951 public_dependencies (list[FileDescriptor]): A subset of
952 :attr:`dependencies`, which were declared as "public".
953 message_types_by_name (dict(str, Descriptor)): Mapping from message names
954 to their :class:`Descriptor`.
955 enum_types_by_name (dict(str, EnumDescriptor)): Mapping from enum names to
956 their :class:`EnumDescriptor`.
957 extensions_by_name (dict(str, FieldDescriptor)): Mapping from extension
958 names declared at file scope to their :class:`FieldDescriptor`.
959 services_by_name (dict(str, ServiceDescriptor)): Mapping from services'
960 names to their :class:`ServiceDescriptor`.
961 pool (DescriptorPool): The pool this descriptor belongs to. When not
962 passed to the constructor, the global default pool is used.
965 if _USE_C_DESCRIPTORS:
966 _C_DESCRIPTOR_CLASS = _message.FileDescriptor
969 serialized_options=None, serialized_pb=None,
970 dependencies=None, public_dependencies=None,
971 syntax=None, pool=None, create_key=None):
975 if serialized_pb == b
'':
978 return _message.default_pool.FindFileByName(name)
980 raise RuntimeError(
'Please link in cpp generated lib for %s' % (name))
982 return _message.default_pool.AddSerializedFile(serialized_pb)
984 return super(FileDescriptor, cls).
__new__(cls)
987 serialized_options=None, serialized_pb=None,
988 dependencies=None, public_dependencies=None,
989 syntax=None, pool=None, create_key=None):
991 if create_key
is not _internal_create_key:
994 super(FileDescriptor, self).
__init__(
995 options, serialized_options,
'FileOptions')
999 pool = descriptor_pool.Default()
1004 self.
syntax = syntax
or "proto2"
1014 """Copies this to a descriptor_pb2.FileDescriptorProto.
1017 proto: An empty descriptor_pb2.FileDescriptorProto.
1023 """Parses serialized options.
1025 This helper function is used to parse serialized options in generated
1026 proto2 files. It must not be used outside proto2.
1028 message.ParseFromString(string)
1033 """Converts name to camel-case and returns it."""
1034 capitalize_next =
False
1040 capitalize_next =
True
1041 elif capitalize_next:
1042 result.append(c.upper())
1043 capitalize_next =
False
1048 if result
and result[0].isupper():
1049 result[0] = result[0].lower()
1050 return ''.join(result)
1054 """Returns the value of the field `options`, or None if it is not set."""
1055 if descriptor_proto.HasField(
'options'):
1056 return descriptor_proto.options
1062 """Converts name to Json name and returns it."""
1063 capitalize_next =
False
1068 capitalize_next =
True
1069 elif capitalize_next:
1070 result.append(c.upper())
1071 capitalize_next =
False
1075 return ''.join(result)
1078 def MakeDescriptor(desc_proto, package='', build_file_if_cpp=True,
1080 """Make a protobuf Descriptor given a DescriptorProto protobuf.
1082 Handles nested descriptors. Note that this is limited to the scope of defining
1083 a message inside of another message. Composite fields can currently only be
1084 resolved if the message is defined in the same scope as the field.
1087 desc_proto: The descriptor_pb2.DescriptorProto protobuf message.
1088 package: Optional package name for the new message Descriptor (string).
1089 build_file_if_cpp: Update the C++ descriptor pool if api matches.
1090 Set to False on recursion, so no duplicates are created.
1091 syntax: The syntax/semantics that should be used. Set to "proto3" to get
1092 proto3 field presence semantics.
1094 A Descriptor for protobuf messages.
1096 if api_implementation.Type() ==
'cpp' and build_file_if_cpp:
1102 file_descriptor_proto = descriptor_pb2.FileDescriptorProto()
1103 file_descriptor_proto.message_type.add().
MergeFrom(desc_proto)
1109 proto_name = binascii.hexlify(os.urandom(16)).
decode(
'ascii')
1112 file_descriptor_proto.name = os.path.join(package.replace(
'.',
'/'),
1113 proto_name +
'.proto')
1114 file_descriptor_proto.package = package
1116 file_descriptor_proto.name = proto_name +
'.proto'
1118 _message.default_pool.Add(file_descriptor_proto)
1119 result = _message.default_pool.FindFileByName(file_descriptor_proto.name)
1121 if _USE_C_DESCRIPTORS:
1122 return result.message_types_by_name[desc_proto.name]
1124 full_message_name = [desc_proto.name]
1125 if package: full_message_name.insert(0, package)
1129 for enum_proto
in desc_proto.enum_type:
1130 full_name =
'.'.join(full_message_name + [enum_proto.name])
1132 enum_proto.name, full_name,
None, [
1134 create_key=_internal_create_key)
1135 for ii, enum_val
in enumerate(enum_proto.value)],
1136 create_key=_internal_create_key)
1137 enum_types[full_name] = enum_desc
1141 for nested_proto
in desc_proto.nested_type:
1142 full_name =
'.'.join(full_message_name + [nested_proto.name])
1146 package=
'.'.join(full_message_name),
1147 build_file_if_cpp=
False,
1149 nested_types[full_name] = nested_desc
1152 for field_proto
in desc_proto.field:
1153 full_name =
'.'.join(full_message_name + [field_proto.name])
1156 if field_proto.json_name:
1157 json_name = field_proto.json_name
1160 if field_proto.HasField(
'type_name'):
1161 type_name = field_proto.type_name
1162 full_type_name =
'.'.join(full_message_name +
1163 [type_name[type_name.rfind(
'.')+1:]])
1164 if full_type_name
in nested_types:
1165 nested_desc = nested_types[full_type_name]
1166 elif full_type_name
in enum_types:
1167 enum_desc = enum_types[full_type_name]
1170 field_proto.name, full_name, field_proto.number - 1,
1171 field_proto.number, field_proto.type,
1172 FieldDescriptor.ProtoTypeToCppProtoType(field_proto.type),
1173 field_proto.label,
None, nested_desc, enum_desc,
None,
False,
None,
1175 json_name=json_name, create_key=_internal_create_key)
1176 fields.append(field)
1178 desc_name =
'.'.join(full_message_name)
1179 return Descriptor(desc_proto.name, desc_name,
None,
None, fields,
1180 list(nested_types.values()), list(enum_types.values()), [],
1182 create_key=_internal_create_key)