Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update patch.py #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions protobuf_gen/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def rename_protobuf_imports(dir_root, root, do_not_replace=DO_NO_REPLACE):
pattern = r'^from ([^ ]+) import ([^ ]+)_pb2 as ([^ ]+)__pb2$'
pattern = r'^(?:from ([^ ]+) )?import ([^ ]+)_pb2 as ([^ ]+)__pb2$'
pattern = re.compile(pattern)

for path, _, files in os.walk(dir_root):
Expand All @@ -35,9 +35,13 @@ def rename_protobuf_imports(dir_root, root, do_not_replace=DO_NO_REPLACE):
with open(os.path.join(path, file), 'w+') as f:
for line in lines:
match = pattern.match(line)
if match and match.group(1) not in do_not_replace:
if match and (match.group(1) or match.group(2)) not in do_not_replace:
changes += 1
f.write(f'from {root}.{match.group(1)} import {match.group(2)}_pb2 as {match.group(3)}__pb2\n')
from_clause, import_clause, as_clause = match.groups()
if from_clause:
f.write(f'from {root}.{from_clause} import {import_clause}_pb2 as {as_clause}__pb2\n')
else:
f.write(f'{root}.{import_clause}_pb2 as {as_clause}__pb2\n')
else:
f.write(line)

Expand Down