Last active
March 23, 2022 00:01
-
-
Save Olshansk/e237fea7645b0f094a808155c4df7fe6 to your computer and use it in GitHub Desktop.
Proto CameCase to snake_case: Install dependencies and run`python3 proto_camel_to_snake.py --help`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import inflection | |
import os | |
import fire | |
from pathlib import Path | |
import re | |
def protoSnakeCase(search=None, exclude=None): | |
"""Recursively search and convert all camelCase attributes to snake_case in .proto files. | |
Args: | |
search (str): Required - the path to search for .proto files. | |
exclude (str): Optional - the path to exclude from the search. | |
""" | |
if search is None: | |
exit("The search path must be specified") | |
search_path = os.path.expanduser(search) | |
if not os.path.exists(search_path): | |
exit("The path to search does not exist") | |
exclude_path = os.path.expanduser(exclude) if exclude is not None else None | |
for path in Path(search_path).rglob("*.proto"): | |
if path.is_file() and ( | |
exclude_path is None or not str(path).startswith(exclude_path) | |
): | |
with open(path, "r") as f: | |
print(path) | |
content = f.read() | |
contentAlt = re.sub( | |
"(.*) (.*) = (\d+);", | |
lambda m: f"{m.group(1)} {inflection.underscore(m.group(2))} = {m.group(3)};", | |
content, | |
) | |
with open(path, "w") as f: | |
f.write(contentAlt) | |
if __name__ == "__main__": | |
fire.Fire(protoSnakeCase) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment