1ffe3c632Sopenharmony_ci# Protocol Buffers - Google's data interchange format 2ffe3c632Sopenharmony_ci# Copyright 2008 Google Inc. All rights reserved. 3ffe3c632Sopenharmony_ci# https://developers.google.com/protocol-buffers/ 4ffe3c632Sopenharmony_ci# 5ffe3c632Sopenharmony_ci# Redistribution and use in source and binary forms, with or without 6ffe3c632Sopenharmony_ci# modification, are permitted provided that the following conditions are 7ffe3c632Sopenharmony_ci# met: 8ffe3c632Sopenharmony_ci# 9ffe3c632Sopenharmony_ci# * Redistributions of source code must retain the above copyright 10ffe3c632Sopenharmony_ci# notice, this list of conditions and the following disclaimer. 11ffe3c632Sopenharmony_ci# * Redistributions in binary form must reproduce the above 12ffe3c632Sopenharmony_ci# copyright notice, this list of conditions and the following disclaimer 13ffe3c632Sopenharmony_ci# in the documentation and/or other materials provided with the 14ffe3c632Sopenharmony_ci# distribution. 15ffe3c632Sopenharmony_ci# * Neither the name of Google Inc. nor the names of its 16ffe3c632Sopenharmony_ci# contributors may be used to endorse or promote products derived from 17ffe3c632Sopenharmony_ci# this software without specific prior written permission. 18ffe3c632Sopenharmony_ci# 19ffe3c632Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20ffe3c632Sopenharmony_ci# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21ffe3c632Sopenharmony_ci# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22ffe3c632Sopenharmony_ci# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23ffe3c632Sopenharmony_ci# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24ffe3c632Sopenharmony_ci# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25ffe3c632Sopenharmony_ci# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26ffe3c632Sopenharmony_ci# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27ffe3c632Sopenharmony_ci# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28ffe3c632Sopenharmony_ci# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29ffe3c632Sopenharmony_ci# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30ffe3c632Sopenharmony_ci 31ffe3c632Sopenharmony_ci"""Descriptors essentially contain exactly the information found in a .proto 32ffe3c632Sopenharmony_cifile, in types that make this information accessible in Python. 33ffe3c632Sopenharmony_ci""" 34ffe3c632Sopenharmony_ci 35ffe3c632Sopenharmony_ci__author__ = 'robinson@google.com (Will Robinson)' 36ffe3c632Sopenharmony_ci 37ffe3c632Sopenharmony_ciimport threading 38ffe3c632Sopenharmony_ciimport warnings 39ffe3c632Sopenharmony_ciimport six 40ffe3c632Sopenharmony_ci 41ffe3c632Sopenharmony_cifrom google.protobuf.internal import api_implementation 42ffe3c632Sopenharmony_ci 43ffe3c632Sopenharmony_ci_USE_C_DESCRIPTORS = False 44ffe3c632Sopenharmony_ciif api_implementation.Type() == 'cpp': 45ffe3c632Sopenharmony_ci # Used by MakeDescriptor in cpp mode 46ffe3c632Sopenharmony_ci import binascii 47ffe3c632Sopenharmony_ci import os 48ffe3c632Sopenharmony_ci from google.protobuf.pyext import _message 49ffe3c632Sopenharmony_ci _USE_C_DESCRIPTORS = True 50ffe3c632Sopenharmony_ci 51ffe3c632Sopenharmony_ci 52ffe3c632Sopenharmony_ciclass Error(Exception): 53ffe3c632Sopenharmony_ci """Base error for this module.""" 54ffe3c632Sopenharmony_ci 55ffe3c632Sopenharmony_ci 56ffe3c632Sopenharmony_ciclass TypeTransformationError(Error): 57ffe3c632Sopenharmony_ci """Error transforming between python proto type and corresponding C++ type.""" 58ffe3c632Sopenharmony_ci 59ffe3c632Sopenharmony_ci 60ffe3c632Sopenharmony_ciif _USE_C_DESCRIPTORS: 61ffe3c632Sopenharmony_ci # This metaclass allows to override the behavior of code like 62ffe3c632Sopenharmony_ci # isinstance(my_descriptor, FieldDescriptor) 63ffe3c632Sopenharmony_ci # and make it return True when the descriptor is an instance of the extension 64ffe3c632Sopenharmony_ci # type written in C++. 65ffe3c632Sopenharmony_ci class DescriptorMetaclass(type): 66ffe3c632Sopenharmony_ci def __instancecheck__(cls, obj): 67ffe3c632Sopenharmony_ci if super(DescriptorMetaclass, cls).__instancecheck__(obj): 68ffe3c632Sopenharmony_ci return True 69ffe3c632Sopenharmony_ci if isinstance(obj, cls._C_DESCRIPTOR_CLASS): 70ffe3c632Sopenharmony_ci return True 71ffe3c632Sopenharmony_ci return False 72ffe3c632Sopenharmony_cielse: 73ffe3c632Sopenharmony_ci # The standard metaclass; nothing changes. 74ffe3c632Sopenharmony_ci DescriptorMetaclass = type 75ffe3c632Sopenharmony_ci 76ffe3c632Sopenharmony_ci 77ffe3c632Sopenharmony_ciclass _Lock(object): 78ffe3c632Sopenharmony_ci """Wrapper class of threading.Lock(), which is allowed by 'with'.""" 79ffe3c632Sopenharmony_ci 80ffe3c632Sopenharmony_ci def __new__(cls): 81ffe3c632Sopenharmony_ci self = object.__new__(cls) 82ffe3c632Sopenharmony_ci self._lock = threading.Lock() # pylint: disable=protected-access 83ffe3c632Sopenharmony_ci return self 84ffe3c632Sopenharmony_ci 85ffe3c632Sopenharmony_ci def __enter__(self): 86ffe3c632Sopenharmony_ci self._lock.acquire() 87ffe3c632Sopenharmony_ci 88ffe3c632Sopenharmony_ci def __exit__(self, exc_type, exc_value, exc_tb): 89ffe3c632Sopenharmony_ci self._lock.release() 90ffe3c632Sopenharmony_ci 91ffe3c632Sopenharmony_ci 92ffe3c632Sopenharmony_ci_lock = threading.Lock() 93ffe3c632Sopenharmony_ci 94ffe3c632Sopenharmony_ci 95ffe3c632Sopenharmony_cidef _Deprecated(name): 96ffe3c632Sopenharmony_ci if _Deprecated.count > 0: 97ffe3c632Sopenharmony_ci _Deprecated.count -= 1 98ffe3c632Sopenharmony_ci warnings.warn( 99ffe3c632Sopenharmony_ci 'Call to deprecated create function %s(). Note: Create unlinked ' 100ffe3c632Sopenharmony_ci 'descriptors is going to go away. Please use get/find descriptors from ' 101ffe3c632Sopenharmony_ci 'generated code or query the descriptor_pool.' 102ffe3c632Sopenharmony_ci % name, 103ffe3c632Sopenharmony_ci category=DeprecationWarning, stacklevel=3) 104ffe3c632Sopenharmony_ci 105ffe3c632Sopenharmony_ci 106ffe3c632Sopenharmony_ci# Deprecated warnings will print 100 times at most which should be enough for 107ffe3c632Sopenharmony_ci# users to notice and do not cause timeout. 108ffe3c632Sopenharmony_ci_Deprecated.count = 100 109ffe3c632Sopenharmony_ci 110ffe3c632Sopenharmony_ci 111ffe3c632Sopenharmony_ci_internal_create_key = object() 112ffe3c632Sopenharmony_ci 113ffe3c632Sopenharmony_ci 114ffe3c632Sopenharmony_ciclass DescriptorBase(six.with_metaclass(DescriptorMetaclass)): 115ffe3c632Sopenharmony_ci 116ffe3c632Sopenharmony_ci """Descriptors base class. 117ffe3c632Sopenharmony_ci 118ffe3c632Sopenharmony_ci This class is the base of all descriptor classes. It provides common options 119ffe3c632Sopenharmony_ci related functionality. 120ffe3c632Sopenharmony_ci 121ffe3c632Sopenharmony_ci Attributes: 122ffe3c632Sopenharmony_ci has_options: True if the descriptor has non-default options. Usually it 123ffe3c632Sopenharmony_ci is not necessary to read this -- just call GetOptions() which will 124ffe3c632Sopenharmony_ci happily return the default instance. However, it's sometimes useful 125ffe3c632Sopenharmony_ci for efficiency, and also useful inside the protobuf implementation to 126ffe3c632Sopenharmony_ci avoid some bootstrapping issues. 127ffe3c632Sopenharmony_ci """ 128ffe3c632Sopenharmony_ci 129ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 130ffe3c632Sopenharmony_ci # The class, or tuple of classes, that are considered as "virtual 131ffe3c632Sopenharmony_ci # subclasses" of this descriptor class. 132ffe3c632Sopenharmony_ci _C_DESCRIPTOR_CLASS = () 133ffe3c632Sopenharmony_ci 134ffe3c632Sopenharmony_ci def __init__(self, options, serialized_options, options_class_name): 135ffe3c632Sopenharmony_ci """Initialize the descriptor given its options message and the name of the 136ffe3c632Sopenharmony_ci class of the options message. The name of the class is required in case 137ffe3c632Sopenharmony_ci the options message is None and has to be created. 138ffe3c632Sopenharmony_ci """ 139ffe3c632Sopenharmony_ci self._options = options 140ffe3c632Sopenharmony_ci self._options_class_name = options_class_name 141ffe3c632Sopenharmony_ci self._serialized_options = serialized_options 142ffe3c632Sopenharmony_ci 143ffe3c632Sopenharmony_ci # Does this descriptor have non-default options? 144ffe3c632Sopenharmony_ci self.has_options = (options is not None) or (serialized_options is not None) 145ffe3c632Sopenharmony_ci 146ffe3c632Sopenharmony_ci def _SetOptions(self, options, options_class_name): 147ffe3c632Sopenharmony_ci """Sets the descriptor's options 148ffe3c632Sopenharmony_ci 149ffe3c632Sopenharmony_ci This function is used in generated proto2 files to update descriptor 150ffe3c632Sopenharmony_ci options. It must not be used outside proto2. 151ffe3c632Sopenharmony_ci """ 152ffe3c632Sopenharmony_ci self._options = options 153ffe3c632Sopenharmony_ci self._options_class_name = options_class_name 154ffe3c632Sopenharmony_ci 155ffe3c632Sopenharmony_ci # Does this descriptor have non-default options? 156ffe3c632Sopenharmony_ci self.has_options = options is not None 157ffe3c632Sopenharmony_ci 158ffe3c632Sopenharmony_ci def GetOptions(self): 159ffe3c632Sopenharmony_ci """Retrieves descriptor options. 160ffe3c632Sopenharmony_ci 161ffe3c632Sopenharmony_ci This method returns the options set or creates the default options for the 162ffe3c632Sopenharmony_ci descriptor. 163ffe3c632Sopenharmony_ci """ 164ffe3c632Sopenharmony_ci if self._options: 165ffe3c632Sopenharmony_ci return self._options 166ffe3c632Sopenharmony_ci 167ffe3c632Sopenharmony_ci from google.protobuf import descriptor_pb2 168ffe3c632Sopenharmony_ci try: 169ffe3c632Sopenharmony_ci options_class = getattr(descriptor_pb2, 170ffe3c632Sopenharmony_ci self._options_class_name) 171ffe3c632Sopenharmony_ci except AttributeError: 172ffe3c632Sopenharmony_ci raise RuntimeError('Unknown options class name %s!' % 173ffe3c632Sopenharmony_ci (self._options_class_name)) 174ffe3c632Sopenharmony_ci 175ffe3c632Sopenharmony_ci with _lock: 176ffe3c632Sopenharmony_ci if self._serialized_options is None: 177ffe3c632Sopenharmony_ci self._options = options_class() 178ffe3c632Sopenharmony_ci else: 179ffe3c632Sopenharmony_ci self._options = _ParseOptions(options_class(), 180ffe3c632Sopenharmony_ci self._serialized_options) 181ffe3c632Sopenharmony_ci 182ffe3c632Sopenharmony_ci return self._options 183ffe3c632Sopenharmony_ci 184ffe3c632Sopenharmony_ci 185ffe3c632Sopenharmony_ciclass _NestedDescriptorBase(DescriptorBase): 186ffe3c632Sopenharmony_ci """Common class for descriptors that can be nested.""" 187ffe3c632Sopenharmony_ci 188ffe3c632Sopenharmony_ci def __init__(self, options, options_class_name, name, full_name, 189ffe3c632Sopenharmony_ci file, containing_type, serialized_start=None, 190ffe3c632Sopenharmony_ci serialized_end=None, serialized_options=None): 191ffe3c632Sopenharmony_ci """Constructor. 192ffe3c632Sopenharmony_ci 193ffe3c632Sopenharmony_ci Args: 194ffe3c632Sopenharmony_ci options: Protocol message options or None 195ffe3c632Sopenharmony_ci to use default message options. 196ffe3c632Sopenharmony_ci options_class_name (str): The class name of the above options. 197ffe3c632Sopenharmony_ci name (str): Name of this protocol message type. 198ffe3c632Sopenharmony_ci full_name (str): Fully-qualified name of this protocol message type, 199ffe3c632Sopenharmony_ci which will include protocol "package" name and the name of any 200ffe3c632Sopenharmony_ci enclosing types. 201ffe3c632Sopenharmony_ci file (FileDescriptor): Reference to file info. 202ffe3c632Sopenharmony_ci containing_type: if provided, this is a nested descriptor, with this 203ffe3c632Sopenharmony_ci descriptor as parent, otherwise None. 204ffe3c632Sopenharmony_ci serialized_start: The start index (inclusive) in block in the 205ffe3c632Sopenharmony_ci file.serialized_pb that describes this descriptor. 206ffe3c632Sopenharmony_ci serialized_end: The end index (exclusive) in block in the 207ffe3c632Sopenharmony_ci file.serialized_pb that describes this descriptor. 208ffe3c632Sopenharmony_ci serialized_options: Protocol message serialized options or None. 209ffe3c632Sopenharmony_ci """ 210ffe3c632Sopenharmony_ci super(_NestedDescriptorBase, self).__init__( 211ffe3c632Sopenharmony_ci options, serialized_options, options_class_name) 212ffe3c632Sopenharmony_ci 213ffe3c632Sopenharmony_ci self.name = name 214ffe3c632Sopenharmony_ci # TODO(falk): Add function to calculate full_name instead of having it in 215ffe3c632Sopenharmony_ci # memory? 216ffe3c632Sopenharmony_ci self.full_name = full_name 217ffe3c632Sopenharmony_ci self.file = file 218ffe3c632Sopenharmony_ci self.containing_type = containing_type 219ffe3c632Sopenharmony_ci 220ffe3c632Sopenharmony_ci self._serialized_start = serialized_start 221ffe3c632Sopenharmony_ci self._serialized_end = serialized_end 222ffe3c632Sopenharmony_ci 223ffe3c632Sopenharmony_ci def CopyToProto(self, proto): 224ffe3c632Sopenharmony_ci """Copies this to the matching proto in descriptor_pb2. 225ffe3c632Sopenharmony_ci 226ffe3c632Sopenharmony_ci Args: 227ffe3c632Sopenharmony_ci proto: An empty proto instance from descriptor_pb2. 228ffe3c632Sopenharmony_ci 229ffe3c632Sopenharmony_ci Raises: 230ffe3c632Sopenharmony_ci Error: If self couldnt be serialized, due to to few constructor arguments. 231ffe3c632Sopenharmony_ci """ 232ffe3c632Sopenharmony_ci if (self.file is not None and 233ffe3c632Sopenharmony_ci self._serialized_start is not None and 234ffe3c632Sopenharmony_ci self._serialized_end is not None): 235ffe3c632Sopenharmony_ci proto.ParseFromString(self.file.serialized_pb[ 236ffe3c632Sopenharmony_ci self._serialized_start:self._serialized_end]) 237ffe3c632Sopenharmony_ci else: 238ffe3c632Sopenharmony_ci raise Error('Descriptor does not contain serialization.') 239ffe3c632Sopenharmony_ci 240ffe3c632Sopenharmony_ci 241ffe3c632Sopenharmony_ciclass Descriptor(_NestedDescriptorBase): 242ffe3c632Sopenharmony_ci 243ffe3c632Sopenharmony_ci """Descriptor for a protocol message type. 244ffe3c632Sopenharmony_ci 245ffe3c632Sopenharmony_ci Attributes: 246ffe3c632Sopenharmony_ci name (str): Name of this protocol message type. 247ffe3c632Sopenharmony_ci full_name (str): Fully-qualified name of this protocol message type, 248ffe3c632Sopenharmony_ci which will include protocol "package" name and the name of any 249ffe3c632Sopenharmony_ci enclosing types. 250ffe3c632Sopenharmony_ci containing_type (Descriptor): Reference to the descriptor of the type 251ffe3c632Sopenharmony_ci containing us, or None if this is top-level. 252ffe3c632Sopenharmony_ci fields (list[FieldDescriptor]): Field descriptors for all fields in 253ffe3c632Sopenharmony_ci this type. 254ffe3c632Sopenharmony_ci fields_by_number (dict(int, FieldDescriptor)): Same 255ffe3c632Sopenharmony_ci :class:`FieldDescriptor` objects as in :attr:`fields`, but indexed 256ffe3c632Sopenharmony_ci by "number" attribute in each FieldDescriptor. 257ffe3c632Sopenharmony_ci fields_by_name (dict(str, FieldDescriptor)): Same 258ffe3c632Sopenharmony_ci :class:`FieldDescriptor` objects as in :attr:`fields`, but indexed by 259ffe3c632Sopenharmony_ci "name" attribute in each :class:`FieldDescriptor`. 260ffe3c632Sopenharmony_ci nested_types (list[Descriptor]): Descriptor references 261ffe3c632Sopenharmony_ci for all protocol message types nested within this one. 262ffe3c632Sopenharmony_ci nested_types_by_name (dict(str, Descriptor)): Same Descriptor 263ffe3c632Sopenharmony_ci objects as in :attr:`nested_types`, but indexed by "name" attribute 264ffe3c632Sopenharmony_ci in each Descriptor. 265ffe3c632Sopenharmony_ci enum_types (list[EnumDescriptor]): :class:`EnumDescriptor` references 266ffe3c632Sopenharmony_ci for all enums contained within this type. 267ffe3c632Sopenharmony_ci enum_types_by_name (dict(str, EnumDescriptor)): Same 268ffe3c632Sopenharmony_ci :class:`EnumDescriptor` objects as in :attr:`enum_types`, but 269ffe3c632Sopenharmony_ci indexed by "name" attribute in each EnumDescriptor. 270ffe3c632Sopenharmony_ci enum_values_by_name (dict(str, EnumValueDescriptor)): Dict mapping 271ffe3c632Sopenharmony_ci from enum value name to :class:`EnumValueDescriptor` for that value. 272ffe3c632Sopenharmony_ci extensions (list[FieldDescriptor]): All extensions defined directly 273ffe3c632Sopenharmony_ci within this message type (NOT within a nested type). 274ffe3c632Sopenharmony_ci extensions_by_name (dict(str, FieldDescriptor)): Same FieldDescriptor 275ffe3c632Sopenharmony_ci objects as :attr:`extensions`, but indexed by "name" attribute of each 276ffe3c632Sopenharmony_ci FieldDescriptor. 277ffe3c632Sopenharmony_ci is_extendable (bool): Does this type define any extension ranges? 278ffe3c632Sopenharmony_ci oneofs (list[OneofDescriptor]): The list of descriptors for oneof fields 279ffe3c632Sopenharmony_ci in this message. 280ffe3c632Sopenharmony_ci oneofs_by_name (dict(str, OneofDescriptor)): Same objects as in 281ffe3c632Sopenharmony_ci :attr:`oneofs`, but indexed by "name" attribute. 282ffe3c632Sopenharmony_ci file (FileDescriptor): Reference to file descriptor. 283ffe3c632Sopenharmony_ci 284ffe3c632Sopenharmony_ci """ 285ffe3c632Sopenharmony_ci 286ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 287ffe3c632Sopenharmony_ci _C_DESCRIPTOR_CLASS = _message.Descriptor 288ffe3c632Sopenharmony_ci 289ffe3c632Sopenharmony_ci def __new__(cls, name, full_name, filename, containing_type, fields, 290ffe3c632Sopenharmony_ci nested_types, enum_types, extensions, options=None, 291ffe3c632Sopenharmony_ci serialized_options=None, 292ffe3c632Sopenharmony_ci is_extendable=True, extension_ranges=None, oneofs=None, 293ffe3c632Sopenharmony_ci file=None, serialized_start=None, serialized_end=None, # pylint: disable=redefined-builtin 294ffe3c632Sopenharmony_ci syntax=None, create_key=None): 295ffe3c632Sopenharmony_ci _message.Message._CheckCalledFromGeneratedFile() 296ffe3c632Sopenharmony_ci return _message.default_pool.FindMessageTypeByName(full_name) 297ffe3c632Sopenharmony_ci 298ffe3c632Sopenharmony_ci # NOTE(tmarek): The file argument redefining a builtin is nothing we can 299ffe3c632Sopenharmony_ci # fix right now since we don't know how many clients already rely on the 300ffe3c632Sopenharmony_ci # name of the argument. 301ffe3c632Sopenharmony_ci def __init__(self, name, full_name, filename, containing_type, fields, 302ffe3c632Sopenharmony_ci nested_types, enum_types, extensions, options=None, 303ffe3c632Sopenharmony_ci serialized_options=None, 304ffe3c632Sopenharmony_ci is_extendable=True, extension_ranges=None, oneofs=None, 305ffe3c632Sopenharmony_ci file=None, serialized_start=None, serialized_end=None, # pylint: disable=redefined-builtin 306ffe3c632Sopenharmony_ci syntax=None, create_key=None): 307ffe3c632Sopenharmony_ci """Arguments to __init__() are as described in the description 308ffe3c632Sopenharmony_ci of Descriptor fields above. 309ffe3c632Sopenharmony_ci 310ffe3c632Sopenharmony_ci Note that filename is an obsolete argument, that is not used anymore. 311ffe3c632Sopenharmony_ci Please use file.name to access this as an attribute. 312ffe3c632Sopenharmony_ci """ 313ffe3c632Sopenharmony_ci if create_key is not _internal_create_key: 314ffe3c632Sopenharmony_ci _Deprecated('Descriptor') 315ffe3c632Sopenharmony_ci 316ffe3c632Sopenharmony_ci super(Descriptor, self).__init__( 317ffe3c632Sopenharmony_ci options, 'MessageOptions', name, full_name, file, 318ffe3c632Sopenharmony_ci containing_type, serialized_start=serialized_start, 319ffe3c632Sopenharmony_ci serialized_end=serialized_end, serialized_options=serialized_options) 320ffe3c632Sopenharmony_ci 321ffe3c632Sopenharmony_ci # We have fields in addition to fields_by_name and fields_by_number, 322ffe3c632Sopenharmony_ci # so that: 323ffe3c632Sopenharmony_ci # 1. Clients can index fields by "order in which they're listed." 324ffe3c632Sopenharmony_ci # 2. Clients can easily iterate over all fields with the terse 325ffe3c632Sopenharmony_ci # syntax: for f in descriptor.fields: ... 326ffe3c632Sopenharmony_ci self.fields = fields 327ffe3c632Sopenharmony_ci for field in self.fields: 328ffe3c632Sopenharmony_ci field.containing_type = self 329ffe3c632Sopenharmony_ci self.fields_by_number = dict((f.number, f) for f in fields) 330ffe3c632Sopenharmony_ci self.fields_by_name = dict((f.name, f) for f in fields) 331ffe3c632Sopenharmony_ci self._fields_by_camelcase_name = None 332ffe3c632Sopenharmony_ci 333ffe3c632Sopenharmony_ci self.nested_types = nested_types 334ffe3c632Sopenharmony_ci for nested_type in nested_types: 335ffe3c632Sopenharmony_ci nested_type.containing_type = self 336ffe3c632Sopenharmony_ci self.nested_types_by_name = dict((t.name, t) for t in nested_types) 337ffe3c632Sopenharmony_ci 338ffe3c632Sopenharmony_ci self.enum_types = enum_types 339ffe3c632Sopenharmony_ci for enum_type in self.enum_types: 340ffe3c632Sopenharmony_ci enum_type.containing_type = self 341ffe3c632Sopenharmony_ci self.enum_types_by_name = dict((t.name, t) for t in enum_types) 342ffe3c632Sopenharmony_ci self.enum_values_by_name = dict( 343ffe3c632Sopenharmony_ci (v.name, v) for t in enum_types for v in t.values) 344ffe3c632Sopenharmony_ci 345ffe3c632Sopenharmony_ci self.extensions = extensions 346ffe3c632Sopenharmony_ci for extension in self.extensions: 347ffe3c632Sopenharmony_ci extension.extension_scope = self 348ffe3c632Sopenharmony_ci self.extensions_by_name = dict((f.name, f) for f in extensions) 349ffe3c632Sopenharmony_ci self.is_extendable = is_extendable 350ffe3c632Sopenharmony_ci self.extension_ranges = extension_ranges 351ffe3c632Sopenharmony_ci self.oneofs = oneofs if oneofs is not None else [] 352ffe3c632Sopenharmony_ci self.oneofs_by_name = dict((o.name, o) for o in self.oneofs) 353ffe3c632Sopenharmony_ci for oneof in self.oneofs: 354ffe3c632Sopenharmony_ci oneof.containing_type = self 355ffe3c632Sopenharmony_ci self.syntax = syntax or "proto2" 356ffe3c632Sopenharmony_ci 357ffe3c632Sopenharmony_ci @property 358ffe3c632Sopenharmony_ci def fields_by_camelcase_name(self): 359ffe3c632Sopenharmony_ci """Same FieldDescriptor objects as in :attr:`fields`, but indexed by 360ffe3c632Sopenharmony_ci :attr:`FieldDescriptor.camelcase_name`. 361ffe3c632Sopenharmony_ci """ 362ffe3c632Sopenharmony_ci if self._fields_by_camelcase_name is None: 363ffe3c632Sopenharmony_ci self._fields_by_camelcase_name = dict( 364ffe3c632Sopenharmony_ci (f.camelcase_name, f) for f in self.fields) 365ffe3c632Sopenharmony_ci return self._fields_by_camelcase_name 366ffe3c632Sopenharmony_ci 367ffe3c632Sopenharmony_ci def EnumValueName(self, enum, value): 368ffe3c632Sopenharmony_ci """Returns the string name of an enum value. 369ffe3c632Sopenharmony_ci 370ffe3c632Sopenharmony_ci This is just a small helper method to simplify a common operation. 371ffe3c632Sopenharmony_ci 372ffe3c632Sopenharmony_ci Args: 373ffe3c632Sopenharmony_ci enum: string name of the Enum. 374ffe3c632Sopenharmony_ci value: int, value of the enum. 375ffe3c632Sopenharmony_ci 376ffe3c632Sopenharmony_ci Returns: 377ffe3c632Sopenharmony_ci string name of the enum value. 378ffe3c632Sopenharmony_ci 379ffe3c632Sopenharmony_ci Raises: 380ffe3c632Sopenharmony_ci KeyError if either the Enum doesn't exist or the value is not a valid 381ffe3c632Sopenharmony_ci value for the enum. 382ffe3c632Sopenharmony_ci """ 383ffe3c632Sopenharmony_ci return self.enum_types_by_name[enum].values_by_number[value].name 384ffe3c632Sopenharmony_ci 385ffe3c632Sopenharmony_ci def CopyToProto(self, proto): 386ffe3c632Sopenharmony_ci """Copies this to a descriptor_pb2.DescriptorProto. 387ffe3c632Sopenharmony_ci 388ffe3c632Sopenharmony_ci Args: 389ffe3c632Sopenharmony_ci proto: An empty descriptor_pb2.DescriptorProto. 390ffe3c632Sopenharmony_ci """ 391ffe3c632Sopenharmony_ci # This function is overridden to give a better doc comment. 392ffe3c632Sopenharmony_ci super(Descriptor, self).CopyToProto(proto) 393ffe3c632Sopenharmony_ci 394ffe3c632Sopenharmony_ci 395ffe3c632Sopenharmony_ci# TODO(robinson): We should have aggressive checking here, 396ffe3c632Sopenharmony_ci# for example: 397ffe3c632Sopenharmony_ci# * If you specify a repeated field, you should not be allowed 398ffe3c632Sopenharmony_ci# to specify a default value. 399ffe3c632Sopenharmony_ci# * [Other examples here as needed]. 400ffe3c632Sopenharmony_ci# 401ffe3c632Sopenharmony_ci# TODO(robinson): for this and other *Descriptor classes, we 402ffe3c632Sopenharmony_ci# might also want to lock things down aggressively (e.g., 403ffe3c632Sopenharmony_ci# prevent clients from setting the attributes). Having 404ffe3c632Sopenharmony_ci# stronger invariants here in general will reduce the number 405ffe3c632Sopenharmony_ci# of runtime checks we must do in reflection.py... 406ffe3c632Sopenharmony_ciclass FieldDescriptor(DescriptorBase): 407ffe3c632Sopenharmony_ci 408ffe3c632Sopenharmony_ci """Descriptor for a single field in a .proto file. 409ffe3c632Sopenharmony_ci 410ffe3c632Sopenharmony_ci Attributes: 411ffe3c632Sopenharmony_ci name (str): Name of this field, exactly as it appears in .proto. 412ffe3c632Sopenharmony_ci full_name (str): Name of this field, including containing scope. This is 413ffe3c632Sopenharmony_ci particularly relevant for extensions. 414ffe3c632Sopenharmony_ci index (int): Dense, 0-indexed index giving the order that this 415ffe3c632Sopenharmony_ci field textually appears within its message in the .proto file. 416ffe3c632Sopenharmony_ci number (int): Tag number declared for this field in the .proto file. 417ffe3c632Sopenharmony_ci 418ffe3c632Sopenharmony_ci type (int): (One of the TYPE_* constants below) Declared type. 419ffe3c632Sopenharmony_ci cpp_type (int): (One of the CPPTYPE_* constants below) C++ type used to 420ffe3c632Sopenharmony_ci represent this field. 421ffe3c632Sopenharmony_ci 422ffe3c632Sopenharmony_ci label (int): (One of the LABEL_* constants below) Tells whether this 423ffe3c632Sopenharmony_ci field is optional, required, or repeated. 424ffe3c632Sopenharmony_ci has_default_value (bool): True if this field has a default value defined, 425ffe3c632Sopenharmony_ci otherwise false. 426ffe3c632Sopenharmony_ci default_value (Varies): Default value of this field. Only 427ffe3c632Sopenharmony_ci meaningful for non-repeated scalar fields. Repeated fields 428ffe3c632Sopenharmony_ci should always set this to [], and non-repeated composite 429ffe3c632Sopenharmony_ci fields should always set this to None. 430ffe3c632Sopenharmony_ci 431ffe3c632Sopenharmony_ci containing_type (Descriptor): Descriptor of the protocol message 432ffe3c632Sopenharmony_ci type that contains this field. Set by the Descriptor constructor 433ffe3c632Sopenharmony_ci if we're passed into one. 434ffe3c632Sopenharmony_ci Somewhat confusingly, for extension fields, this is the 435ffe3c632Sopenharmony_ci descriptor of the EXTENDED message, not the descriptor 436ffe3c632Sopenharmony_ci of the message containing this field. (See is_extension and 437ffe3c632Sopenharmony_ci extension_scope below). 438ffe3c632Sopenharmony_ci message_type (Descriptor): If a composite field, a descriptor 439ffe3c632Sopenharmony_ci of the message type contained in this field. Otherwise, this is None. 440ffe3c632Sopenharmony_ci enum_type (EnumDescriptor): If this field contains an enum, a 441ffe3c632Sopenharmony_ci descriptor of that enum. Otherwise, this is None. 442ffe3c632Sopenharmony_ci 443ffe3c632Sopenharmony_ci is_extension: True iff this describes an extension field. 444ffe3c632Sopenharmony_ci extension_scope (Descriptor): Only meaningful if is_extension is True. 445ffe3c632Sopenharmony_ci Gives the message that immediately contains this extension field. 446ffe3c632Sopenharmony_ci Will be None iff we're a top-level (file-level) extension field. 447ffe3c632Sopenharmony_ci 448ffe3c632Sopenharmony_ci options (descriptor_pb2.FieldOptions): Protocol message field options or 449ffe3c632Sopenharmony_ci None to use default field options. 450ffe3c632Sopenharmony_ci 451ffe3c632Sopenharmony_ci containing_oneof (OneofDescriptor): If the field is a member of a oneof 452ffe3c632Sopenharmony_ci union, contains its descriptor. Otherwise, None. 453ffe3c632Sopenharmony_ci 454ffe3c632Sopenharmony_ci file (FileDescriptor): Reference to file descriptor. 455ffe3c632Sopenharmony_ci """ 456ffe3c632Sopenharmony_ci 457ffe3c632Sopenharmony_ci # Must be consistent with C++ FieldDescriptor::Type enum in 458ffe3c632Sopenharmony_ci # descriptor.h. 459ffe3c632Sopenharmony_ci # 460ffe3c632Sopenharmony_ci # TODO(robinson): Find a way to eliminate this repetition. 461ffe3c632Sopenharmony_ci TYPE_DOUBLE = 1 462ffe3c632Sopenharmony_ci TYPE_FLOAT = 2 463ffe3c632Sopenharmony_ci TYPE_INT64 = 3 464ffe3c632Sopenharmony_ci TYPE_UINT64 = 4 465ffe3c632Sopenharmony_ci TYPE_INT32 = 5 466ffe3c632Sopenharmony_ci TYPE_FIXED64 = 6 467ffe3c632Sopenharmony_ci TYPE_FIXED32 = 7 468ffe3c632Sopenharmony_ci TYPE_BOOL = 8 469ffe3c632Sopenharmony_ci TYPE_STRING = 9 470ffe3c632Sopenharmony_ci TYPE_GROUP = 10 471ffe3c632Sopenharmony_ci TYPE_MESSAGE = 11 472ffe3c632Sopenharmony_ci TYPE_BYTES = 12 473ffe3c632Sopenharmony_ci TYPE_UINT32 = 13 474ffe3c632Sopenharmony_ci TYPE_ENUM = 14 475ffe3c632Sopenharmony_ci TYPE_SFIXED32 = 15 476ffe3c632Sopenharmony_ci TYPE_SFIXED64 = 16 477ffe3c632Sopenharmony_ci TYPE_SINT32 = 17 478ffe3c632Sopenharmony_ci TYPE_SINT64 = 18 479ffe3c632Sopenharmony_ci MAX_TYPE = 18 480ffe3c632Sopenharmony_ci 481ffe3c632Sopenharmony_ci # Must be consistent with C++ FieldDescriptor::CppType enum in 482ffe3c632Sopenharmony_ci # descriptor.h. 483ffe3c632Sopenharmony_ci # 484ffe3c632Sopenharmony_ci # TODO(robinson): Find a way to eliminate this repetition. 485ffe3c632Sopenharmony_ci CPPTYPE_INT32 = 1 486ffe3c632Sopenharmony_ci CPPTYPE_INT64 = 2 487ffe3c632Sopenharmony_ci CPPTYPE_UINT32 = 3 488ffe3c632Sopenharmony_ci CPPTYPE_UINT64 = 4 489ffe3c632Sopenharmony_ci CPPTYPE_DOUBLE = 5 490ffe3c632Sopenharmony_ci CPPTYPE_FLOAT = 6 491ffe3c632Sopenharmony_ci CPPTYPE_BOOL = 7 492ffe3c632Sopenharmony_ci CPPTYPE_ENUM = 8 493ffe3c632Sopenharmony_ci CPPTYPE_STRING = 9 494ffe3c632Sopenharmony_ci CPPTYPE_MESSAGE = 10 495ffe3c632Sopenharmony_ci MAX_CPPTYPE = 10 496ffe3c632Sopenharmony_ci 497ffe3c632Sopenharmony_ci _PYTHON_TO_CPP_PROTO_TYPE_MAP = { 498ffe3c632Sopenharmony_ci TYPE_DOUBLE: CPPTYPE_DOUBLE, 499ffe3c632Sopenharmony_ci TYPE_FLOAT: CPPTYPE_FLOAT, 500ffe3c632Sopenharmony_ci TYPE_ENUM: CPPTYPE_ENUM, 501ffe3c632Sopenharmony_ci TYPE_INT64: CPPTYPE_INT64, 502ffe3c632Sopenharmony_ci TYPE_SINT64: CPPTYPE_INT64, 503ffe3c632Sopenharmony_ci TYPE_SFIXED64: CPPTYPE_INT64, 504ffe3c632Sopenharmony_ci TYPE_UINT64: CPPTYPE_UINT64, 505ffe3c632Sopenharmony_ci TYPE_FIXED64: CPPTYPE_UINT64, 506ffe3c632Sopenharmony_ci TYPE_INT32: CPPTYPE_INT32, 507ffe3c632Sopenharmony_ci TYPE_SFIXED32: CPPTYPE_INT32, 508ffe3c632Sopenharmony_ci TYPE_SINT32: CPPTYPE_INT32, 509ffe3c632Sopenharmony_ci TYPE_UINT32: CPPTYPE_UINT32, 510ffe3c632Sopenharmony_ci TYPE_FIXED32: CPPTYPE_UINT32, 511ffe3c632Sopenharmony_ci TYPE_BYTES: CPPTYPE_STRING, 512ffe3c632Sopenharmony_ci TYPE_STRING: CPPTYPE_STRING, 513ffe3c632Sopenharmony_ci TYPE_BOOL: CPPTYPE_BOOL, 514ffe3c632Sopenharmony_ci TYPE_MESSAGE: CPPTYPE_MESSAGE, 515ffe3c632Sopenharmony_ci TYPE_GROUP: CPPTYPE_MESSAGE 516ffe3c632Sopenharmony_ci } 517ffe3c632Sopenharmony_ci 518ffe3c632Sopenharmony_ci # Must be consistent with C++ FieldDescriptor::Label enum in 519ffe3c632Sopenharmony_ci # descriptor.h. 520ffe3c632Sopenharmony_ci # 521ffe3c632Sopenharmony_ci # TODO(robinson): Find a way to eliminate this repetition. 522ffe3c632Sopenharmony_ci LABEL_OPTIONAL = 1 523ffe3c632Sopenharmony_ci LABEL_REQUIRED = 2 524ffe3c632Sopenharmony_ci LABEL_REPEATED = 3 525ffe3c632Sopenharmony_ci MAX_LABEL = 3 526ffe3c632Sopenharmony_ci 527ffe3c632Sopenharmony_ci # Must be consistent with C++ constants kMaxNumber, kFirstReservedNumber, 528ffe3c632Sopenharmony_ci # and kLastReservedNumber in descriptor.h 529ffe3c632Sopenharmony_ci MAX_FIELD_NUMBER = (1 << 29) - 1 530ffe3c632Sopenharmony_ci FIRST_RESERVED_FIELD_NUMBER = 19000 531ffe3c632Sopenharmony_ci LAST_RESERVED_FIELD_NUMBER = 19999 532ffe3c632Sopenharmony_ci 533ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 534ffe3c632Sopenharmony_ci _C_DESCRIPTOR_CLASS = _message.FieldDescriptor 535ffe3c632Sopenharmony_ci 536ffe3c632Sopenharmony_ci def __new__(cls, name, full_name, index, number, type, cpp_type, label, 537ffe3c632Sopenharmony_ci default_value, message_type, enum_type, containing_type, 538ffe3c632Sopenharmony_ci is_extension, extension_scope, options=None, 539ffe3c632Sopenharmony_ci serialized_options=None, 540ffe3c632Sopenharmony_ci has_default_value=True, containing_oneof=None, json_name=None, 541ffe3c632Sopenharmony_ci file=None, create_key=None): # pylint: disable=redefined-builtin 542ffe3c632Sopenharmony_ci _message.Message._CheckCalledFromGeneratedFile() 543ffe3c632Sopenharmony_ci if is_extension: 544ffe3c632Sopenharmony_ci return _message.default_pool.FindExtensionByName(full_name) 545ffe3c632Sopenharmony_ci else: 546ffe3c632Sopenharmony_ci return _message.default_pool.FindFieldByName(full_name) 547ffe3c632Sopenharmony_ci 548ffe3c632Sopenharmony_ci def __init__(self, name, full_name, index, number, type, cpp_type, label, 549ffe3c632Sopenharmony_ci default_value, message_type, enum_type, containing_type, 550ffe3c632Sopenharmony_ci is_extension, extension_scope, options=None, 551ffe3c632Sopenharmony_ci serialized_options=None, 552ffe3c632Sopenharmony_ci has_default_value=True, containing_oneof=None, json_name=None, 553ffe3c632Sopenharmony_ci file=None, create_key=None): # pylint: disable=redefined-builtin 554ffe3c632Sopenharmony_ci """The arguments are as described in the description of FieldDescriptor 555ffe3c632Sopenharmony_ci attributes above. 556ffe3c632Sopenharmony_ci 557ffe3c632Sopenharmony_ci Note that containing_type may be None, and may be set later if necessary 558ffe3c632Sopenharmony_ci (to deal with circular references between message types, for example). 559ffe3c632Sopenharmony_ci Likewise for extension_scope. 560ffe3c632Sopenharmony_ci """ 561ffe3c632Sopenharmony_ci if create_key is not _internal_create_key: 562ffe3c632Sopenharmony_ci _Deprecated('FieldDescriptor') 563ffe3c632Sopenharmony_ci 564ffe3c632Sopenharmony_ci super(FieldDescriptor, self).__init__( 565ffe3c632Sopenharmony_ci options, serialized_options, 'FieldOptions') 566ffe3c632Sopenharmony_ci self.name = name 567ffe3c632Sopenharmony_ci self.full_name = full_name 568ffe3c632Sopenharmony_ci self.file = file 569ffe3c632Sopenharmony_ci self._camelcase_name = None 570ffe3c632Sopenharmony_ci if json_name is None: 571ffe3c632Sopenharmony_ci self.json_name = _ToJsonName(name) 572ffe3c632Sopenharmony_ci else: 573ffe3c632Sopenharmony_ci self.json_name = json_name 574ffe3c632Sopenharmony_ci self.index = index 575ffe3c632Sopenharmony_ci self.number = number 576ffe3c632Sopenharmony_ci self.type = type 577ffe3c632Sopenharmony_ci self.cpp_type = cpp_type 578ffe3c632Sopenharmony_ci self.label = label 579ffe3c632Sopenharmony_ci self.has_default_value = has_default_value 580ffe3c632Sopenharmony_ci self.default_value = default_value 581ffe3c632Sopenharmony_ci self.containing_type = containing_type 582ffe3c632Sopenharmony_ci self.message_type = message_type 583ffe3c632Sopenharmony_ci self.enum_type = enum_type 584ffe3c632Sopenharmony_ci self.is_extension = is_extension 585ffe3c632Sopenharmony_ci self.extension_scope = extension_scope 586ffe3c632Sopenharmony_ci self.containing_oneof = containing_oneof 587ffe3c632Sopenharmony_ci if api_implementation.Type() == 'cpp': 588ffe3c632Sopenharmony_ci if is_extension: 589ffe3c632Sopenharmony_ci self._cdescriptor = _message.default_pool.FindExtensionByName(full_name) 590ffe3c632Sopenharmony_ci else: 591ffe3c632Sopenharmony_ci self._cdescriptor = _message.default_pool.FindFieldByName(full_name) 592ffe3c632Sopenharmony_ci else: 593ffe3c632Sopenharmony_ci self._cdescriptor = None 594ffe3c632Sopenharmony_ci 595ffe3c632Sopenharmony_ci @property 596ffe3c632Sopenharmony_ci def camelcase_name(self): 597ffe3c632Sopenharmony_ci """Camelcase name of this field. 598ffe3c632Sopenharmony_ci 599ffe3c632Sopenharmony_ci Returns: 600ffe3c632Sopenharmony_ci str: the name in CamelCase. 601ffe3c632Sopenharmony_ci """ 602ffe3c632Sopenharmony_ci if self._camelcase_name is None: 603ffe3c632Sopenharmony_ci self._camelcase_name = _ToCamelCase(self.name) 604ffe3c632Sopenharmony_ci return self._camelcase_name 605ffe3c632Sopenharmony_ci 606ffe3c632Sopenharmony_ci @staticmethod 607ffe3c632Sopenharmony_ci def ProtoTypeToCppProtoType(proto_type): 608ffe3c632Sopenharmony_ci """Converts from a Python proto type to a C++ Proto Type. 609ffe3c632Sopenharmony_ci 610ffe3c632Sopenharmony_ci The Python ProtocolBuffer classes specify both the 'Python' datatype and the 611ffe3c632Sopenharmony_ci 'C++' datatype - and they're not the same. This helper method should 612ffe3c632Sopenharmony_ci translate from one to another. 613ffe3c632Sopenharmony_ci 614ffe3c632Sopenharmony_ci Args: 615ffe3c632Sopenharmony_ci proto_type: the Python proto type (descriptor.FieldDescriptor.TYPE_*) 616ffe3c632Sopenharmony_ci Returns: 617ffe3c632Sopenharmony_ci int: descriptor.FieldDescriptor.CPPTYPE_*, the C++ type. 618ffe3c632Sopenharmony_ci Raises: 619ffe3c632Sopenharmony_ci TypeTransformationError: when the Python proto type isn't known. 620ffe3c632Sopenharmony_ci """ 621ffe3c632Sopenharmony_ci try: 622ffe3c632Sopenharmony_ci return FieldDescriptor._PYTHON_TO_CPP_PROTO_TYPE_MAP[proto_type] 623ffe3c632Sopenharmony_ci except KeyError: 624ffe3c632Sopenharmony_ci raise TypeTransformationError('Unknown proto_type: %s' % proto_type) 625ffe3c632Sopenharmony_ci 626ffe3c632Sopenharmony_ci 627ffe3c632Sopenharmony_ciclass EnumDescriptor(_NestedDescriptorBase): 628ffe3c632Sopenharmony_ci 629ffe3c632Sopenharmony_ci """Descriptor for an enum defined in a .proto file. 630ffe3c632Sopenharmony_ci 631ffe3c632Sopenharmony_ci Attributes: 632ffe3c632Sopenharmony_ci name (str): Name of the enum type. 633ffe3c632Sopenharmony_ci full_name (str): Full name of the type, including package name 634ffe3c632Sopenharmony_ci and any enclosing type(s). 635ffe3c632Sopenharmony_ci 636ffe3c632Sopenharmony_ci values (list[EnumValueDescriptors]): List of the values 637ffe3c632Sopenharmony_ci in this enum. 638ffe3c632Sopenharmony_ci values_by_name (dict(str, EnumValueDescriptor)): Same as :attr:`values`, 639ffe3c632Sopenharmony_ci but indexed by the "name" field of each EnumValueDescriptor. 640ffe3c632Sopenharmony_ci values_by_number (dict(int, EnumValueDescriptor)): Same as :attr:`values`, 641ffe3c632Sopenharmony_ci but indexed by the "number" field of each EnumValueDescriptor. 642ffe3c632Sopenharmony_ci containing_type (Descriptor): Descriptor of the immediate containing 643ffe3c632Sopenharmony_ci type of this enum, or None if this is an enum defined at the 644ffe3c632Sopenharmony_ci top level in a .proto file. Set by Descriptor's constructor 645ffe3c632Sopenharmony_ci if we're passed into one. 646ffe3c632Sopenharmony_ci file (FileDescriptor): Reference to file descriptor. 647ffe3c632Sopenharmony_ci options (descriptor_pb2.EnumOptions): Enum options message or 648ffe3c632Sopenharmony_ci None to use default enum options. 649ffe3c632Sopenharmony_ci """ 650ffe3c632Sopenharmony_ci 651ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 652ffe3c632Sopenharmony_ci _C_DESCRIPTOR_CLASS = _message.EnumDescriptor 653ffe3c632Sopenharmony_ci 654ffe3c632Sopenharmony_ci def __new__(cls, name, full_name, filename, values, 655ffe3c632Sopenharmony_ci containing_type=None, options=None, 656ffe3c632Sopenharmony_ci serialized_options=None, file=None, # pylint: disable=redefined-builtin 657ffe3c632Sopenharmony_ci serialized_start=None, serialized_end=None, create_key=None): 658ffe3c632Sopenharmony_ci _message.Message._CheckCalledFromGeneratedFile() 659ffe3c632Sopenharmony_ci return _message.default_pool.FindEnumTypeByName(full_name) 660ffe3c632Sopenharmony_ci 661ffe3c632Sopenharmony_ci def __init__(self, name, full_name, filename, values, 662ffe3c632Sopenharmony_ci containing_type=None, options=None, 663ffe3c632Sopenharmony_ci serialized_options=None, file=None, # pylint: disable=redefined-builtin 664ffe3c632Sopenharmony_ci serialized_start=None, serialized_end=None, create_key=None): 665ffe3c632Sopenharmony_ci """Arguments are as described in the attribute description above. 666ffe3c632Sopenharmony_ci 667ffe3c632Sopenharmony_ci Note that filename is an obsolete argument, that is not used anymore. 668ffe3c632Sopenharmony_ci Please use file.name to access this as an attribute. 669ffe3c632Sopenharmony_ci """ 670ffe3c632Sopenharmony_ci if create_key is not _internal_create_key: 671ffe3c632Sopenharmony_ci _Deprecated('EnumDescriptor') 672ffe3c632Sopenharmony_ci 673ffe3c632Sopenharmony_ci super(EnumDescriptor, self).__init__( 674ffe3c632Sopenharmony_ci options, 'EnumOptions', name, full_name, file, 675ffe3c632Sopenharmony_ci containing_type, serialized_start=serialized_start, 676ffe3c632Sopenharmony_ci serialized_end=serialized_end, serialized_options=serialized_options) 677ffe3c632Sopenharmony_ci 678ffe3c632Sopenharmony_ci self.values = values 679ffe3c632Sopenharmony_ci for value in self.values: 680ffe3c632Sopenharmony_ci value.type = self 681ffe3c632Sopenharmony_ci self.values_by_name = dict((v.name, v) for v in values) 682ffe3c632Sopenharmony_ci # Values are reversed to ensure that the first alias is retained. 683ffe3c632Sopenharmony_ci self.values_by_number = dict((v.number, v) for v in reversed(values)) 684ffe3c632Sopenharmony_ci 685ffe3c632Sopenharmony_ci def CopyToProto(self, proto): 686ffe3c632Sopenharmony_ci """Copies this to a descriptor_pb2.EnumDescriptorProto. 687ffe3c632Sopenharmony_ci 688ffe3c632Sopenharmony_ci Args: 689ffe3c632Sopenharmony_ci proto (descriptor_pb2.EnumDescriptorProto): An empty descriptor proto. 690ffe3c632Sopenharmony_ci """ 691ffe3c632Sopenharmony_ci # This function is overridden to give a better doc comment. 692ffe3c632Sopenharmony_ci super(EnumDescriptor, self).CopyToProto(proto) 693ffe3c632Sopenharmony_ci 694ffe3c632Sopenharmony_ci 695ffe3c632Sopenharmony_ciclass EnumValueDescriptor(DescriptorBase): 696ffe3c632Sopenharmony_ci 697ffe3c632Sopenharmony_ci """Descriptor for a single value within an enum. 698ffe3c632Sopenharmony_ci 699ffe3c632Sopenharmony_ci Attributes: 700ffe3c632Sopenharmony_ci name (str): Name of this value. 701ffe3c632Sopenharmony_ci index (int): Dense, 0-indexed index giving the order that this 702ffe3c632Sopenharmony_ci value appears textually within its enum in the .proto file. 703ffe3c632Sopenharmony_ci number (int): Actual number assigned to this enum value. 704ffe3c632Sopenharmony_ci type (EnumDescriptor): :class:`EnumDescriptor` to which this value 705ffe3c632Sopenharmony_ci belongs. Set by :class:`EnumDescriptor`'s constructor if we're 706ffe3c632Sopenharmony_ci passed into one. 707ffe3c632Sopenharmony_ci options (descriptor_pb2.EnumValueOptions): Enum value options message or 708ffe3c632Sopenharmony_ci None to use default enum value options options. 709ffe3c632Sopenharmony_ci """ 710ffe3c632Sopenharmony_ci 711ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 712ffe3c632Sopenharmony_ci _C_DESCRIPTOR_CLASS = _message.EnumValueDescriptor 713ffe3c632Sopenharmony_ci 714ffe3c632Sopenharmony_ci def __new__(cls, name, index, number, 715ffe3c632Sopenharmony_ci type=None, # pylint: disable=redefined-builtin 716ffe3c632Sopenharmony_ci options=None, serialized_options=None, create_key=None): 717ffe3c632Sopenharmony_ci _message.Message._CheckCalledFromGeneratedFile() 718ffe3c632Sopenharmony_ci # There is no way we can build a complete EnumValueDescriptor with the 719ffe3c632Sopenharmony_ci # given parameters (the name of the Enum is not known, for example). 720ffe3c632Sopenharmony_ci # Fortunately generated files just pass it to the EnumDescriptor() 721ffe3c632Sopenharmony_ci # constructor, which will ignore it, so returning None is good enough. 722ffe3c632Sopenharmony_ci return None 723ffe3c632Sopenharmony_ci 724ffe3c632Sopenharmony_ci def __init__(self, name, index, number, 725ffe3c632Sopenharmony_ci type=None, # pylint: disable=redefined-builtin 726ffe3c632Sopenharmony_ci options=None, serialized_options=None, create_key=None): 727ffe3c632Sopenharmony_ci """Arguments are as described in the attribute description above.""" 728ffe3c632Sopenharmony_ci if create_key is not _internal_create_key: 729ffe3c632Sopenharmony_ci _Deprecated('EnumValueDescriptor') 730ffe3c632Sopenharmony_ci 731ffe3c632Sopenharmony_ci super(EnumValueDescriptor, self).__init__( 732ffe3c632Sopenharmony_ci options, serialized_options, 'EnumValueOptions') 733ffe3c632Sopenharmony_ci self.name = name 734ffe3c632Sopenharmony_ci self.index = index 735ffe3c632Sopenharmony_ci self.number = number 736ffe3c632Sopenharmony_ci self.type = type 737ffe3c632Sopenharmony_ci 738ffe3c632Sopenharmony_ci 739ffe3c632Sopenharmony_ciclass OneofDescriptor(DescriptorBase): 740ffe3c632Sopenharmony_ci """Descriptor for a oneof field. 741ffe3c632Sopenharmony_ci 742ffe3c632Sopenharmony_ci Attributes: 743ffe3c632Sopenharmony_ci name (str): Name of the oneof field. 744ffe3c632Sopenharmony_ci full_name (str): Full name of the oneof field, including package name. 745ffe3c632Sopenharmony_ci index (int): 0-based index giving the order of the oneof field inside 746ffe3c632Sopenharmony_ci its containing type. 747ffe3c632Sopenharmony_ci containing_type (Descriptor): :class:`Descriptor` of the protocol message 748ffe3c632Sopenharmony_ci type that contains this field. Set by the :class:`Descriptor` constructor 749ffe3c632Sopenharmony_ci if we're passed into one. 750ffe3c632Sopenharmony_ci fields (list[FieldDescriptor]): The list of field descriptors this 751ffe3c632Sopenharmony_ci oneof can contain. 752ffe3c632Sopenharmony_ci """ 753ffe3c632Sopenharmony_ci 754ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 755ffe3c632Sopenharmony_ci _C_DESCRIPTOR_CLASS = _message.OneofDescriptor 756ffe3c632Sopenharmony_ci 757ffe3c632Sopenharmony_ci def __new__( 758ffe3c632Sopenharmony_ci cls, name, full_name, index, containing_type, fields, options=None, 759ffe3c632Sopenharmony_ci serialized_options=None, create_key=None): 760ffe3c632Sopenharmony_ci _message.Message._CheckCalledFromGeneratedFile() 761ffe3c632Sopenharmony_ci return _message.default_pool.FindOneofByName(full_name) 762ffe3c632Sopenharmony_ci 763ffe3c632Sopenharmony_ci def __init__( 764ffe3c632Sopenharmony_ci self, name, full_name, index, containing_type, fields, options=None, 765ffe3c632Sopenharmony_ci serialized_options=None, create_key=None): 766ffe3c632Sopenharmony_ci """Arguments are as described in the attribute description above.""" 767ffe3c632Sopenharmony_ci if create_key is not _internal_create_key: 768ffe3c632Sopenharmony_ci _Deprecated('OneofDescriptor') 769ffe3c632Sopenharmony_ci 770ffe3c632Sopenharmony_ci super(OneofDescriptor, self).__init__( 771ffe3c632Sopenharmony_ci options, serialized_options, 'OneofOptions') 772ffe3c632Sopenharmony_ci self.name = name 773ffe3c632Sopenharmony_ci self.full_name = full_name 774ffe3c632Sopenharmony_ci self.index = index 775ffe3c632Sopenharmony_ci self.containing_type = containing_type 776ffe3c632Sopenharmony_ci self.fields = fields 777ffe3c632Sopenharmony_ci 778ffe3c632Sopenharmony_ci 779ffe3c632Sopenharmony_ciclass ServiceDescriptor(_NestedDescriptorBase): 780ffe3c632Sopenharmony_ci 781ffe3c632Sopenharmony_ci """Descriptor for a service. 782ffe3c632Sopenharmony_ci 783ffe3c632Sopenharmony_ci Attributes: 784ffe3c632Sopenharmony_ci name (str): Name of the service. 785ffe3c632Sopenharmony_ci full_name (str): Full name of the service, including package name. 786ffe3c632Sopenharmony_ci index (int): 0-indexed index giving the order that this services 787ffe3c632Sopenharmony_ci definition appears within the .proto file. 788ffe3c632Sopenharmony_ci methods (list[MethodDescriptor]): List of methods provided by this 789ffe3c632Sopenharmony_ci service. 790ffe3c632Sopenharmony_ci methods_by_name (dict(str, MethodDescriptor)): Same 791ffe3c632Sopenharmony_ci :class:`MethodDescriptor` objects as in :attr:`methods_by_name`, but 792ffe3c632Sopenharmony_ci indexed by "name" attribute in each :class:`MethodDescriptor`. 793ffe3c632Sopenharmony_ci options (descriptor_pb2.ServiceOptions): Service options message or 794ffe3c632Sopenharmony_ci None to use default service options. 795ffe3c632Sopenharmony_ci file (FileDescriptor): Reference to file info. 796ffe3c632Sopenharmony_ci """ 797ffe3c632Sopenharmony_ci 798ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 799ffe3c632Sopenharmony_ci _C_DESCRIPTOR_CLASS = _message.ServiceDescriptor 800ffe3c632Sopenharmony_ci 801ffe3c632Sopenharmony_ci def __new__(cls, name, full_name, index, methods, options=None, 802ffe3c632Sopenharmony_ci serialized_options=None, file=None, # pylint: disable=redefined-builtin 803ffe3c632Sopenharmony_ci serialized_start=None, serialized_end=None, create_key=None): 804ffe3c632Sopenharmony_ci _message.Message._CheckCalledFromGeneratedFile() # pylint: disable=protected-access 805ffe3c632Sopenharmony_ci return _message.default_pool.FindServiceByName(full_name) 806ffe3c632Sopenharmony_ci 807ffe3c632Sopenharmony_ci def __init__(self, name, full_name, index, methods, options=None, 808ffe3c632Sopenharmony_ci serialized_options=None, file=None, # pylint: disable=redefined-builtin 809ffe3c632Sopenharmony_ci serialized_start=None, serialized_end=None, create_key=None): 810ffe3c632Sopenharmony_ci if create_key is not _internal_create_key: 811ffe3c632Sopenharmony_ci _Deprecated('ServiceDescriptor') 812ffe3c632Sopenharmony_ci 813ffe3c632Sopenharmony_ci super(ServiceDescriptor, self).__init__( 814ffe3c632Sopenharmony_ci options, 'ServiceOptions', name, full_name, file, 815ffe3c632Sopenharmony_ci None, serialized_start=serialized_start, 816ffe3c632Sopenharmony_ci serialized_end=serialized_end, serialized_options=serialized_options) 817ffe3c632Sopenharmony_ci self.index = index 818ffe3c632Sopenharmony_ci self.methods = methods 819ffe3c632Sopenharmony_ci self.methods_by_name = dict((m.name, m) for m in methods) 820ffe3c632Sopenharmony_ci # Set the containing service for each method in this service. 821ffe3c632Sopenharmony_ci for method in self.methods: 822ffe3c632Sopenharmony_ci method.containing_service = self 823ffe3c632Sopenharmony_ci 824ffe3c632Sopenharmony_ci def FindMethodByName(self, name): 825ffe3c632Sopenharmony_ci """Searches for the specified method, and returns its descriptor. 826ffe3c632Sopenharmony_ci 827ffe3c632Sopenharmony_ci Args: 828ffe3c632Sopenharmony_ci name (str): Name of the method. 829ffe3c632Sopenharmony_ci Returns: 830ffe3c632Sopenharmony_ci MethodDescriptor or None: the desctiptor for the requested method, if 831ffe3c632Sopenharmony_ci found. 832ffe3c632Sopenharmony_ci """ 833ffe3c632Sopenharmony_ci return self.methods_by_name.get(name, None) 834ffe3c632Sopenharmony_ci 835ffe3c632Sopenharmony_ci def CopyToProto(self, proto): 836ffe3c632Sopenharmony_ci """Copies this to a descriptor_pb2.ServiceDescriptorProto. 837ffe3c632Sopenharmony_ci 838ffe3c632Sopenharmony_ci Args: 839ffe3c632Sopenharmony_ci proto (descriptor_pb2.ServiceDescriptorProto): An empty descriptor proto. 840ffe3c632Sopenharmony_ci """ 841ffe3c632Sopenharmony_ci # This function is overridden to give a better doc comment. 842ffe3c632Sopenharmony_ci super(ServiceDescriptor, self).CopyToProto(proto) 843ffe3c632Sopenharmony_ci 844ffe3c632Sopenharmony_ci 845ffe3c632Sopenharmony_ciclass MethodDescriptor(DescriptorBase): 846ffe3c632Sopenharmony_ci 847ffe3c632Sopenharmony_ci """Descriptor for a method in a service. 848ffe3c632Sopenharmony_ci 849ffe3c632Sopenharmony_ci Attributes: 850ffe3c632Sopenharmony_ci name (str): Name of the method within the service. 851ffe3c632Sopenharmony_ci full_name (str): Full name of method. 852ffe3c632Sopenharmony_ci index (int): 0-indexed index of the method inside the service. 853ffe3c632Sopenharmony_ci containing_service (ServiceDescriptor): The service that contains this 854ffe3c632Sopenharmony_ci method. 855ffe3c632Sopenharmony_ci input_type (Descriptor): The descriptor of the message that this method 856ffe3c632Sopenharmony_ci accepts. 857ffe3c632Sopenharmony_ci output_type (Descriptor): The descriptor of the message that this method 858ffe3c632Sopenharmony_ci returns. 859ffe3c632Sopenharmony_ci options (descriptor_pb2.MethodOptions or None): Method options message, or 860ffe3c632Sopenharmony_ci None to use default method options. 861ffe3c632Sopenharmony_ci """ 862ffe3c632Sopenharmony_ci 863ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 864ffe3c632Sopenharmony_ci _C_DESCRIPTOR_CLASS = _message.MethodDescriptor 865ffe3c632Sopenharmony_ci 866ffe3c632Sopenharmony_ci def __new__(cls, name, full_name, index, containing_service, 867ffe3c632Sopenharmony_ci input_type, output_type, options=None, serialized_options=None, 868ffe3c632Sopenharmony_ci create_key=None): 869ffe3c632Sopenharmony_ci _message.Message._CheckCalledFromGeneratedFile() # pylint: disable=protected-access 870ffe3c632Sopenharmony_ci return _message.default_pool.FindMethodByName(full_name) 871ffe3c632Sopenharmony_ci 872ffe3c632Sopenharmony_ci def __init__(self, name, full_name, index, containing_service, 873ffe3c632Sopenharmony_ci input_type, output_type, options=None, serialized_options=None, 874ffe3c632Sopenharmony_ci create_key=None): 875ffe3c632Sopenharmony_ci """The arguments are as described in the description of MethodDescriptor 876ffe3c632Sopenharmony_ci attributes above. 877ffe3c632Sopenharmony_ci 878ffe3c632Sopenharmony_ci Note that containing_service may be None, and may be set later if necessary. 879ffe3c632Sopenharmony_ci """ 880ffe3c632Sopenharmony_ci if create_key is not _internal_create_key: 881ffe3c632Sopenharmony_ci _Deprecated('MethodDescriptor') 882ffe3c632Sopenharmony_ci 883ffe3c632Sopenharmony_ci super(MethodDescriptor, self).__init__( 884ffe3c632Sopenharmony_ci options, serialized_options, 'MethodOptions') 885ffe3c632Sopenharmony_ci self.name = name 886ffe3c632Sopenharmony_ci self.full_name = full_name 887ffe3c632Sopenharmony_ci self.index = index 888ffe3c632Sopenharmony_ci self.containing_service = containing_service 889ffe3c632Sopenharmony_ci self.input_type = input_type 890ffe3c632Sopenharmony_ci self.output_type = output_type 891ffe3c632Sopenharmony_ci 892ffe3c632Sopenharmony_ci 893ffe3c632Sopenharmony_ciclass FileDescriptor(DescriptorBase): 894ffe3c632Sopenharmony_ci """Descriptor for a file. Mimics the descriptor_pb2.FileDescriptorProto. 895ffe3c632Sopenharmony_ci 896ffe3c632Sopenharmony_ci Note that :attr:`enum_types_by_name`, :attr:`extensions_by_name`, and 897ffe3c632Sopenharmony_ci :attr:`dependencies` fields are only set by the 898ffe3c632Sopenharmony_ci :py:mod:`google.protobuf.message_factory` module, and not by the generated 899ffe3c632Sopenharmony_ci proto code. 900ffe3c632Sopenharmony_ci 901ffe3c632Sopenharmony_ci Attributes: 902ffe3c632Sopenharmony_ci name (str): Name of file, relative to root of source tree. 903ffe3c632Sopenharmony_ci package (str): Name of the package 904ffe3c632Sopenharmony_ci syntax (str): string indicating syntax of the file (can be "proto2" or 905ffe3c632Sopenharmony_ci "proto3") 906ffe3c632Sopenharmony_ci serialized_pb (bytes): Byte string of serialized 907ffe3c632Sopenharmony_ci :class:`descriptor_pb2.FileDescriptorProto`. 908ffe3c632Sopenharmony_ci dependencies (list[FileDescriptor]): List of other :class:`FileDescriptor` 909ffe3c632Sopenharmony_ci objects this :class:`FileDescriptor` depends on. 910ffe3c632Sopenharmony_ci public_dependencies (list[FileDescriptor]): A subset of 911ffe3c632Sopenharmony_ci :attr:`dependencies`, which were declared as "public". 912ffe3c632Sopenharmony_ci message_types_by_name (dict(str, Descriptor)): Mapping from message names 913ffe3c632Sopenharmony_ci to their :class:`Desctiptor`. 914ffe3c632Sopenharmony_ci enum_types_by_name (dict(str, EnumDescriptor)): Mapping from enum names to 915ffe3c632Sopenharmony_ci their :class:`EnumDescriptor`. 916ffe3c632Sopenharmony_ci extensions_by_name (dict(str, FieldDescriptor)): Mapping from extension 917ffe3c632Sopenharmony_ci names declared at file scope to their :class:`FieldDescriptor`. 918ffe3c632Sopenharmony_ci services_by_name (dict(str, ServiceDescriptor)): Mapping from services' 919ffe3c632Sopenharmony_ci names to their :class:`ServiceDescriptor`. 920ffe3c632Sopenharmony_ci pool (DescriptorPool): The pool this descriptor belongs to. When not 921ffe3c632Sopenharmony_ci passed to the constructor, the global default pool is used. 922ffe3c632Sopenharmony_ci """ 923ffe3c632Sopenharmony_ci 924ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 925ffe3c632Sopenharmony_ci _C_DESCRIPTOR_CLASS = _message.FileDescriptor 926ffe3c632Sopenharmony_ci 927ffe3c632Sopenharmony_ci def __new__(cls, name, package, options=None, 928ffe3c632Sopenharmony_ci serialized_options=None, serialized_pb=None, 929ffe3c632Sopenharmony_ci dependencies=None, public_dependencies=None, 930ffe3c632Sopenharmony_ci syntax=None, pool=None, create_key=None): 931ffe3c632Sopenharmony_ci # FileDescriptor() is called from various places, not only from generated 932ffe3c632Sopenharmony_ci # files, to register dynamic proto files and messages. 933ffe3c632Sopenharmony_ci # pylint: disable=g-explicit-bool-comparison 934ffe3c632Sopenharmony_ci if serialized_pb == b'': 935ffe3c632Sopenharmony_ci # Cpp generated code must be linked in if serialized_pb is '' 936ffe3c632Sopenharmony_ci try: 937ffe3c632Sopenharmony_ci return _message.default_pool.FindFileByName(name) 938ffe3c632Sopenharmony_ci except KeyError: 939ffe3c632Sopenharmony_ci raise RuntimeError('Please link in cpp generated lib for %s' % (name)) 940ffe3c632Sopenharmony_ci elif serialized_pb: 941ffe3c632Sopenharmony_ci return _message.default_pool.AddSerializedFile(serialized_pb) 942ffe3c632Sopenharmony_ci else: 943ffe3c632Sopenharmony_ci return super(FileDescriptor, cls).__new__(cls) 944ffe3c632Sopenharmony_ci 945ffe3c632Sopenharmony_ci def __init__(self, name, package, options=None, 946ffe3c632Sopenharmony_ci serialized_options=None, serialized_pb=None, 947ffe3c632Sopenharmony_ci dependencies=None, public_dependencies=None, 948ffe3c632Sopenharmony_ci syntax=None, pool=None, create_key=None): 949ffe3c632Sopenharmony_ci """Constructor.""" 950ffe3c632Sopenharmony_ci if create_key is not _internal_create_key: 951ffe3c632Sopenharmony_ci _Deprecated('FileDescriptor') 952ffe3c632Sopenharmony_ci 953ffe3c632Sopenharmony_ci super(FileDescriptor, self).__init__( 954ffe3c632Sopenharmony_ci options, serialized_options, 'FileOptions') 955ffe3c632Sopenharmony_ci 956ffe3c632Sopenharmony_ci if pool is None: 957ffe3c632Sopenharmony_ci from google.protobuf import descriptor_pool 958ffe3c632Sopenharmony_ci pool = descriptor_pool.Default() 959ffe3c632Sopenharmony_ci self.pool = pool 960ffe3c632Sopenharmony_ci self.message_types_by_name = {} 961ffe3c632Sopenharmony_ci self.name = name 962ffe3c632Sopenharmony_ci self.package = package 963ffe3c632Sopenharmony_ci self.syntax = syntax or "proto2" 964ffe3c632Sopenharmony_ci self.serialized_pb = serialized_pb 965ffe3c632Sopenharmony_ci 966ffe3c632Sopenharmony_ci self.enum_types_by_name = {} 967ffe3c632Sopenharmony_ci self.extensions_by_name = {} 968ffe3c632Sopenharmony_ci self.services_by_name = {} 969ffe3c632Sopenharmony_ci self.dependencies = (dependencies or []) 970ffe3c632Sopenharmony_ci self.public_dependencies = (public_dependencies or []) 971ffe3c632Sopenharmony_ci 972ffe3c632Sopenharmony_ci def CopyToProto(self, proto): 973ffe3c632Sopenharmony_ci """Copies this to a descriptor_pb2.FileDescriptorProto. 974ffe3c632Sopenharmony_ci 975ffe3c632Sopenharmony_ci Args: 976ffe3c632Sopenharmony_ci proto: An empty descriptor_pb2.FileDescriptorProto. 977ffe3c632Sopenharmony_ci """ 978ffe3c632Sopenharmony_ci proto.ParseFromString(self.serialized_pb) 979ffe3c632Sopenharmony_ci 980ffe3c632Sopenharmony_ci 981ffe3c632Sopenharmony_cidef _ParseOptions(message, string): 982ffe3c632Sopenharmony_ci """Parses serialized options. 983ffe3c632Sopenharmony_ci 984ffe3c632Sopenharmony_ci This helper function is used to parse serialized options in generated 985ffe3c632Sopenharmony_ci proto2 files. It must not be used outside proto2. 986ffe3c632Sopenharmony_ci """ 987ffe3c632Sopenharmony_ci message.ParseFromString(string) 988ffe3c632Sopenharmony_ci return message 989ffe3c632Sopenharmony_ci 990ffe3c632Sopenharmony_ci 991ffe3c632Sopenharmony_cidef _ToCamelCase(name): 992ffe3c632Sopenharmony_ci """Converts name to camel-case and returns it.""" 993ffe3c632Sopenharmony_ci capitalize_next = False 994ffe3c632Sopenharmony_ci result = [] 995ffe3c632Sopenharmony_ci 996ffe3c632Sopenharmony_ci for c in name: 997ffe3c632Sopenharmony_ci if c == '_': 998ffe3c632Sopenharmony_ci if result: 999ffe3c632Sopenharmony_ci capitalize_next = True 1000ffe3c632Sopenharmony_ci elif capitalize_next: 1001ffe3c632Sopenharmony_ci result.append(c.upper()) 1002ffe3c632Sopenharmony_ci capitalize_next = False 1003ffe3c632Sopenharmony_ci else: 1004ffe3c632Sopenharmony_ci result += c 1005ffe3c632Sopenharmony_ci 1006ffe3c632Sopenharmony_ci # Lower-case the first letter. 1007ffe3c632Sopenharmony_ci if result and result[0].isupper(): 1008ffe3c632Sopenharmony_ci result[0] = result[0].lower() 1009ffe3c632Sopenharmony_ci return ''.join(result) 1010ffe3c632Sopenharmony_ci 1011ffe3c632Sopenharmony_ci 1012ffe3c632Sopenharmony_cidef _OptionsOrNone(descriptor_proto): 1013ffe3c632Sopenharmony_ci """Returns the value of the field `options`, or None if it is not set.""" 1014ffe3c632Sopenharmony_ci if descriptor_proto.HasField('options'): 1015ffe3c632Sopenharmony_ci return descriptor_proto.options 1016ffe3c632Sopenharmony_ci else: 1017ffe3c632Sopenharmony_ci return None 1018ffe3c632Sopenharmony_ci 1019ffe3c632Sopenharmony_ci 1020ffe3c632Sopenharmony_cidef _ToJsonName(name): 1021ffe3c632Sopenharmony_ci """Converts name to Json name and returns it.""" 1022ffe3c632Sopenharmony_ci capitalize_next = False 1023ffe3c632Sopenharmony_ci result = [] 1024ffe3c632Sopenharmony_ci 1025ffe3c632Sopenharmony_ci for c in name: 1026ffe3c632Sopenharmony_ci if c == '_': 1027ffe3c632Sopenharmony_ci capitalize_next = True 1028ffe3c632Sopenharmony_ci elif capitalize_next: 1029ffe3c632Sopenharmony_ci result.append(c.upper()) 1030ffe3c632Sopenharmony_ci capitalize_next = False 1031ffe3c632Sopenharmony_ci else: 1032ffe3c632Sopenharmony_ci result += c 1033ffe3c632Sopenharmony_ci 1034ffe3c632Sopenharmony_ci return ''.join(result) 1035ffe3c632Sopenharmony_ci 1036ffe3c632Sopenharmony_ci 1037ffe3c632Sopenharmony_cidef MakeDescriptor(desc_proto, package='', build_file_if_cpp=True, 1038ffe3c632Sopenharmony_ci syntax=None): 1039ffe3c632Sopenharmony_ci """Make a protobuf Descriptor given a DescriptorProto protobuf. 1040ffe3c632Sopenharmony_ci 1041ffe3c632Sopenharmony_ci Handles nested descriptors. Note that this is limited to the scope of defining 1042ffe3c632Sopenharmony_ci a message inside of another message. Composite fields can currently only be 1043ffe3c632Sopenharmony_ci resolved if the message is defined in the same scope as the field. 1044ffe3c632Sopenharmony_ci 1045ffe3c632Sopenharmony_ci Args: 1046ffe3c632Sopenharmony_ci desc_proto: The descriptor_pb2.DescriptorProto protobuf message. 1047ffe3c632Sopenharmony_ci package: Optional package name for the new message Descriptor (string). 1048ffe3c632Sopenharmony_ci build_file_if_cpp: Update the C++ descriptor pool if api matches. 1049ffe3c632Sopenharmony_ci Set to False on recursion, so no duplicates are created. 1050ffe3c632Sopenharmony_ci syntax: The syntax/semantics that should be used. Set to "proto3" to get 1051ffe3c632Sopenharmony_ci proto3 field presence semantics. 1052ffe3c632Sopenharmony_ci Returns: 1053ffe3c632Sopenharmony_ci A Descriptor for protobuf messages. 1054ffe3c632Sopenharmony_ci """ 1055ffe3c632Sopenharmony_ci if api_implementation.Type() == 'cpp' and build_file_if_cpp: 1056ffe3c632Sopenharmony_ci # The C++ implementation requires all descriptors to be backed by the same 1057ffe3c632Sopenharmony_ci # definition in the C++ descriptor pool. To do this, we build a 1058ffe3c632Sopenharmony_ci # FileDescriptorProto with the same definition as this descriptor and build 1059ffe3c632Sopenharmony_ci # it into the pool. 1060ffe3c632Sopenharmony_ci from google.protobuf import descriptor_pb2 1061ffe3c632Sopenharmony_ci file_descriptor_proto = descriptor_pb2.FileDescriptorProto() 1062ffe3c632Sopenharmony_ci file_descriptor_proto.message_type.add().MergeFrom(desc_proto) 1063ffe3c632Sopenharmony_ci 1064ffe3c632Sopenharmony_ci # Generate a random name for this proto file to prevent conflicts with any 1065ffe3c632Sopenharmony_ci # imported ones. We need to specify a file name so the descriptor pool 1066ffe3c632Sopenharmony_ci # accepts our FileDescriptorProto, but it is not important what that file 1067ffe3c632Sopenharmony_ci # name is actually set to. 1068ffe3c632Sopenharmony_ci proto_name = binascii.hexlify(os.urandom(16)).decode('ascii') 1069ffe3c632Sopenharmony_ci 1070ffe3c632Sopenharmony_ci if package: 1071ffe3c632Sopenharmony_ci file_descriptor_proto.name = os.path.join(package.replace('.', '/'), 1072ffe3c632Sopenharmony_ci proto_name + '.proto') 1073ffe3c632Sopenharmony_ci file_descriptor_proto.package = package 1074ffe3c632Sopenharmony_ci else: 1075ffe3c632Sopenharmony_ci file_descriptor_proto.name = proto_name + '.proto' 1076ffe3c632Sopenharmony_ci 1077ffe3c632Sopenharmony_ci _message.default_pool.Add(file_descriptor_proto) 1078ffe3c632Sopenharmony_ci result = _message.default_pool.FindFileByName(file_descriptor_proto.name) 1079ffe3c632Sopenharmony_ci 1080ffe3c632Sopenharmony_ci if _USE_C_DESCRIPTORS: 1081ffe3c632Sopenharmony_ci return result.message_types_by_name[desc_proto.name] 1082ffe3c632Sopenharmony_ci 1083ffe3c632Sopenharmony_ci full_message_name = [desc_proto.name] 1084ffe3c632Sopenharmony_ci if package: full_message_name.insert(0, package) 1085ffe3c632Sopenharmony_ci 1086ffe3c632Sopenharmony_ci # Create Descriptors for enum types 1087ffe3c632Sopenharmony_ci enum_types = {} 1088ffe3c632Sopenharmony_ci for enum_proto in desc_proto.enum_type: 1089ffe3c632Sopenharmony_ci full_name = '.'.join(full_message_name + [enum_proto.name]) 1090ffe3c632Sopenharmony_ci enum_desc = EnumDescriptor( 1091ffe3c632Sopenharmony_ci enum_proto.name, full_name, None, [ 1092ffe3c632Sopenharmony_ci EnumValueDescriptor(enum_val.name, ii, enum_val.number, 1093ffe3c632Sopenharmony_ci create_key=_internal_create_key) 1094ffe3c632Sopenharmony_ci for ii, enum_val in enumerate(enum_proto.value)], 1095ffe3c632Sopenharmony_ci create_key=_internal_create_key) 1096ffe3c632Sopenharmony_ci enum_types[full_name] = enum_desc 1097ffe3c632Sopenharmony_ci 1098ffe3c632Sopenharmony_ci # Create Descriptors for nested types 1099ffe3c632Sopenharmony_ci nested_types = {} 1100ffe3c632Sopenharmony_ci for nested_proto in desc_proto.nested_type: 1101ffe3c632Sopenharmony_ci full_name = '.'.join(full_message_name + [nested_proto.name]) 1102ffe3c632Sopenharmony_ci # Nested types are just those defined inside of the message, not all types 1103ffe3c632Sopenharmony_ci # used by fields in the message, so no loops are possible here. 1104ffe3c632Sopenharmony_ci nested_desc = MakeDescriptor(nested_proto, 1105ffe3c632Sopenharmony_ci package='.'.join(full_message_name), 1106ffe3c632Sopenharmony_ci build_file_if_cpp=False, 1107ffe3c632Sopenharmony_ci syntax=syntax) 1108ffe3c632Sopenharmony_ci nested_types[full_name] = nested_desc 1109ffe3c632Sopenharmony_ci 1110ffe3c632Sopenharmony_ci fields = [] 1111ffe3c632Sopenharmony_ci for field_proto in desc_proto.field: 1112ffe3c632Sopenharmony_ci full_name = '.'.join(full_message_name + [field_proto.name]) 1113ffe3c632Sopenharmony_ci enum_desc = None 1114ffe3c632Sopenharmony_ci nested_desc = None 1115ffe3c632Sopenharmony_ci if field_proto.json_name: 1116ffe3c632Sopenharmony_ci json_name = field_proto.json_name 1117ffe3c632Sopenharmony_ci else: 1118ffe3c632Sopenharmony_ci json_name = None 1119ffe3c632Sopenharmony_ci if field_proto.HasField('type_name'): 1120ffe3c632Sopenharmony_ci type_name = field_proto.type_name 1121ffe3c632Sopenharmony_ci full_type_name = '.'.join(full_message_name + 1122ffe3c632Sopenharmony_ci [type_name[type_name.rfind('.')+1:]]) 1123ffe3c632Sopenharmony_ci if full_type_name in nested_types: 1124ffe3c632Sopenharmony_ci nested_desc = nested_types[full_type_name] 1125ffe3c632Sopenharmony_ci elif full_type_name in enum_types: 1126ffe3c632Sopenharmony_ci enum_desc = enum_types[full_type_name] 1127ffe3c632Sopenharmony_ci # Else type_name references a non-local type, which isn't implemented 1128ffe3c632Sopenharmony_ci field = FieldDescriptor( 1129ffe3c632Sopenharmony_ci field_proto.name, full_name, field_proto.number - 1, 1130ffe3c632Sopenharmony_ci field_proto.number, field_proto.type, 1131ffe3c632Sopenharmony_ci FieldDescriptor.ProtoTypeToCppProtoType(field_proto.type), 1132ffe3c632Sopenharmony_ci field_proto.label, None, nested_desc, enum_desc, None, False, None, 1133ffe3c632Sopenharmony_ci options=_OptionsOrNone(field_proto), has_default_value=False, 1134ffe3c632Sopenharmony_ci json_name=json_name, create_key=_internal_create_key) 1135ffe3c632Sopenharmony_ci fields.append(field) 1136ffe3c632Sopenharmony_ci 1137ffe3c632Sopenharmony_ci desc_name = '.'.join(full_message_name) 1138ffe3c632Sopenharmony_ci return Descriptor(desc_proto.name, desc_name, None, None, fields, 1139ffe3c632Sopenharmony_ci list(nested_types.values()), list(enum_types.values()), [], 1140ffe3c632Sopenharmony_ci options=_OptionsOrNone(desc_proto), 1141ffe3c632Sopenharmony_ci create_key=_internal_create_key) 1142