Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: third_party/protobuf/python/google/protobuf/descriptor.py

Issue 2600753002: Reverts third_party/protobuf: Update to HEAD (f52e188fe4) (Closed)
Patch Set: Created 3 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 # Protocol Buffers - Google's data interchange format 1 # Protocol Buffers - Google's data interchange format
2 # Copyright 2008 Google Inc. All rights reserved. 2 # Copyright 2008 Google Inc. All rights reserved.
3 # https://developers.google.com/protocol-buffers/ 3 # https://developers.google.com/protocol-buffers/
4 # 4 #
5 # Redistribution and use in source and binary forms, with or without 5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are 6 # modification, are permitted provided that the following conditions are
7 # met: 7 # met:
8 # 8 #
9 # * Redistributions of source code must retain the above copyright 9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer. 10 # notice, this list of conditions and the following disclaimer.
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 self.name = name 164 self.name = name
165 # TODO(falk): Add function to calculate full_name instead of having it in 165 # TODO(falk): Add function to calculate full_name instead of having it in
166 # memory? 166 # memory?
167 self.full_name = full_name 167 self.full_name = full_name
168 self.file = file 168 self.file = file
169 self.containing_type = containing_type 169 self.containing_type = containing_type
170 170
171 self._serialized_start = serialized_start 171 self._serialized_start = serialized_start
172 self._serialized_end = serialized_end 172 self._serialized_end = serialized_end
173 173
174 def GetTopLevelContainingType(self):
175 """Returns the root if this is a nested type, or itself if its the root."""
176 desc = self
177 while desc.containing_type is not None:
178 desc = desc.containing_type
179 return desc
180
174 def CopyToProto(self, proto): 181 def CopyToProto(self, proto):
175 """Copies this to the matching proto in descriptor_pb2. 182 """Copies this to the matching proto in descriptor_pb2.
176 183
177 Args: 184 Args:
178 proto: An empty proto instance from descriptor_pb2. 185 proto: An empty proto instance from descriptor_pb2.
179 186
180 Raises: 187 Raises:
181 Error: If self couldnt be serialized, due to to few constructor arguments. 188 Error: If self couldnt be serialized, due to to few constructor arguments.
182 """ 189 """
183 if (self.file is not None and 190 if (self.file is not None and
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 251
245 file: (FileDescriptor) Reference to file descriptor. 252 file: (FileDescriptor) Reference to file descriptor.
246 """ 253 """
247 254
248 if _USE_C_DESCRIPTORS: 255 if _USE_C_DESCRIPTORS:
249 _C_DESCRIPTOR_CLASS = _message.Descriptor 256 _C_DESCRIPTOR_CLASS = _message.Descriptor
250 257
251 def __new__(cls, name, full_name, filename, containing_type, fields, 258 def __new__(cls, name, full_name, filename, containing_type, fields,
252 nested_types, enum_types, extensions, options=None, 259 nested_types, enum_types, extensions, options=None,
253 is_extendable=True, extension_ranges=None, oneofs=None, 260 is_extendable=True, extension_ranges=None, oneofs=None,
254 file=None, serialized_start=None, serialized_end=None, # pylint : disable=redefined-builtin 261 file=None, serialized_start=None, serialized_end=None,
255 syntax=None): 262 syntax=None):
256 _message.Message._CheckCalledFromGeneratedFile() 263 _message.Message._CheckCalledFromGeneratedFile()
257 return _message.default_pool.FindMessageTypeByName(full_name) 264 return _message.default_pool.FindMessageTypeByName(full_name)
258 265
259 # NOTE(tmarek): The file argument redefining a builtin is nothing we can 266 # NOTE(tmarek): The file argument redefining a builtin is nothing we can
260 # fix right now since we don't know how many clients already rely on the 267 # fix right now since we don't know how many clients already rely on the
261 # name of the argument. 268 # name of the argument.
262 def __init__(self, name, full_name, filename, containing_type, fields, 269 def __init__(self, name, full_name, filename, containing_type, fields,
263 nested_types, enum_types, extensions, options=None, 270 nested_types, enum_types, extensions, options=None,
264 is_extendable=True, extension_ranges=None, oneofs=None, 271 is_extendable=True, extension_ranges=None, oneofs=None,
265 file=None, serialized_start=None, serialized_end=None, # pylint: disable=redefined-builtin 272 file=None, serialized_start=None, serialized_end=None,
266 syntax=None): 273 syntax=None): # pylint:disable=redefined-builtin
267 """Arguments to __init__() are as described in the description 274 """Arguments to __init__() are as described in the description
268 of Descriptor fields above. 275 of Descriptor fields above.
269 276
270 Note that filename is an obsolete argument, that is not used anymore. 277 Note that filename is an obsolete argument, that is not used anymore.
271 Please use file.name to access this as an attribute. 278 Please use file.name to access this as an attribute.
272 """ 279 """
273 super(Descriptor, self).__init__( 280 super(Descriptor, self).__init__(
274 options, 'MessageOptions', name, full_name, file, 281 options, 'MessageOptions', name, full_name, file,
275 containing_type, serialized_start=serialized_start, 282 containing_type, serialized_start=serialized_start,
276 serialized_end=serialized_end) 283 serialized_end=serialized_end)
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 value for the enum. 342 value for the enum.
336 """ 343 """
337 return self.enum_types_by_name[enum].values_by_number[value].name 344 return self.enum_types_by_name[enum].values_by_number[value].name
338 345
339 def CopyToProto(self, proto): 346 def CopyToProto(self, proto):
340 """Copies this to a descriptor_pb2.DescriptorProto. 347 """Copies this to a descriptor_pb2.DescriptorProto.
341 348
342 Args: 349 Args:
343 proto: An empty descriptor_pb2.DescriptorProto. 350 proto: An empty descriptor_pb2.DescriptorProto.
344 """ 351 """
345 # This function is overridden to give a better doc comment. 352 # This function is overriden to give a better doc comment.
346 super(Descriptor, self).CopyToProto(proto) 353 super(Descriptor, self).CopyToProto(proto)
347 354
348 355
349 # TODO(robinson): We should have aggressive checking here, 356 # TODO(robinson): We should have aggressive checking here,
350 # for example: 357 # for example:
351 # * If you specify a repeated field, you should not be allowed 358 # * If you specify a repeated field, you should not be allowed
352 # to specify a default value. 359 # to specify a default value.
353 # * [Other examples here as needed]. 360 # * [Other examples here as needed].
354 # 361 #
355 # TODO(robinson): for this and other *Descriptor classes, we 362 # TODO(robinson): for this and other *Descriptor classes, we
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 MAX_FIELD_NUMBER = (1 << 29) - 1 490 MAX_FIELD_NUMBER = (1 << 29) - 1
484 FIRST_RESERVED_FIELD_NUMBER = 19000 491 FIRST_RESERVED_FIELD_NUMBER = 19000
485 LAST_RESERVED_FIELD_NUMBER = 19999 492 LAST_RESERVED_FIELD_NUMBER = 19999
486 493
487 if _USE_C_DESCRIPTORS: 494 if _USE_C_DESCRIPTORS:
488 _C_DESCRIPTOR_CLASS = _message.FieldDescriptor 495 _C_DESCRIPTOR_CLASS = _message.FieldDescriptor
489 496
490 def __new__(cls, name, full_name, index, number, type, cpp_type, label, 497 def __new__(cls, name, full_name, index, number, type, cpp_type, label,
491 default_value, message_type, enum_type, containing_type, 498 default_value, message_type, enum_type, containing_type,
492 is_extension, extension_scope, options=None, 499 is_extension, extension_scope, options=None,
493 has_default_value=True, containing_oneof=None, json_name=None): 500 has_default_value=True, containing_oneof=None):
494 _message.Message._CheckCalledFromGeneratedFile() 501 _message.Message._CheckCalledFromGeneratedFile()
495 if is_extension: 502 if is_extension:
496 return _message.default_pool.FindExtensionByName(full_name) 503 return _message.default_pool.FindExtensionByName(full_name)
497 else: 504 else:
498 return _message.default_pool.FindFieldByName(full_name) 505 return _message.default_pool.FindFieldByName(full_name)
499 506
500 def __init__(self, name, full_name, index, number, type, cpp_type, label, 507 def __init__(self, name, full_name, index, number, type, cpp_type, label,
501 default_value, message_type, enum_type, containing_type, 508 default_value, message_type, enum_type, containing_type,
502 is_extension, extension_scope, options=None, 509 is_extension, extension_scope, options=None,
503 has_default_value=True, containing_oneof=None, json_name=None): 510 has_default_value=True, containing_oneof=None):
504 """The arguments are as described in the description of FieldDescriptor 511 """The arguments are as described in the description of FieldDescriptor
505 attributes above. 512 attributes above.
506 513
507 Note that containing_type may be None, and may be set later if necessary 514 Note that containing_type may be None, and may be set later if necessary
508 (to deal with circular references between message types, for example). 515 (to deal with circular references between message types, for example).
509 Likewise for extension_scope. 516 Likewise for extension_scope.
510 """ 517 """
511 super(FieldDescriptor, self).__init__(options, 'FieldOptions') 518 super(FieldDescriptor, self).__init__(options, 'FieldOptions')
512 self.name = name 519 self.name = name
513 self.full_name = full_name 520 self.full_name = full_name
514 self._camelcase_name = None 521 self._camelcase_name = None
515 if json_name is None:
516 self.json_name = _ToJsonName(name)
517 else:
518 self.json_name = json_name
519 self.index = index 522 self.index = index
520 self.number = number 523 self.number = number
521 self.type = type 524 self.type = type
522 self.cpp_type = cpp_type 525 self.cpp_type = cpp_type
523 self.label = label 526 self.label = label
524 self.has_default_value = has_default_value 527 self.has_default_value = has_default_value
525 self.default_value = default_value 528 self.default_value = default_value
526 self.containing_type = containing_type 529 self.containing_type = containing_type
527 self.message_type = message_type 530 self.message_type = message_type
528 self.enum_type = enum_type 531 self.enum_type = enum_type
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
616 value.type = self 619 value.type = self
617 self.values_by_name = dict((v.name, v) for v in values) 620 self.values_by_name = dict((v.name, v) for v in values)
618 self.values_by_number = dict((v.number, v) for v in values) 621 self.values_by_number = dict((v.number, v) for v in values)
619 622
620 def CopyToProto(self, proto): 623 def CopyToProto(self, proto):
621 """Copies this to a descriptor_pb2.EnumDescriptorProto. 624 """Copies this to a descriptor_pb2.EnumDescriptorProto.
622 625
623 Args: 626 Args:
624 proto: An empty descriptor_pb2.EnumDescriptorProto. 627 proto: An empty descriptor_pb2.EnumDescriptorProto.
625 """ 628 """
626 # This function is overridden to give a better doc comment. 629 # This function is overriden to give a better doc comment.
627 super(EnumDescriptor, self).CopyToProto(proto) 630 super(EnumDescriptor, self).CopyToProto(proto)
628 631
629 632
630 class EnumValueDescriptor(DescriptorBase): 633 class EnumValueDescriptor(DescriptorBase):
631 634
632 """Descriptor for a single value within an enum. 635 """Descriptor for a single value within an enum.
633 636
634 name: (str) Name of this value. 637 name: (str) Name of this value.
635 index: (int) Dense, 0-indexed index giving the order that this 638 index: (int) Dense, 0-indexed index giving the order that this
636 value appears textually within its enum in the .proto file. 639 value appears textually within its enum in the .proto file.
(...skipping 18 matching lines...) Expand all
655 658
656 def __init__(self, name, index, number, type=None, options=None): 659 def __init__(self, name, index, number, type=None, options=None):
657 """Arguments are as described in the attribute description above.""" 660 """Arguments are as described in the attribute description above."""
658 super(EnumValueDescriptor, self).__init__(options, 'EnumValueOptions') 661 super(EnumValueDescriptor, self).__init__(options, 'EnumValueOptions')
659 self.name = name 662 self.name = name
660 self.index = index 663 self.index = index
661 self.number = number 664 self.number = number
662 self.type = type 665 self.type = type
663 666
664 667
665 class OneofDescriptor(DescriptorBase): 668 class OneofDescriptor(object):
666 """Descriptor for a oneof field. 669 """Descriptor for a oneof field.
667 670
668 name: (str) Name of the oneof field. 671 name: (str) Name of the oneof field.
669 full_name: (str) Full name of the oneof field, including package name. 672 full_name: (str) Full name of the oneof field, including package name.
670 index: (int) 0-based index giving the order of the oneof field inside 673 index: (int) 0-based index giving the order of the oneof field inside
671 its containing type. 674 its containing type.
672 containing_type: (Descriptor) Descriptor of the protocol message 675 containing_type: (Descriptor) Descriptor of the protocol message
673 type that contains this field. Set by the Descriptor constructor 676 type that contains this field. Set by the Descriptor constructor
674 if we're passed into one. 677 if we're passed into one.
675 fields: (list of FieldDescriptor) The list of field descriptors this 678 fields: (list of FieldDescriptor) The list of field descriptors this
676 oneof can contain. 679 oneof can contain.
677 """ 680 """
678 681
679 if _USE_C_DESCRIPTORS: 682 if _USE_C_DESCRIPTORS:
680 _C_DESCRIPTOR_CLASS = _message.OneofDescriptor 683 _C_DESCRIPTOR_CLASS = _message.OneofDescriptor
681 684
682 def __new__( 685 def __new__(cls, name, full_name, index, containing_type, fields):
683 cls, name, full_name, index, containing_type, fields, options=None):
684 _message.Message._CheckCalledFromGeneratedFile() 686 _message.Message._CheckCalledFromGeneratedFile()
685 return _message.default_pool.FindOneofByName(full_name) 687 return _message.default_pool.FindOneofByName(full_name)
686 688
687 def __init__( 689 def __init__(self, name, full_name, index, containing_type, fields):
688 self, name, full_name, index, containing_type, fields, options=None):
689 """Arguments are as described in the attribute description above.""" 690 """Arguments are as described in the attribute description above."""
690 super(OneofDescriptor, self).__init__(options, 'OneofOptions')
691 self.name = name 691 self.name = name
692 self.full_name = full_name 692 self.full_name = full_name
693 self.index = index 693 self.index = index
694 self.containing_type = containing_type 694 self.containing_type = containing_type
695 self.fields = fields 695 self.fields = fields
696 696
697 697
698 class ServiceDescriptor(_NestedDescriptorBase): 698 class ServiceDescriptor(_NestedDescriptorBase):
699 699
700 """Descriptor for a service. 700 """Descriptor for a service.
701 701
702 name: (str) Name of the service. 702 name: (str) Name of the service.
703 full_name: (str) Full name of the service, including package name. 703 full_name: (str) Full name of the service, including package name.
704 index: (int) 0-indexed index giving the order that this services 704 index: (int) 0-indexed index giving the order that this services
705 definition appears withing the .proto file. 705 definition appears withing the .proto file.
706 methods: (list of MethodDescriptor) List of methods provided by this 706 methods: (list of MethodDescriptor) List of methods provided by this
707 service. 707 service.
708 methods_by_name: (dict str -> MethodDescriptor) Same MethodDescriptor
709 objects as in |methods_by_name|, but indexed by "name" attribute in each
710 MethodDescriptor.
711 options: (descriptor_pb2.ServiceOptions) Service options message or 708 options: (descriptor_pb2.ServiceOptions) Service options message or
712 None to use default service options. 709 None to use default service options.
713 file: (FileDescriptor) Reference to file info. 710 file: (FileDescriptor) Reference to file info.
714 """ 711 """
715 712
716 if _USE_C_DESCRIPTORS:
717 _C_DESCRIPTOR_CLASS = _message.ServiceDescriptor
718
719 def __new__(cls, name, full_name, index, methods, options=None, file=None, # pylint: disable=redefined-builtin
720 serialized_start=None, serialized_end=None):
721 _message.Message._CheckCalledFromGeneratedFile() # pylint: disable=protec ted-access
722 return _message.default_pool.FindServiceByName(full_name)
723
724 def __init__(self, name, full_name, index, methods, options=None, file=None, 713 def __init__(self, name, full_name, index, methods, options=None, file=None,
725 serialized_start=None, serialized_end=None): 714 serialized_start=None, serialized_end=None):
726 super(ServiceDescriptor, self).__init__( 715 super(ServiceDescriptor, self).__init__(
727 options, 'ServiceOptions', name, full_name, file, 716 options, 'ServiceOptions', name, full_name, file,
728 None, serialized_start=serialized_start, 717 None, serialized_start=serialized_start,
729 serialized_end=serialized_end) 718 serialized_end=serialized_end)
730 self.index = index 719 self.index = index
731 self.methods = methods 720 self.methods = methods
732 self.methods_by_name = dict((m.name, m) for m in methods)
733 # Set the containing service for each method in this service. 721 # Set the containing service for each method in this service.
734 for method in self.methods: 722 for method in self.methods:
735 method.containing_service = self 723 method.containing_service = self
736 724
737 def FindMethodByName(self, name): 725 def FindMethodByName(self, name):
738 """Searches for the specified method, and returns its descriptor.""" 726 """Searches for the specified method, and returns its descriptor."""
739 return self.methods_by_name.get(name, None) 727 for method in self.methods:
728 if name == method.name:
729 return method
730 return None
740 731
741 def CopyToProto(self, proto): 732 def CopyToProto(self, proto):
742 """Copies this to a descriptor_pb2.ServiceDescriptorProto. 733 """Copies this to a descriptor_pb2.ServiceDescriptorProto.
743 734
744 Args: 735 Args:
745 proto: An empty descriptor_pb2.ServiceDescriptorProto. 736 proto: An empty descriptor_pb2.ServiceDescriptorProto.
746 """ 737 """
747 # This function is overridden to give a better doc comment. 738 # This function is overriden to give a better doc comment.
748 super(ServiceDescriptor, self).CopyToProto(proto) 739 super(ServiceDescriptor, self).CopyToProto(proto)
749 740
750 741
751 class MethodDescriptor(DescriptorBase): 742 class MethodDescriptor(DescriptorBase):
752 743
753 """Descriptor for a method in a service. 744 """Descriptor for a method in a service.
754 745
755 name: (str) Name of the method within the service. 746 name: (str) Name of the method within the service.
756 full_name: (str) Full name of method. 747 full_name: (str) Full name of method.
757 index: (int) 0-indexed index of the method inside the service. 748 index: (int) 0-indexed index of the method inside the service.
758 containing_service: (ServiceDescriptor) The service that contains this 749 containing_service: (ServiceDescriptor) The service that contains this
759 method. 750 method.
760 input_type: The descriptor of the message that this method accepts. 751 input_type: The descriptor of the message that this method accepts.
761 output_type: The descriptor of the message that this method returns. 752 output_type: The descriptor of the message that this method returns.
762 options: (descriptor_pb2.MethodOptions) Method options message or 753 options: (descriptor_pb2.MethodOptions) Method options message or
763 None to use default method options. 754 None to use default method options.
764 """ 755 """
765 756
766 if _USE_C_DESCRIPTORS:
767 _C_DESCRIPTOR_CLASS = _message.MethodDescriptor
768
769 def __new__(cls, name, full_name, index, containing_service,
770 input_type, output_type, options=None):
771 _message.Message._CheckCalledFromGeneratedFile() # pylint: disable=protec ted-access
772 return _message.default_pool.FindMethodByName(full_name)
773
774 def __init__(self, name, full_name, index, containing_service, 757 def __init__(self, name, full_name, index, containing_service,
775 input_type, output_type, options=None): 758 input_type, output_type, options=None):
776 """The arguments are as described in the description of MethodDescriptor 759 """The arguments are as described in the description of MethodDescriptor
777 attributes above. 760 attributes above.
778 761
779 Note that containing_service may be None, and may be set later if necessary. 762 Note that containing_service may be None, and may be set later if necessary.
780 """ 763 """
781 super(MethodDescriptor, self).__init__(options, 'MethodOptions') 764 super(MethodDescriptor, self).__init__(options, 'MethodOptions')
782 self.name = name 765 self.name = name
783 self.full_name = full_name 766 self.full_name = full_name
(...skipping 14 matching lines...) Expand all
798 package: name of the package 781 package: name of the package
799 syntax: string indicating syntax of the file (can be "proto2" or "proto3") 782 syntax: string indicating syntax of the file (can be "proto2" or "proto3")
800 serialized_pb: (str) Byte string of serialized 783 serialized_pb: (str) Byte string of serialized
801 descriptor_pb2.FileDescriptorProto. 784 descriptor_pb2.FileDescriptorProto.
802 dependencies: List of other FileDescriptors this FileDescriptor depends on. 785 dependencies: List of other FileDescriptors this FileDescriptor depends on.
803 public_dependencies: A list of FileDescriptors, subset of the dependencies 786 public_dependencies: A list of FileDescriptors, subset of the dependencies
804 above, which were declared as "public". 787 above, which were declared as "public".
805 message_types_by_name: Dict of message names of their descriptors. 788 message_types_by_name: Dict of message names of their descriptors.
806 enum_types_by_name: Dict of enum names and their descriptors. 789 enum_types_by_name: Dict of enum names and their descriptors.
807 extensions_by_name: Dict of extension names and their descriptors. 790 extensions_by_name: Dict of extension names and their descriptors.
808 services_by_name: Dict of services names and their descriptors.
809 pool: the DescriptorPool this descriptor belongs to. When not passed to the 791 pool: the DescriptorPool this descriptor belongs to. When not passed to the
810 constructor, the global default pool is used. 792 constructor, the global default pool is used.
811 """ 793 """
812 794
813 if _USE_C_DESCRIPTORS: 795 if _USE_C_DESCRIPTORS:
814 _C_DESCRIPTOR_CLASS = _message.FileDescriptor 796 _C_DESCRIPTOR_CLASS = _message.FileDescriptor
815 797
816 def __new__(cls, name, package, options=None, serialized_pb=None, 798 def __new__(cls, name, package, options=None, serialized_pb=None,
817 dependencies=None, public_dependencies=None, 799 dependencies=None, public_dependencies=None,
818 syntax=None, pool=None): 800 syntax=None, pool=None):
(...skipping 17 matching lines...) Expand all
836 pool = descriptor_pool.Default() 818 pool = descriptor_pool.Default()
837 self.pool = pool 819 self.pool = pool
838 self.message_types_by_name = {} 820 self.message_types_by_name = {}
839 self.name = name 821 self.name = name
840 self.package = package 822 self.package = package
841 self.syntax = syntax or "proto2" 823 self.syntax = syntax or "proto2"
842 self.serialized_pb = serialized_pb 824 self.serialized_pb = serialized_pb
843 825
844 self.enum_types_by_name = {} 826 self.enum_types_by_name = {}
845 self.extensions_by_name = {} 827 self.extensions_by_name = {}
846 self.services_by_name = {}
847 self.dependencies = (dependencies or []) 828 self.dependencies = (dependencies or [])
848 self.public_dependencies = (public_dependencies or []) 829 self.public_dependencies = (public_dependencies or [])
849 830
850 if (api_implementation.Type() == 'cpp' and 831 if (api_implementation.Type() == 'cpp' and
851 self.serialized_pb is not None): 832 self.serialized_pb is not None):
852 _message.default_pool.AddSerializedFile(self.serialized_pb) 833 _message.default_pool.AddSerializedFile(self.serialized_pb)
853 834
854 def CopyToProto(self, proto): 835 def CopyToProto(self, proto):
855 """Copies this to a descriptor_pb2.FileDescriptorProto. 836 """Copies this to a descriptor_pb2.FileDescriptorProto.
856 837
(...skipping 27 matching lines...) Expand all
884 capitalize_next = False 865 capitalize_next = False
885 else: 866 else:
886 result += c 867 result += c
887 868
888 # Lower-case the first letter. 869 # Lower-case the first letter.
889 if result and result[0].isupper(): 870 if result and result[0].isupper():
890 result[0] = result[0].lower() 871 result[0] = result[0].lower()
891 return ''.join(result) 872 return ''.join(result)
892 873
893 874
894 def _OptionsOrNone(descriptor_proto):
895 """Returns the value of the field `options`, or None if it is not set."""
896 if descriptor_proto.HasField('options'):
897 return descriptor_proto.options
898 else:
899 return None
900
901
902 def _ToJsonName(name):
903 """Converts name to Json name and returns it."""
904 capitalize_next = False
905 result = []
906
907 for c in name:
908 if c == '_':
909 capitalize_next = True
910 elif capitalize_next:
911 result.append(c.upper())
912 capitalize_next = False
913 else:
914 result += c
915
916 return ''.join(result)
917
918
919 def MakeDescriptor(desc_proto, package='', build_file_if_cpp=True, 875 def MakeDescriptor(desc_proto, package='', build_file_if_cpp=True,
920 syntax=None): 876 syntax=None):
921 """Make a protobuf Descriptor given a DescriptorProto protobuf. 877 """Make a protobuf Descriptor given a DescriptorProto protobuf.
922 878
923 Handles nested descriptors. Note that this is limited to the scope of defining 879 Handles nested descriptors. Note that this is limited to the scope of defining
924 a message inside of another message. Composite fields can currently only be 880 a message inside of another message. Composite fields can currently only be
925 resolved if the message is defined in the same scope as the field. 881 resolved if the message is defined in the same scope as the field.
926 882
927 Args: 883 Args:
928 desc_proto: The descriptor_pb2.DescriptorProto protobuf message. 884 desc_proto: The descriptor_pb2.DescriptorProto protobuf message.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
985 package='.'.join(full_message_name), 941 package='.'.join(full_message_name),
986 build_file_if_cpp=False, 942 build_file_if_cpp=False,
987 syntax=syntax) 943 syntax=syntax)
988 nested_types[full_name] = nested_desc 944 nested_types[full_name] = nested_desc
989 945
990 fields = [] 946 fields = []
991 for field_proto in desc_proto.field: 947 for field_proto in desc_proto.field:
992 full_name = '.'.join(full_message_name + [field_proto.name]) 948 full_name = '.'.join(full_message_name + [field_proto.name])
993 enum_desc = None 949 enum_desc = None
994 nested_desc = None 950 nested_desc = None
995 if field_proto.json_name:
996 json_name = field_proto.json_name
997 else:
998 json_name = None
999 if field_proto.HasField('type_name'): 951 if field_proto.HasField('type_name'):
1000 type_name = field_proto.type_name 952 type_name = field_proto.type_name
1001 full_type_name = '.'.join(full_message_name + 953 full_type_name = '.'.join(full_message_name +
1002 [type_name[type_name.rfind('.')+1:]]) 954 [type_name[type_name.rfind('.')+1:]])
1003 if full_type_name in nested_types: 955 if full_type_name in nested_types:
1004 nested_desc = nested_types[full_type_name] 956 nested_desc = nested_types[full_type_name]
1005 elif full_type_name in enum_types: 957 elif full_type_name in enum_types:
1006 enum_desc = enum_types[full_type_name] 958 enum_desc = enum_types[full_type_name]
1007 # Else type_name references a non-local type, which isn't implemented 959 # Else type_name references a non-local type, which isn't implemented
1008 field = FieldDescriptor( 960 field = FieldDescriptor(
1009 field_proto.name, full_name, field_proto.number - 1, 961 field_proto.name, full_name, field_proto.number - 1,
1010 field_proto.number, field_proto.type, 962 field_proto.number, field_proto.type,
1011 FieldDescriptor.ProtoTypeToCppProtoType(field_proto.type), 963 FieldDescriptor.ProtoTypeToCppProtoType(field_proto.type),
1012 field_proto.label, None, nested_desc, enum_desc, None, False, None, 964 field_proto.label, None, nested_desc, enum_desc, None, False, None,
1013 options=_OptionsOrNone(field_proto), has_default_value=False, 965 options=field_proto.options, has_default_value=False)
1014 json_name=json_name)
1015 fields.append(field) 966 fields.append(field)
1016 967
1017 desc_name = '.'.join(full_message_name) 968 desc_name = '.'.join(full_message_name)
1018 return Descriptor(desc_proto.name, desc_name, None, None, fields, 969 return Descriptor(desc_proto.name, desc_name, None, None, fields,
1019 list(nested_types.values()), list(enum_types.values()), [], 970 list(nested_types.values()), list(enum_types.values()), [],
1020 options=_OptionsOrNone(desc_proto)) 971 options=desc_proto.options)
OLDNEW
« no previous file with comments | « third_party/protobuf/python/google/protobuf/__init__.py ('k') | third_party/protobuf/python/google/protobuf/descriptor_pb2.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698