1+ """Generated definition of python_grpc_library.""" 
2+ 
3+ load ("@rules_proto_grpc//:defs.bzl" , "bazel_build_rule_common_attrs" , "proto_compile_attrs" )
4+ load ("@pypi//:requirements.bzl" , "requirement" )
5+ load ("@rules_python//python:defs.bzl" , "py_library" )
6+ load (":python_grpc_compile.bzl" , "python_grpc_compile" )
7+ 
8+ def  python_grpc_library (name , generate_pyi  =  False , ** kwargs ):
9+     """ 
10+     python_grpc_library generates Python code from proto and gRPC, and creates a py_library for them. 
11+ 
12+     Args: 
13+         name: the name of the target. 
14+         generate_pyi: flag to specify whether .pyi files should be created. 
15+         **kwargs: common Bazel attributes will be passed to both python_grpc_compile and py_library; 
16+         python_grpc_compile attributes will be passed to python_grpc_compile only. 
17+     """ 
18+ 
19+     # Compile protos 
20+     name_pb  =  name  +  "_pb" 
21+     python_grpc_compile (
22+         name  =  name_pb ,
23+         ** {
24+             k : v 
25+             for  (k , v ) in  kwargs .items ()
26+             if  k  in  proto_compile_attrs .keys () or 
27+                k  in  bazel_build_rule_common_attrs 
28+         }  # Forward args 
29+     )
30+ 
31+     # For other code to import generated code with prefix_path if it's given 
32+     output_mode  =  kwargs .get ("output_mode" , "PREFIXED" )
33+     if  output_mode  ==  "PREFIXED" :
34+         imports  =  [name_pb ]
35+     else :
36+         imports  =  ["." ]
37+ 
38+     # for pb2_grpc.py to import pb2.py 
39+     prefix_path  =  kwargs .get ("prefix_path" , None )
40+     if  prefix_path :
41+         imports .append (imports [0 ] +  "/"  +  prefix_path )
42+ 
43+     # Create python library 
44+     py_library (
45+         name  =  name ,
46+         srcs  =  [name_pb ],
47+         deps  =  GRPC_DEPS  +  kwargs .get ("deps" , []),
48+         data  =  kwargs .get ("data" , []),  # See https://github.com/rules-proto-grpc/rules_proto_grpc/issues/257 for use case 
49+         imports  =  imports ,
50+         ** {
51+             k : v 
52+             for  (k , v ) in  kwargs .items ()
53+             if  k  in  bazel_build_rule_common_attrs 
54+         }  # Forward Bazel common args 
55+     )
56+ 
57+ GRPC_DEPS  =  [
58+     Label (requirement ("grpcio" )),
59+     Label (requirement ("protobuf" )),
60+ ]
0 commit comments