Skip to content

Commit 7d2b704

Browse files
Revathyvenugopal162pyansys-ci-botpre-commit-ci[bot]
authored
feat: add autosummary using jinja to all auto keywords files (#820)
Co-authored-by: pyansys-ci-bot <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 813f8f7 commit 7d2b704

File tree

8 files changed

+15925
-19016
lines changed

8 files changed

+15925
-19016
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,3 @@ cython_debug/
165165
doc/source/examples/
166166
doc/source/API/_autosummary/
167167
doc/source/api
168-
doc/source/keyword_classes/api
169-
doc/source/keyword_classes/_build/

codegen/generate.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,11 @@ def get_keywords_to_generate(kwd_name: typing.Optional[str] = None) -> typing.Li
186186
return keywords
187187

188188

189-
def generate_classes(lib_path: str, kwd_name: typing.Optional[str] = None) -> None:
189+
def generate_classes(lib_path: str, kwd_name: typing.Optional[str] = None, autodoc_output_path: str = "") -> None:
190190
"""Generates the keyword classes, importer, and type-mapper
191191
if kwd_name is not None, this only generates that particular keyword class
192192
"""
193+
autodoc_entries = []
193194
env = Environment(loader=get_loader(), trim_blocks=True, lstrip_blocks=True)
194195
if not os.path.exists(os.path.join(lib_path, "auto")):
195196
os.mkdir(os.path.join(lib_path, "auto"))
@@ -200,7 +201,16 @@ def generate_classes(lib_path: str, kwd_name: typing.Optional[str] = None) -> No
200201
continue
201202
if data_model.is_aliased(name):
202203
continue
203-
generate_class(env, lib_path, item)
204+
classname, filename = generate_class(env, lib_path, item)
205+
autodoc_entries.append((classname, filename))
206+
207+
if autodoc_output_path:
208+
os.makedirs(autodoc_output_path, exist_ok=True)
209+
rst_template = env.get_template("autodoc_rst.jinja")
210+
combined_rst = rst_template.render(entries=autodoc_entries)
211+
combined_filepath = os.path.join(autodoc_output_path, "index.rst")
212+
with open(combined_filepath, "w", encoding="utf-8") as f:
213+
f.write(combined_rst)
204214
keywords_list.extend(get_undefined_alias_keywords(keywords_list))
205215
if kwd_name == None:
206216
generate_entrypoints(env, lib_path, keywords_list)
@@ -224,6 +234,12 @@ def load_inputs(this_folder, args):
224234
def run_codegen(args):
225235
output = args.output
226236
this_folder = get_this_folder()
237+
autodoc_path = args.autodoc_path
238+
if not autodoc_path:
239+
autodoc_path = this_folder.parent / "doc" / "source" / "_autosummary"
240+
if not os.path.exists(autodoc_path):
241+
os.makedirs(autodoc_path)
242+
autodoc_path = str(autodoc_path.resolve())
227243
if args.output == "":
228244
output = this_folder.parent / "src" / "ansys" / "dyna" / "core" / "keywords" / "keyword_classes"
229245
output = str(output.resolve())
@@ -232,16 +248,15 @@ def run_codegen(args):
232248
if args.clean:
233249
clean(output)
234250
return
235-
236251
load_inputs(this_folder, args)
237252
if args.keyword == "":
238253
kwd = None
239254
print(f"Generating code for all keywords")
240-
generate_classes(output)
255+
generate_classes(output, autodoc_output_path=autodoc_path)
241256
else:
242257
kwd = args.keyword
243258
print(f"Generating code for {kwd}")
244-
generate_classes(output, kwd)
259+
generate_classes(output, kwd, autodoc_output_path=autodoc_path)
245260

246261

247262
def parse_args():
@@ -276,6 +291,12 @@ def parse_args():
276291
default="",
277292
help="Path to additional cards file, defaults to additional-cards.json in the same folder as this file.",
278293
)
294+
parser.add_argument(
295+
"autodoc_path",
296+
default="",
297+
nargs="?",
298+
help="Path to the autodoc output folder. Defaults to doc/source/_autosummary.",
299+
)
279300
return parser.parse_args()
280301

281302

codegen/keyword_generation/generators/class_generator.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ def _get_base_variable(classname: str, keyword: str, keyword_options: typing.Dic
376376
return data
377377

378378

379-
def generate_class(env: Environment, lib_path: str, item: typing.Dict) -> None:
379+
def generate_class(env: Environment, lib_path: str, item: typing.Dict) -> typing.Tuple[str, str]:
380380
keyword = item["name"]
381381
fixed_keyword = fix_keyword(keyword)
382382
classname = item["options"].get("classname", get_classname(fixed_keyword))
@@ -386,6 +386,7 @@ def generate_class(env: Environment, lib_path: str, item: typing.Dict) -> None:
386386
filename = os.path.join(lib_path, "auto", fixed_keyword.lower() + ".py")
387387
with open(filename, "w", encoding="utf-8") as f:
388388
f.write(env.get_template("keyword.j2").render(**jinja_variable))
389+
return classname, fixed_keyword.lower()
389390
except Exception as e:
390391
print(f"Failure in generating {classname}")
391392
raise e
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.. _autodoc_all:
2+
3+
All Keyword Class Summaries
4+
###########################
5+
6+
{% for classname, filename in entries %}
7+
8+
{{ classname }}
9+
{{ "=" * classname|length }}
10+
11+
.. automodule:: ansys.dyna.core.keywords.keyword_classes.auto.{{ filename }}
12+
:members:
13+
14+
{% endfor %}

doc/changelog/820.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add autosummary using jinja to all auto keywords files

0 commit comments

Comments
 (0)