|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +def extract_operator_description(content): |
| 5 | + match = re.search(r'## Description\s*(.*?)\s*(?=## |\Z)', content, re.DOTALL) |
| 6 | + return match.group(0) if match else None |
| 7 | + |
| 8 | +def replace_operator_description(original, new_desc): |
| 9 | + return re.sub(r'## Description\s*.*?(?=## |\Z)', new_desc, original, flags=re.DOTALL) |
| 10 | + |
| 11 | +def process_operator_files(root_directory): |
| 12 | + all_md_files = {} |
| 13 | + |
| 14 | + # Walk through the target directory and all subdirectories |
| 15 | + for root, _, files in os.walk(root_directory): |
| 16 | + for file in files: |
| 17 | + if file.endswith('.md'): |
| 18 | + full_path = os.path.join(root, file) |
| 19 | + all_md_files[full_path] = file # Store full path and just filename |
| 20 | + |
| 21 | + for base_path, file_name in all_md_files.items(): |
| 22 | + if file_name.endswith('_upd.md'): |
| 23 | + continue # Skip update files |
| 24 | + |
| 25 | + # Construct the expected update file name and path |
| 26 | + name_wo_ext = file_name[:-3] |
| 27 | + upd_file_name = f"{name_wo_ext}_upd.md" |
| 28 | + |
| 29 | + # Look for the update file in the same folder |
| 30 | + upd_path = os.path.join(os.path.dirname(base_path), upd_file_name) |
| 31 | + if not os.path.exists(upd_path): |
| 32 | + print(f"❌ No update file found for: {base_path}") |
| 33 | + continue |
| 34 | + |
| 35 | + # Load contents |
| 36 | + with open(base_path, 'r', encoding='utf-8') as bf: |
| 37 | + base_content = bf.read() |
| 38 | + with open(upd_path, 'r', encoding='utf-8') as uf: |
| 39 | + upd_content = uf.read() |
| 40 | + |
| 41 | + # Extract and replace description |
| 42 | + new_description = extract_operator_description(upd_content) |
| 43 | + if new_description: |
| 44 | + updated_content = replace_operator_description(base_content, new_description) |
| 45 | + with open(base_path, 'w', encoding='utf-8') as bf: |
| 46 | + bf.write(updated_content) |
| 47 | + print(f"✅ Updated: {base_path}") |
| 48 | + else: |
| 49 | + print(f"⚠️ No 'operator_description' found in: {upd_path}") |
| 50 | + |
| 51 | +# Run the script on doc/source/operators_doc |
| 52 | +process_operator_files('doc/source/operators_doc') |
0 commit comments