B @`H-@spdZdZddlmZedkr,ddlmZGdddeZGdd d eZ 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_implementationcpp)_messagec@seZdZdZdZddZdS)GeneratedServiceTypeaMetaclass 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|tr8tj|}||tj<t|}||||_ dS)aCreates 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 isinstancestrr default_poolFindServiceByName_ServiceBuilder BuildServicer)clsnamebases dictionary descriptorZservice_builderrF/tmp/pip-unpacked-wheel-3chbcimp/google/protobuf/service_reflection.py__init__Fs      zGeneratedServiceType.__init__N)__name__ __module__ __qualname____doc__rrrrrrr0srcs$eZdZdZdZfddZZS)GeneratedServiceStubTypezMetaclass 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|tr,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 rsuperr_ServiceStubBuilderBuildServiceStub)rrrrrZservice_stub_builder) __class__rrrjs     z!GeneratedServiceStubType.__init__)rrrrrr __classcell__rr)rrr`src@sHeZdZdZddZddZddZdd Zd d Zd d Z ddZ 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)zInitializes an instance of the service class builder. Args: service_descriptor: ServiceDescriptor to use when constructing the service class. N)r)selfservice_descriptorrrrrsz_ServiceBuilder.__init__cslfdd}|_||_tfdd|_d|j_j|_j|_x$j j D]}t ||j |qLWdS)z\Constructs the service class. Args: cls: The class that will be constructed. cs|||||S)N) _CallMethod)srvcmethod_descriptorrpc_controllerrequestcallback)r!rr_WrapCallMethodsz5_ServiceBuilder.BuildService.._WrapCallMethodcsjS)N)rr)r!rrz._ServiceBuilder.BuildService..zReturns the service descriptor.N)r CallMethod staticmethodZ GetDescriptorr_GetRequestClassZGetRequestClass_GetResponseClassZGetResponseClassrmethodssetattrr_GenerateNonImplementedMethod)r!rr)methodr)r!rr s z_ServiceBuilder.BuildServicecCs,|j|jkrtdt||j}||||S)aCalls 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!r3r)r3r!rr2s z-_ServiceBuilder._GenerateNonImplementedMethodcCs|d||ddS)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(rrrr9sz%_ServiceBuilder._NonImplementedMethodN) rrrrrr r#r.r/r2r9rrrrr s  r c@s0eZdZdZddZddZddZdd Zd S) raConstructs 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)zInitializes an instance of the service stub class builder. Args: service_descriptor: ServiceDescriptor to use when constructing the stub class. N)r)r!r"rrrrsz_ServiceStubBuilder.__init__cCs>dd}||_||_x$|jjD]}t||j||qWdS)zYConstructs the stub class. Args: cls: The class that will be constructed. cSs ||_dS)N) rpc_channel)stubr<rrr_ServiceStubInitsz>_ServiceStubBuilder.BuildServiceStub.._ServiceStubInitN)rrrr0r1r_GenerateStubMethod)r!rr>r3rrrr s z$_ServiceStubBuilder.BuildServiceStubcsdfdd S)Ncs||||S)N) _StubMethod)r:r&r'r()r3r!rrr*sz9_ServiceStubBuilder._GenerateStubMethod..)Nr)r!r3r)r3r!rr?sz'_ServiceStubBuilder._GenerateStubMethodcCs|j||||jj|S)aThe 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). )r<r,r8r7)r!r=r%r&r'r(rrrr@s z_ServiceStubBuilder._StubMethodN)rrrrrrr?r@rrrrrs   rN) r __author__Zgoogle.protobuf.internalrTypeZgoogle.protobuf.pyextrtyperrobjectr rrrrr%s   0"t