I'm a little addicted to it, so I'll write it down as a memorandum.
--I want to properly convert multiple protos that have dependencies
--I don't know what arguments to pass to protoc.main ()
The following two methods are supported to convert proto to python.
$ python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. helloworld.proto
from grpc.tools import protoc
protoc.main((
'',
'-I.',
'--python_out=.',
'--grpc_python_out=.',
'helloworld.proto',
))
From the conclusion, there is no function like "specify a directory and read proto recursively", and it is necessary to specify proto one by one as an argument.
Therefore, it is not realistic to do it in the shell, and it can be assumed that using python's glob.glob ()
is a shortcut.
from grpc.tools import protoc
protoc.main((
'', #Magic
'-I.', #Specifying a directory to scan proto--proto_path=***May be
'--python_out=.', # ***_pb2.Storage location of py
'--grpc_python_out=.', # ***_pb2_grpc.It seems that there is almost no advantage to separate it from the storage location of py ↑
'helloworld.proto', #Specifying the proto file to convert
))
That is, pass a tuple to protoc.main ()
.
All you have to do is add more and more to your butt.
from grpc.tools import protoc
protoc.main((
'',
'-I.',
'--python_out=.',
'--grpc_python_out=.',
'helloworld.proto',
'hoge.proto',
'fuga.proto',
'poyo.proto',
'piyo.proto'
))
As mentioned above, using glob.glob ()
is smooth.
from grpc.tools import protoc
import glob
protos = glob.glob('hoge/**/*.proto', recursive=True)
protoc.main((
'',
'-I.',
'--python_out=.',
'--grpc_python_out=.',
'helloworld.proto',
*protos
))
What is *
! ?? If you think, you can google with "python variable length argument".
To put it plainly, it's a convenient one that expands to multiple variables by adding an asterisk to the beginning of the list.
Recommended Posts