1414 "proto/datamodel.proto" ,
1515 "proto/options.proto" ,
1616 "proto/sandbox.proto" ,
17+ "proto/extension.proto" ,
18+ "proto/credential_driver.proto" ,
1719]
1820
19- LINE_REWRITES = {
20- "python/openshell/_proto/openshell_pb2_grpc.py" : [
21- (
22- r"^import openshell_pb2 as openshell__pb2$" ,
23- "from . import openshell_pb2 as openshell__pb2" ,
24- ),
25- (
26- r"^import sandbox_pb2 as sandbox__pb2$" ,
27- "from . import sandbox_pb2 as sandbox__pb2" ,
28- ),
29- ],
30- "python/openshell/_proto/openshell_pb2.py" : [
31- (
32- r"^import datamodel_pb2 as datamodel__pb2$" ,
33- "from . import datamodel_pb2 as datamodel__pb2" ,
34- ),
35- (
36- r"^import options_pb2 as options__pb2$" ,
37- "from . import options_pb2 as options__pb2" ,
38- ),
39- (
40- r"^import sandbox_pb2 as sandbox__pb2$" ,
41- "from . import sandbox_pb2 as sandbox__pb2" ,
42- ),
43- ],
44- "python/openshell/_proto/datamodel_pb2.py" : [
45- (
46- r"^import options_pb2 as options__pb2$" ,
47- "from . import options_pb2 as options__pb2" ,
48- ),
49- (
50- r"^import sandbox_pb2 as sandbox__pb2$" ,
51- "from . import sandbox_pb2 as sandbox__pb2" ,
52- ),
53- ],
54- "python/openshell/_proto/datamodel_pb2_grpc.py" : [
55- (
56- r"^import datamodel_pb2 as datamodel__pb2$" ,
57- "from . import datamodel_pb2 as datamodel__pb2" ,
58- ),
59- ],
60- "python/openshell/_proto/sandbox_pb2.py" : [
61- (
62- r"^import datamodel_pb2 as datamodel__pb2$" ,
63- "from . import datamodel_pb2 as datamodel__pb2" ,
64- ),
65- ],
66- "python/openshell/_proto/sandbox_pb2_grpc.py" : [
67- (
68- r"^import sandbox_pb2 as sandbox__pb2$" ,
69- "from . import sandbox_pb2 as sandbox__pb2" ,
70- ),
71- ],
72- }
73-
7421
7522def main () -> None :
7623 parser = argparse .ArgumentParser (description = __doc__ )
@@ -83,6 +30,7 @@ def main() -> None:
8330 ),
8431 )
8532 args = parser .parse_args ()
33+ output_dir = Path ("python/openshell/_proto" )
8634
8735 subprocess .run (
8836 [
@@ -98,13 +46,19 @@ def main() -> None:
9846 check = True ,
9947 )
10048
101- for path , rules in LINE_REWRITES .items ():
102- file_path = Path (path )
103- text = file_path .read_text ()
104- text = text .replace ("from . from . import" , "from . import" )
105- for pattern , replacement in rules :
106- text = re .sub (pattern , replacement , text , flags = re .MULTILINE )
107- file_path .write_text (text )
49+ # Rewrite only imports of our generated modules, leaving google.protobuf
50+ # imports intact. The same rule covers runtime modules and type stubs.
51+ modules = "|" .join (re .escape (f"{ Path (path ).stem } _pb2" ) for path in PROTO_FILES )
52+ pattern = rf"^import ({ modules } )( as \w+)?$"
53+ suffixes = ["_pb2.py" , "_pb2_grpc.py" ]
54+ if not args .no_pyi :
55+ suffixes .append ("_pb2.pyi" )
56+ for proto in PROTO_FILES :
57+ for suffix in suffixes :
58+ file_path = output_dir / f"{ Path (proto ).stem } { suffix } "
59+ text = file_path .read_text ()
60+ text = re .sub (pattern , r"from . import \1\2" , text , flags = re .MULTILINE )
61+ file_path .write_text (text )
10862
10963
11064if __name__ == "__main__" :
0 commit comments