U Xzf_H-ã@spdZdZddlmZe ¡dkr,ddlmZGdd„deƒZGdd „d eƒZ Gd d „d e ƒZ Gd d „d e ƒZ dS)a*Contains metaclasses used to create protocol service and service stub classes from ServiceDescriptor objects at runtime. The GeneratedServiceType and GeneratedServiceStubType metaclasses are used to inject all useful functionality into the classes output by the protocol compiler at compile-time. zpetar@google.com (Petar Petrov)é)Úapi_implementationÚcpp)Ú_messagec@seZdZdZdZdd„ZdS)ÚGeneratedServiceTypeaœMetaclass for service classes created at runtime from ServiceDescriptors. Implementations for all methods described in the Service class are added here by this class. We also create properties to allow getting/setting all fields in the protocol message. The protocol compiler currently uses this metaclass to create protocol service classes at runtime. Clients can also manually create their own classes at runtime, as in this example:: mydescriptor = ServiceDescriptor(.....) class MyProtoService(service.Service): __metaclass__ = GeneratedServiceType DESCRIPTOR = mydescriptor myservice_instance = MyProtoService() # ... Ú DESCRIPTORcCsTtj|krdS|tj}t|tƒr8tj |¡}||tj<t|ƒ}| |¡||_ dS)a…Creates a message service class. Args: name: Name of the class (ignored, but required by the metaclass protocol). bases: Base classes of the class being constructed. dictionary: The class dictionary of the class being constructed. dictionary[_DESCRIPTOR_KEY] must contain a ServiceDescriptor object describing this protocol service type. N) rÚ_DESCRIPTOR_KEYÚ isinstanceÚstrrÚ default_poolÚFindServiceByNameÚ_ServiceBuilderÚ BuildServicer)ÚclsÚnameÚbasesÚ dictionaryÚ descriptorZservice_builder©rúz/private/var/folders/n9/53xbvtmd7sjg1q1l55xmpg58n4mgng/T/pip-unpacked-wheel-3cr5c46n/google/protobuf/service_reflection.pyÚ__init__Fs      zGeneratedServiceType.__init__N)Ú__name__Ú __module__Ú __qualname__Ú__doc__rrrrrrr0srcs$eZdZdZdZ‡fdd„Z‡ZS)ÚGeneratedServiceStubTypezÁMetaclass for service stubs created at runtime from ServiceDescriptors. This class has similar responsibilities as GeneratedServiceType, except that it creates the service stub classes. rcsd| |j¡}t|tƒr,tj |¡}||tj<tt|ƒ  |||¡tj|krNdSt |ƒ}|  |¡dS)a`Creates a message service stub class. Args: name: Name of the class (ignored, here). bases: Base classes of the class being constructed. dictionary: The class dictionary of the class being constructed. dictionary[_DESCRIPTOR_KEY] must contain a ServiceDescriptor object describing this protocol service type. N) Úgetrrr rr r rÚsuperrÚ_ServiceStubBuilderÚBuildServiceStub)rrrrrZservice_stub_builder©Ú __class__rrrjs     z!GeneratedServiceStubType.__init__)rrrrrrÚ __classcell__rrrrr`src@sHeZdZdZdd„Zdd„Zdd„Zdd „Zd d „Zd d „Z dd„Z dS)r aDThis class constructs a protocol service class using a service descriptor. Given a service descriptor, this class constructs a class that represents the specified service descriptor. One service builder instance constructs exactly one service class. That means all instances of that class share the same builder. cCs ||_dS)z¥Initializes an instance of the service class builder. Args: service_descriptor: ServiceDescriptor to use when constructing the service class. N©r©ÚselfZservice_descriptorrrrrŒsz_ServiceBuilder.__init__csh‡fdd„}|ˆ_||_t‡fdd„ƒ|_d|j_ˆj|_ˆj|_ˆj j D]}t ||j ˆ  |¡ƒqJdS)z\Constructs the service class. Args: cls: The class that will be constructed. csˆ |||||¡S©N)Ú _CallMethod)ÚsrvcÚmethod_descriptorÚrpc_controllerÚrequestÚcallback©r$rrÚ_WrapCallMethodŸs ÿz5_ServiceBuilder.BuildService.._WrapCallMethodcsˆjSr%r"rr,rrÚ¥óz._ServiceBuilder.BuildService..zReturns the service descriptor.N)rÚ CallMethodÚ staticmethodZ GetDescriptorrÚ_GetRequestClassZGetRequestClassÚ_GetResponseClassZGetResponseClassrÚmethodsÚsetattrrÚ_GenerateNonImplementedMethod)r$rr-Úmethodrr,rr •s  z_ServiceBuilder.BuildServicecCs,|j|jkrtdƒ‚t||jƒ}||||ƒS)a‹Calls the method described by a given method descriptor. Args: srvc: Instance of the service for which this method is called. method_descriptor: Descriptor that represent the method to call. rpc_controller: RPC controller to use for this method's execution. request: Request protocol message. callback: A callback to invoke after the method has completed. z.r©r$r7rrArr6Ýs z-_ServiceBuilder._GenerateNonImplementedMethodcCs| d|¡|dƒdS)a The body of all methods in the generated service class. Args: method_name: Name of the method being executed. rpc_controller: RPC controller used to execute this method. callback: A callback which will be invoked when the method finishes. zMethod %s not implemented.N)Z SetFailed)r$Ú method_namer)r+rrrr>êsz%_ServiceBuilder._NonImplementedMethodN) rrrrrr r&r2r3r6r>rrrrr ‚s  r c@s0eZdZdZdd„Zdd„Zdd„Zdd „Zd S) ra”Constructs a protocol service stub class using a service descriptor. Given a service descriptor, this class constructs a suitable stub class. A stub is just a type-safe wrapper around an RpcChannel which emulates a local implementation of the service. One service stub builder instance constructs exactly one class. It means all instances of that class share the same service stub builder. cCs ||_dS)z§Initializes an instance of the service stub class builder. Args: service_descriptor: ServiceDescriptor to use when constructing the stub class. Nr"r#rrrrsz_ServiceStubBuilder.__init__cCs:dd„}||_||_|jjD]}t||j| |¡ƒqdS)zYConstructs the stub class. Args: cls: The class that will be constructed. cSs ||_dSr%)Ú rpc_channel)ÚstubrDrrrÚ_ServiceStubInitsz>_ServiceStubBuilder.BuildServiceStub.._ServiceStubInitN)rrrr4r5rÚ_GenerateStubMethod)r$rrFr7rrrr s  z$_ServiceStubBuilder.BuildServiceStubcsd‡‡fdd„ S)Ncsˆ |ˆ|||¡Sr%)Ú _StubMethodr?rArrr.sz9_ServiceStubBuilder._GenerateStubMethod..)NrrBrrArrGsz'_ServiceStubBuilder._GenerateStubMethodcCs|j ||||jj|¡S)a‡The body of all service methods in the generated stub class. Args: stub: Stub instance. method_descriptor: Descriptor of the invoked method. rpc_controller: Rpc controller to execute the method. request: Request protocol message. callback: A callback to execute when the method finishes. Returns: Response message (in case of blocking call). )rDr0r=r;)r$rEr(r)r*r+rrrrHs þz_ServiceStubBuilder._StubMethodN)rrrrrrrGrHrrrrrös   rN) rÚ __author__Zgoogle.protobuf.internalrÚTypeZgoogle.protobuf.pyextrÚtyperrÚobjectr rrrrrÚs   0"t