2424from __future__ import annotations
2525
2626import argparse
27- from os import PathLike
27+ import os
2828from pathlib import Path
29+ import re
2930
3031from ansys .dpf import core as dpf
3132from ansys .dpf .core .changelog import Changelog
@@ -51,7 +52,7 @@ def __init__(
5152
5253
5354def initialize_server (
54- ansys_path : str | PathLike = None ,
55+ ansys_path : str | os . PathLike = None ,
5556 include_composites : bool = False ,
5657 include_sound : bool = False ,
5758 verbose : bool = False ,
@@ -105,6 +106,91 @@ def initialize_server(
105106 return server
106107
107108
109+ def extract_operator_description_update (content : str ) -> str :
110+ """Extract the updated description to use for an operator.
111+
112+ Parameters
113+ ----------
114+ content:
115+ The contents of the '*-upd.md' file.
116+
117+ Returns
118+ -------
119+ description_update:
120+ The updated description to use for the operator.
121+ """
122+ match = re .search (r"## Description\s*(.*?)\s*(?=## |\Z)" , content , re .DOTALL )
123+ return match .group (0 ) if match else None
124+
125+
126+ def replace_operator_description (original_documentation : str , new_description : str ):
127+ """Replace the original operator description with a new one in the operator documentation file.
128+
129+ Parameters
130+ ----------
131+ original_documentation:
132+ Original operator documentation.
133+ new_description:
134+ New operator description
135+
136+ Returns
137+ -------
138+ updated_documentation:
139+ The updated operator documentation content
140+
141+ """
142+ return re .sub (
143+ r"## Description\s*.*?(?=## |\Z)" , new_description , original_documentation , flags = re .DOTALL
144+ )
145+
146+
147+ def update_operator_descriptions (docs_path : Path ):
148+ """Update operator descriptions based on '*-upd.md' files in DPF documentation sources.
149+
150+ Parameters
151+ ----------
152+ docs_path:
153+ Root path of the DPF documentation to update operator descriptions for.
154+
155+ """
156+ all_md_files = {}
157+ specs_path = docs_path / "operator-specifications"
158+ # Walk through the target directory and all subdirectories
159+ for root , _ , files in os .walk (specs_path ):
160+ for file in files :
161+ if file .endswith (".md" ):
162+ full_path = Path (root ) / file
163+ all_md_files [str (full_path )] = file # Store full path and just filename
164+
165+ for base_path , file_name in all_md_files .items ():
166+ if file_name .endswith ("_upd.md" ):
167+ continue # Skip update files
168+
169+ # Construct the expected update file name and path
170+ upd_file_name = f"{ file_name [:- 3 ]} _upd.md"
171+
172+ # Look for the update file in the same folder
173+ upd_path = Path (base_path ).parent / upd_file_name
174+ if not upd_path .exists :
175+ continue
176+
177+ # Load contents
178+ with Path (base_path ).open (mode = "r" , encoding = "utf-8" ) as bf :
179+ base_content = bf .read ()
180+ with Path (upd_path ).open (mode = "r" , encoding = "utf-8" ) as uf :
181+ upd_content = uf .read ()
182+
183+ # Extract and replace description
184+ new_description = extract_operator_description_update (upd_content )
185+ if new_description :
186+ updated_content = replace_operator_description (base_content , new_description )
187+ with Path (base_path ).open (mode = "w" , encoding = "utf-8" ) as bf :
188+ bf .write (updated_content )
189+ print (f"Updated description for: { file_name } " )
190+ else :
191+ print (f"No operator description found in: { upd_path } " )
192+
193+
108194def fetch_doc_info (server : dpf .AnyServerType , operator_name : str ) -> dict :
109195 """Fetch information about the specifications of a given operator.
110196
@@ -357,7 +443,10 @@ def generate_operators_doc(
357443 operators = get_plugin_operators (server , desired_plugin )
358444 for operator_name in operators :
359445 generate_operator_doc (server , operator_name , include_private , output_path )
446+ # Generate the toc tree
360447 generate_toc_tree (output_path )
448+ # Use update files in output_path
449+ update_operator_descriptions (output_path )
361450
362451
363452def run_with_args (): # pragma: nocover
0 commit comments