3 SLj@sNdZdZddlmZddlmZddlmZGdddeZeZdd Z d S) aoProvides a factory class for generating dynamic messages. The easiest way to use this class is if you have access to the FileDescriptor protos containing the messages you want to create you can just do the following: message_classes = message_factory.GetMessages(iterable_of_file_descriptors) my_proto_instance = message_classes['some.proto.package.MessageName']() z"matthewtoia@google.com (Matt Toia))descriptor_pool)message) reflectionc@s*eZdZdZd ddZddZddZdS) MessageFactoryz@Factory for creating Proto2 messages from descriptors in a pool.NcCs|p tj|_i|_dS)zInitializes a new factory.N)rZDescriptorPoolpool_classes)selfrr %/usr/lib/python3.6/message_factory.py__init__2szMessageFactory.__init__cCs||jkr|j}ttkr&|jjdd}tj|tjf|dd}||j|<x |j D]}|j rP|j |j qPWx>|j j D]2}|j|jkr|j |j|j|j}|j|qtW|j|S)aDBuilds a proto2 message class based on the passed in descriptor. Passing a descriptor with a fully qualified name matching a previous invocation will cause the same class to be returned. Args: descriptor: The descriptor to build from. Returns: A class describing the passed in descriptor. asciiignoreN) DESCRIPTOR __module__)rnamestrbytesencoderZGeneratedProtocolMessageTyperZMessageZfieldsZ message_type GetPrototyper extensionscontaining_typeRegisterExtension)rZ descriptorZdescriptor_nameZ result_classZfield extensionextended_classr r r r9s$       zMessageFactory.GetPrototypecCsi}x~|D]v}|jj|}x"|jjD]}|j|||j<q&Wx@|jjD]2}|j|jkrf|j|j|j|j}|j |qJWq W|S)aGets all the messages from a specified file. This will find and resolve dependencies, failing if the descriptor pool cannot satisfy them. Args: files: The file names to extract messages from. Returns: A dictionary mapping proto names to the message classes. This will include any dependent messages as well as any messages defined in the same file as a specified message. ) rZFindFileByNameZmessage_types_by_namevaluesrZ full_nameZextensions_by_namerrr)rfilesresult file_nameZ file_descZdescrrr r r GetMessagesYs      zMessageFactory.GetMessages)N)__name__r __qualname____doc__r rrr r r r r/s  rcsJdd|Dfddxr4jdqWtjdd|DS)a^Builds a dictionary of all the messages available in a set of files. Args: file_protos: Iterable of FileDescriptorProto to build messages out of. Returns: A dictionary mapping proto names to the message classes. This will include any dependent messages as well as any messages defined in the same file as a specified message. cSsi|] }||jqSr )r).0 file_protor r r szGetMessages..cs6x$|jD]}|krj|qWtjj|dS)N) dependencypop_FACTORYrZAdd)r#r%)_AddFile file_by_namer r r(s zGetMessages.._AddFilecSsg|] }|jqSr )r)r"r#r r r szGetMessages..)popitemr'r)Z file_protosr )r(r)r rs rN) r! __author__Zgoogle.protobufrrrobjectrr'rr r r r &s   O