|
3 | 3 | import re |
4 | 4 | import argparse |
5 | 5 | from inject_mixin_skip import MIXIN_SKIP_PATTERNS, FUNCTION_SKIP_PATTERNS |
| 6 | +from inject_replace import MIXIN_REPLACEMENT_PATTERNS, FUNCTION_REPLACEMENT_PATTERNS |
6 | 7 |
|
7 | 8 |
|
8 | 9 | def parse_annotations(source_dir): |
@@ -176,6 +177,31 @@ def inject_mixins(target_dir, mixins_map, dry_run=False): |
176 | 177 | already_present = True |
177 | 178 |
|
178 | 179 | if not already_present and anno_lines: |
| 180 | + # --- Apply Regex Replacements (Mixins) --- |
| 181 | + replacement_rules = MIXIN_REPLACEMENT_PATTERNS.get( |
| 182 | + mixin_name, [] |
| 183 | + ) |
| 184 | + if replacement_rules: |
| 185 | + temp_lines = [] |
| 186 | + for al_line in anno_lines: |
| 187 | + replaced = False |
| 188 | + for pattern, replacement in replacement_rules: |
| 189 | + if re.match(pattern, al_line): |
| 190 | + new_line = re.sub( |
| 191 | + pattern, replacement, al_line |
| 192 | + ) |
| 193 | + if al_line.endswith( |
| 194 | + "\n" |
| 195 | + ) and not new_line.endswith("\n"): |
| 196 | + new_line += "\n" |
| 197 | + temp_lines.append(new_line) |
| 198 | + replaced = True |
| 199 | + break |
| 200 | + if not replaced: |
| 201 | + temp_lines.append(al_line) |
| 202 | + anno_lines = temp_lines |
| 203 | + # ----------------------------------------- |
| 204 | + |
179 | 205 | if dry_run: |
180 | 206 | print( |
181 | 207 | f"[Dry Run] Injecting mixin docs for: {mixin_name} in {path} (from {source_file})" |
@@ -427,6 +453,32 @@ def inject_annotations(target_dir, annotations_map, dry_run=False): |
427 | 453 | # Inject annotations |
428 | 454 | # We prepend the indentation found on the function line |
429 | 455 |
|
| 456 | + # --- Apply Regex Replacements (Functions) --- |
| 457 | + replacement_rules = FUNCTION_REPLACEMENT_PATTERNS.get( |
| 458 | + func_name, [] |
| 459 | + ) |
| 460 | + if replacement_rules: |
| 461 | + temp_lines = [] |
| 462 | + for al_line in anno_lines: |
| 463 | + replaced = False |
| 464 | + for pattern, replacement in replacement_rules: |
| 465 | + if re.match(pattern, al_line): |
| 466 | + new_line = re.sub( |
| 467 | + pattern, replacement, al_line |
| 468 | + ) |
| 469 | + # Ensure newline if lost |
| 470 | + if al_line.endswith( |
| 471 | + "\n" |
| 472 | + ) and not new_line.endswith("\n"): |
| 473 | + new_line += "\n" |
| 474 | + temp_lines.append(new_line) |
| 475 | + replaced = True |
| 476 | + break |
| 477 | + if not replaced: |
| 478 | + temp_lines.append(al_line) |
| 479 | + anno_lines = temp_lines |
| 480 | + # -------------------------------------------- |
| 481 | + |
430 | 482 | # Get skip patterns for this function |
431 | 483 | skip_patterns = [ |
432 | 484 | re.compile(p) |
|
0 commit comments