|
19 | 19 |
|
20 | 20 | import collections |
21 | 21 | import dataclasses |
| 22 | +import keyword |
| 23 | +import os |
22 | 24 | import sys |
23 | 25 | from itertools import chain |
24 | | -from typing import Callable, Dict, FrozenSet, Mapping, Sequence, Set, Tuple |
| 26 | +from typing import Callable, Container, Dict, FrozenSet, Mapping, Sequence, Set, Tuple |
25 | 27 |
|
26 | 28 | from google.api_core import exceptions # type: ignore |
27 | 29 | from google.longrunning import operations_pb2 # type: ignore |
@@ -204,10 +206,24 @@ def build(cls, |
204 | 206 | file_descriptors, |
205 | 207 | ), opts=opts) |
206 | 208 |
|
| 209 | + def disambiguate_keyword_fname( |
| 210 | + full_path: str, |
| 211 | + visited_names: Container[str]) -> str: |
| 212 | + path, fname = os.path.split(full_path) |
| 213 | + name, ext = os.path.splitext(fname) |
| 214 | + if name in keyword.kwlist or full_path in visited_names: |
| 215 | + name += "_" |
| 216 | + full_path = os.path.join(path, name + ext) |
| 217 | + if full_path in visited_names: |
| 218 | + return disambiguate_keyword_fname(full_path, visited_names) |
| 219 | + |
| 220 | + return full_path |
| 221 | + |
207 | 222 | # Iterate over each FileDescriptorProto and fill out a Proto |
208 | 223 | # object describing it, and save these to the instance. |
209 | 224 | protos: Dict[str, Proto] = {} |
210 | 225 | for fd in file_descriptors: |
| 226 | + fd.name = disambiguate_keyword_fname(fd.name, protos) |
211 | 227 | protos[fd.name] = _ProtoBuilder( |
212 | 228 | file_descriptor=fd, |
213 | 229 | file_to_generate=fd.package.startswith(package), |
|
0 commit comments