Convert multiple proto files at once with python

I'm a little addicted to it, so I'll write it down as a memorandum.

Thing you want to do

--I want to properly convert multiple protos that have dependencies --I don't know what arguments to pass to protoc.main ()

What i did

Looking back on the contents of hands-on

The following two methods are supported to convert proto to python.

Hit directly from the shell

$ python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. helloworld.proto

Call from within python

from import protoc


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.

Argument confirmation of protoc.main ()

from import protoc

    '',                       #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 ().

When writing solid

All you have to do is add more and more to your butt.

from import protoc


When using a list

As mentioned above, using glob.glob () is smooth.

from import protoc
import glob

protos = glob.glob('hoge/**/*.proto', recursive=True)


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

Convert multiple proto files at once with python
Convert memo at once with Python 2to3
Rsync multiple files at once
Convert HEIC files to PNG files with Python
Generate multiple HTML files at once by pouring JSON data into an HTML template with Python
Erase image files at once with one liner
Update multiple tables at once with pandas to_sql
Get corporate number at once via gbizinfo with python
Beginners try to convert Word files to PDF at once
Remove headings from multiple format CSV files with python
Sorting image files with Python (2)
Sort huge files with python
Sorting image files with Python (3)
Sorting image files with Python
Integrate PDF files with Python
Reading .txt files with Python
[Python] Send gmail with python: Send one by one with multiple image files attached
Create multiple users with serial numbers at once with Ansible Playbook
Cropping images at once [python] [Pillow]
Manipulating EAGLE .brd files with Python
Load multiple JavaScript files with PyWebView
[Python] POST wav files with requests [POST]
Decrypt files encrypted with OpenSSL with Python 3
Convert list to DataFrame with python
Post multiple Twitter images with python
Animate multiple still images with Python
Handle Excel CSV files with Python
Read files in parallel with Python
Replace all at once with sed
Multiple integrals with Python and Sympy
[Python] Creating multiple windows with Tkinter
Convert files written in python etc. to pdf with syntax highlighting
Create multiple users with serial numbers at once in Ansible Playbook: Part 2
Play audio files from Python with interrupts
Send newsletters all at once with Gmail
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Decrypt files encrypted with openssl from python with openssl
Reading and writing JSON files with Python
Download files on the web with Python
Process multiple lists with for in Python
Get out of multiple loops at once
Question: Multiple integrals with python don't work
Convert Chinese numerals to Arabic numerals with Python
[Easy Python] Reading Excel files with pandas
Combine multiple python files into one python file
Manage multiple Python versions with update-alternatives (Ubuntu)
Sample to convert image to Wavelet with Python
[Python] How to save images on the Web at once with Beautiful Soup
Convert images in multiple folders to different pdfs for each folder at once
Turn multiple lists with a for statement at the same time in Python
[Python] Get the files in a folder with Python
Convert FBX files to ASCII <-> BINARY in Python
Convert PDF to image (JPEG / PNG) with Python
Convert PDFs to images in bulk with Python
[Python & Unix] Combine multiple PDF files into one.
[Python] How to draw multiple graphs with Matplotlib
Convert svg file to png / ico with Python
Manage Python multiple version environment with Pythonz, virtualenv
Recursively search all files with multiple specified extensions
Convert Windows epoch values to date with python