21
21
import os
22
22
import shutil
23
23
import subprocess
24
+
25
+ import jinja2
24
26
import sys
25
27
import textwrap
26
28
from functools import lru_cache
@@ -139,18 +141,22 @@ def post_process_historically_publicised_methods(stub_file_path: Path, line: str
139
141
new_lines .append (line )
140
142
141
143
142
- def post_process_generated_stub_file (stub_file_path : Path , lines : list [str ], patch_historical_methods = False ):
144
+ def post_process_generated_stub_file (
145
+ module_name : str , stub_file_path : Path , lines : list [str ], patch_historical_methods = False
146
+ ):
143
147
"""
144
148
Post process the stub file - add the preamble and optionally patch historical methods.
145
149
Adding preamble always, makes sure that we can update the preamble and have it automatically updated
146
150
in generated files even if no API specification changes.
147
151
152
+ :param module_name: name of the module of the file
148
153
:param stub_file_path: path of the stub fil
149
154
:param lines: lines that were read from the file (with stripped comments)
150
155
:param patch_historical_methods: whether we should patch historical methods
151
156
:return: resulting lines of the file after post-processing
152
157
"""
153
- new_lines = PREAMBLE .splitlines ()
158
+ template = jinja2 .Template (PREAMBLE )
159
+ new_lines = template .render (module_name = module_name ).splitlines ()
154
160
for line in lines :
155
161
if patch_historical_methods :
156
162
post_process_historically_publicised_methods (stub_file_path , line , new_lines )
@@ -159,28 +165,39 @@ def post_process_generated_stub_file(stub_file_path: Path, lines: list[str], pat
159
165
return new_lines
160
166
161
167
162
- def read_pyi_file_content (pyi_file_path : Path , patch_historical_methods = False ) -> list [str ] | None :
168
+ def read_pyi_file_content (
169
+ module_name : str , pyi_file_path : Path , patch_historical_methods = False
170
+ ) -> list [str ] | None :
163
171
"""
164
172
Reads stub file content with post-processing and optionally patching historical methods. The comments
165
- are stripped and preamble is always added. It makes sure that we can update the preamble
166
- and have it automatically updated in generated files even if no API specification changes.
173
+ and initial javadoc are stripped and preamble is always added. It makes sure that we can update
174
+ the preamble and have it automatically updated in generated files even if no API specification changes.
167
175
168
176
If None is returned, the file should be deleted.
169
177
170
- :param pyi_file_path:
171
- :param patch_historical_methods:
178
+ :param module_name: name of the module in question
179
+ :param pyi_file_path: the path of the file to read
180
+ :param patch_historical_methods: whether the historical methods should be patched
172
181
:return: list of lines of post-processed content or None if the file should be deleted.
173
182
"""
174
- lines = [
183
+ lines_no_comments = [
175
184
line
176
185
for line in pyi_file_path .read_text (encoding = "utf-8" ).splitlines ()
177
186
if line .strip () and not line .strip ().startswith ("#" )
178
187
]
188
+ remove_docstring = False
189
+ lines = []
190
+ for line in lines_no_comments :
191
+ if line .strip ().startswith ('"""' ):
192
+ remove_docstring = not remove_docstring
193
+ continue
194
+ if not remove_docstring :
195
+ lines .append (line )
179
196
if (pyi_file_path .name == "__init__.pyi" ) and lines == []:
180
197
console .print (f"[yellow]Skip { pyi_file_path } as it is an empty stub for __init__.py file" )
181
198
return None
182
199
return post_process_generated_stub_file (
183
- pyi_file_path , lines , patch_historical_methods = patch_historical_methods
200
+ module_name , pyi_file_path , lines , patch_historical_methods = patch_historical_methods
184
201
)
185
202
186
203
@@ -194,7 +211,10 @@ def compare_stub_files(generated_stub_path: Path, force_override: bool) -> tuple
194
211
_removals , _additions = 0 , 0
195
212
rel_path = generated_stub_path .relative_to (OUT_DIR )
196
213
target_path = PROVIDERS_ROOT / rel_path
197
- generated_pyi_content = read_pyi_file_content (generated_stub_path , patch_historical_methods = True )
214
+ module_name = "airflow.providers." + os .fspath (rel_path .with_suffix ("" )).replace (os .path .sep , "." )
215
+ generated_pyi_content = read_pyi_file_content (
216
+ module_name , generated_stub_path , patch_historical_methods = True
217
+ )
198
218
if generated_pyi_content is None :
199
219
os .unlink (generated_stub_path )
200
220
if target_path .exists ():
@@ -217,7 +237,7 @@ def compare_stub_files(generated_stub_path: Path, force_override: bool) -> tuple
217
237
console .print (f"[yellow]New file { target_path } has been missing. Treated as addition." )
218
238
target_path .write_text ("\n " .join (generated_pyi_content ), encoding = "utf-8" )
219
239
return 0 , 1
220
- target_pyi_content = read_pyi_file_content (target_path , patch_historical_methods = False )
240
+ target_pyi_content = read_pyi_file_content (module_name , target_path , patch_historical_methods = False )
221
241
if target_pyi_content is None :
222
242
target_pyi_content = []
223
243
if generated_pyi_content != target_pyi_content :
@@ -288,6 +308,10 @@ def compare_stub_files(generated_stub_path: Path, force_override: bool) -> tuple
288
308
#
289
309
# You can read more in the README_API.md file
290
310
#
311
+ \" \" \"
312
+ Definition of the public interface for {{ module_name }}
313
+ isort:skip_file
314
+ \" \" \"
291
315
"""
292
316
293
317
0 commit comments