3 SÉLj‡ã@sNdZdZddlmZddlmZddlmZGdd„deƒZeƒZdd „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 dd„Zdd„Zdd„ZdS) ÚMessageFactoryz@Factory for creating Proto2 messages from descriptors in a pool.NcCs|p tjƒ|_i|_dS)zInitializes a new factory.N)rZDescriptorPoolÚpoolÚ_classes)Úselfr©r ú%/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. ÚasciiÚignoreN)Ú DESCRIPTORÚ __module__)rÚnameÚstrÚbytesÚencoderZGeneratedProtocolMessageTyperZMessageZfieldsZ message_typeÚ GetPrototyperÚ extensionsÚcontaining_typeÚRegisterExtension)rZ descriptorZdescriptor_nameZ result_classZfieldÚ extensionÚextended_classr r r r9s$       zMessageFactory.GetPrototypecCsˆi}x~|D]v}|jj|ƒ}x"|jjƒD]}|j|ƒ||j<q&Wx@|jjƒD]2}|j|jkrf|j|jƒ|j|j}|j |ƒqJWq W|S)a£Gets 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_nameÚvaluesrZ full_nameZextensions_by_namerrr)rÚfilesÚresultÚ file_nameZ file_descZdescrrr r r Ú GetMessagesYs      zMessageFactory.GetMessages)N)Ú__name__rÚ __qualname__Ú__doc__r rrr r r r r/s  rcsJdd„|Dƒ‰‡‡fdd„‰xˆr4ˆˆjƒdƒqWtjdd„|DƒƒS)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|] }||j“qSr )r)Ú.0Ú file_protor r r ú ŽszGetMessages..cs6x$|jD]}|ˆkrˆˆj|ƒƒqWtjj|ƒdS)N)Ú dependencyÚpopÚ_FACTORYrZAdd)r#r%)Ú_AddFileÚ file_by_namer r r(�s zGetMessages.._AddFileécSsg|] }|j‘qSr )r)r"r#r r r ú —szGetMessages..)Úpopitemr'r)Z file_protosr )r(r)r r�s rN) r!Ú __author__Zgoogle.protobufrrrÚobjectrr'rr r r r Ú&s   O