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

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

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