1- """
2- Fix absolute imports in *_pb2_grpc.py files.
1+ """Fix absolute imports in *_pb2_grpc.py files.
2+
33Example:
44import a2a_pb2 as a2a__pb2
55from . import a2a_pb2 as a2a__pb2
@@ -16,40 +16,40 @@ def process_generated_code(src_folder: str = 'src/a2a/grpc'):
1616 dir_path = Path (src_folder )
1717 print (dir_path )
1818 if not dir_path .is_dir ():
19- print ('Source folder not found' )
20- sys .exit (1 )
19+ print ('Source folder not found' )
20+ sys .exit (1 )
2121
2222 grpc_pattern = '**/*_pb2_grpc.py'
2323 files = dir_path .glob (grpc_pattern )
2424
2525 for file in files :
26- print (f'Processing { file } ' )
27- try :
28- with file .open ('r' , encoding = 'utf-8' ) as f :
29- src_content = f .read ()
30-
31- # Change import a2a_pb2 as a2a__pb2
32- import_pattern = r'^import (\w+_pb2) as (\w+__pb2)$'
33- # to from . import a2a_pb2 as a2a__pb2
34- replacement_pattern = r'from . import \1 as \2'
35-
36- fixed_src_content = re .sub (
37- import_pattern ,
38- replacement_pattern ,
39- src_content ,
40- flags = re .MULTILINE ,
41- )
42-
43- if fixed_src_content != src_content :
44- with file .open ('w' , encoding = 'utf-8' ) as f :
45- f .write (fixed_src_content )
46- print ('Imports fixed' )
47- else :
48- print ('No changes needed' )
49-
50- except Exception as e :
51- print (f'Error processing file { file } : { e } ' )
52- sys .exit (1 )
26+ print (f'Processing { file } ' )
27+ try :
28+ with file .open ('r' , encoding = 'utf-8' ) as f :
29+ src_content = f .read ()
30+
31+ # Change import a2a_pb2 as a2a__pb2
32+ import_pattern = r'^import (\w+_pb2) as (\w+__pb2)$'
33+ # to from . import a2a_pb2 as a2a__pb2
34+ replacement_pattern = r'from . import \1 as \2'
35+
36+ fixed_src_content = re .sub (
37+ import_pattern ,
38+ replacement_pattern ,
39+ src_content ,
40+ flags = re .MULTILINE ,
41+ )
42+
43+ if fixed_src_content != src_content :
44+ with file .open ('w' , encoding = 'utf-8' ) as f :
45+ f .write (fixed_src_content )
46+ print ('Imports fixed' )
47+ else :
48+ print ('No changes needed' )
49+
50+ except Exception as e :
51+ print (f'Error processing file { file } : { e } ' )
52+ sys .exit (1 )
5353
5454
5555if __name__ == '__main__' :
0 commit comments