예제 #1
0
  def __init__(self, name, package, options=None,
               serialized_options=None, serialized_pb=None,
               dependencies=None, public_dependencies=None,
               syntax=None, pool=None):
    """Constructor."""
    super(FileDescriptor, self).__init__(
        options, serialized_options, 'FileOptions')

    if pool is None:
      from google.net.proto2.python.public import descriptor_pool
      pool = descriptor_pool.Default()
    self.pool = pool
    self.message_types_by_name = {}
    self.name = name
    self.package = package
    self.syntax = syntax or "proto2"
    self.serialized_pb = serialized_pb

    self.enum_types_by_name = {}
    self.extensions_by_name = {}
    self.services_by_name = {}
    self.dependencies = (dependencies or [])
    self.public_dependencies = (public_dependencies or [])

    if (api_implementation.Type() == 'cpp' and
        self.serialized_pb is not None):
      _message.default_pool.AddSerializedFile(self.serialized_pb)
예제 #2
0
    def __init__(self,
                 name,
                 package,
                 options=None,
                 serialized_options=None,
                 serialized_pb=None,
                 dependencies=None,
                 public_dependencies=None,
                 syntax=None,
                 pool=None,
                 create_key=None):
        """Constructor."""
        if create_key is not _internal_create_key:
            _Deprecated('FileDescriptor')

        super(FileDescriptor, self).__init__(options, serialized_options,
                                             'FileOptions')

        if pool is None:
            from google.net.proto2.python.public import descriptor_pool
            pool = descriptor_pool.Default()
        self.pool = pool
        self.message_types_by_name = {}
        self.name = name
        self.package = package
        self.syntax = syntax or "proto2"
        self.serialized_pb = serialized_pb

        self.enum_types_by_name = {}
        self.extensions_by_name = {}
        self.services_by_name = {}
        self.dependencies = (dependencies or [])
        self.public_dependencies = (public_dependencies or [])
예제 #3
0
def _BuildMessageFromTypeName(type_name, descriptor_pool):
  """Returns a protobuf message instance.

  Args:
    type_name: Fully-qualified protobuf  message type name string.
    descriptor_pool: DescriptorPool instance.

  Returns:
    A Message instance of type matching type_name, or None if the a Descriptor
    wasn't found matching type_name.
  """

  if descriptor_pool is None:
    from google.net.proto2.python.public import descriptor_pool as pool_mod
    descriptor_pool = pool_mod.Default()
  from google.net.proto2.python.public import symbol_database
  database = symbol_database.Default()
  try:
    message_descriptor = descriptor_pool.FindMessageTypeByName(type_name)
  except KeyError:
    return None
  message_type = database.GetPrototype(message_descriptor)
  return message_type()
예제 #4
0
    Raises:
      KeyError: if a file could not be found.
    """
        def _GetAllMessages(desc):
            """Walk a message Descriptor and recursively yields all message names."""
            yield desc
            for msg_desc in desc.nested_types:
                for nested_desc in _GetAllMessages(msg_desc):
                    yield nested_desc

        result = {}
        for file_name in files:
            file_desc = self.pool.FindFileByName(file_name)
            for msg_desc in file_desc.message_types_by_name.values():
                for desc in _GetAllMessages(msg_desc):
                    try:
                        result[desc.full_name] = self._classes[desc]
                    except KeyError:

                        pass
        return result


_DEFAULT = SymbolDatabase(pool=descriptor_pool.Default())


def Default():
    """Returns the default SymbolDatabase."""
    return _DEFAULT