From ed119952339b664481e0b235b5105753189099a6 Mon Sep 17 00:00:00 2001 From: Harshal Pohekar <106588300+hpohekar@users.noreply.github.com> Date: Thu, 17 Oct 2024 21:31:39 +0530 Subject: [PATCH] feat: Stub file for MeshingUtilities (#3334) * feat: Stub file for MeshingUtilities * stub file autogeneration * working fine * working fine 1 * working fine 2 * working fine 3 * test fix * test fix 1 * Use string io * Update src/ansys/fluent/core/codegen/datamodelgen.py Co-authored-by: Sean Pearson <93727996+seanpearsonuk@users.noreply.github.com> * Remove typing import * error fix * close stub file * remove test code * Use with syntax * test fix * update type checking --------- Co-authored-by: Sean Pearson <93727996+seanpearsonuk@users.noreply.github.com> --- src/ansys/fluent/core/codegen/datamodelgen.py | 61 ++++++++++++++++--- src/ansys/fluent/core/session_meshing.pyi | 5 ++ .../fluent/core/session_pure_meshing.pyi | 5 +- tests/test_codegen.py | 4 +- 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/src/ansys/fluent/core/codegen/datamodelgen.py b/src/ansys/fluent/core/codegen/datamodelgen.py index 20cf9f79d72..781be261648 100644 --- a/src/ansys/fluent/core/codegen/datamodelgen.py +++ b/src/ansys/fluent/core/codegen/datamodelgen.py @@ -1,6 +1,6 @@ """Module to generate Fluent datamodel API classes.""" -from io import FileIO +from io import FileIO, StringIO import os from pathlib import Path import shutil @@ -21,11 +21,11 @@ _PY_TYPE_BY_DM_TYPE = { **dict.fromkeys(["Logical", "Bool"], "bool"), - **dict.fromkeys(["Logical List", "ListBool"], "List[bool]"), + **dict.fromkeys(["Logical List", "ListBool"], "list[bool]"), "String": "str", - **dict.fromkeys(["String List", "ListString"], "List[str]"), + **dict.fromkeys(["String List", "ListString"], "list[str]"), **dict.fromkeys(["Integer", "Int"], "int"), - **dict.fromkeys(["Integer List", "ListInt"], "List[int]"), + **dict.fromkeys(["Integer List", "ListInt"], "list[int]"), "Real": "float", **dict.fromkeys( [ @@ -36,9 +36,9 @@ "Real Triplet List", "ListRealTriplet", ], - "List[float]", + "list[float]", ), - **dict.fromkeys(["Dict", "ModelObject"], "Dict[str, Any]"), + **dict.fromkeys(["Dict", "ModelObject"], "dict[str, Any]"), "None": "None", } @@ -66,6 +66,19 @@ ) +def _write_command_query_stub(name: str, info: Any, f: FileIO): + signature = StringIO() + indent = " " + signature.write(f"(\n{indent}self,\n") + if info.get("args"): + for arg in info.get("args"): + signature.write( + f'{indent}{arg["name"]}: {_PY_TYPE_BY_DM_TYPE[arg["type"]]} | None = None,\n' + ) + signature.write(f'{indent}) -> {_PY_TYPE_BY_DM_TYPE[info["returntype"]]}: ...') + f.write(f"\n def {name}{signature.getvalue()}\n") + + def _build_singleton_docstring(name: str): return f"Singleton {name}." @@ -130,6 +143,8 @@ def __init__( datamodel_dir = (pyfluent.CODEGEN_OUTDIR / f"datamodel_{version}").resolve() datamodel_dir.mkdir(exist_ok=True) self.file_name = (datamodel_dir / f"{rules_save_name}.py").resolve() + if rules == "MeshingUtilities": + self.stub_file = (datamodel_dir / "MeshingUtilities.pyi").resolve() if len(modes) > 1: for mode in modes[1:]: DataModelStaticInfo._noindices.append(f"{mode}.datamodel.{rules}") @@ -316,12 +331,12 @@ def _write_static_info(self, name: str, info: Any, f: FileIO, level: int = 0): # print("\t\texcluded", k) pass for k in parameters: - k_type = _PY_TYPE_BY_DM_TYPE[info["parameters"][k]["type"]] - if k_type in ["str", "List[str]"]: + k_type = info["parameters"][k]["type"] + if k_type in {"String", "String List", "ListString"}: f.write(f"{indent} class {k}(PyTextual):\n") - elif k_type in ["int", "float"]: + elif k_type in {"Integer", "Int", "Real"}: f.write(f"{indent} class {k}(PyNumerical):\n") - elif k_type in ["Dict", "Dict[str, Any]"]: + elif k_type in {"Dict", "ModelObject"}: f.write(f"{indent} class {k}(PyDictionary):\n") else: f.write(f"{indent} class {k}(PyParameter):\n") @@ -333,6 +348,32 @@ def _write_static_info(self, name: str, info: Any, f: FileIO, level: int = 0): f.write(f'{indent} """\n') f.write(f"{indent} pass\n\n") api_tree[k] = "Parameter" + if "MeshingUtilities" in f.name: + stub_file = self._static_info["MeshingUtilities"].stub_file + stub_file.unlink(missing_ok=True) + with open(stub_file, "w", encoding="utf8") as file: + file.write("#\n") + file.write("# This is an auto-generated file. DO NOT EDIT!\n") + file.write("#\n") + file.write("# pylint: disable=line-too-long\n\n") + file.write( + "from ansys.fluent.core.services.datamodel_se import PyMenu\n" + ) + file.write("from typing import Any\n") + file.write("\n\n") + file.write(f"class Root(PyMenu):\n") + for k in commands: + _write_command_query_stub( + k, + info["commands"][k]["commandinfo"], + file, + ) + for k in queries: + _write_command_query_stub( + k, + info["queries"][k]["queryinfo"], + file, + ) for k in commands: f.write(f"{indent} class {k}(PyCommand):\n") f.write(f'{indent} """\n') diff --git a/src/ansys/fluent/core/session_meshing.pyi b/src/ansys/fluent/core/session_meshing.pyi index 33c4c908541..2011f4d25b9 100644 --- a/src/ansys/fluent/core/session_meshing.pyi +++ b/src/ansys/fluent/core/session_meshing.pyi @@ -1,3 +1,6 @@ +from ansys.fluent.core.generated.datamodel_242.MeshingUtilities import ( + Root as meshing_utilities_root, +) from ansys.fluent.core.generated.datamodel_242.PMFileManagement import ( Root as pmfilemanagement_root, ) @@ -17,6 +20,8 @@ class Meshing: @property def meshing(self) -> meshing_root: ... @property + def meshing_utilities(self) -> meshing_utilities_root: ... + @property def workflow(self) -> workflow_root: ... @property def PartManagement(self) -> partmanagement_root: ... diff --git a/src/ansys/fluent/core/session_pure_meshing.pyi b/src/ansys/fluent/core/session_pure_meshing.pyi index 7103d21f76d..5acb7bb6236 100644 --- a/src/ansys/fluent/core/session_pure_meshing.pyi +++ b/src/ansys/fluent/core/session_pure_meshing.pyi @@ -1,3 +1,6 @@ +from ansys.fluent.core.generated.datamodel_242.MeshingUtilities import ( + Root as meshing_utilities_root, +) from ansys.fluent.core.generated.datamodel_242.PMFileManagement import ( Root as pmfilemanagement_root, ) @@ -17,7 +20,7 @@ class PureMeshing: @property def meshing(self) -> meshing_root: ... @property - def meshing_utilities(self) -> MeshingQueries: ... + def meshing_utilities(self) -> meshing_utilities_root: ... @property def workflow(self) -> workflow_root: ... def watertight(self): ... diff --git a/tests/test_codegen.py b/tests/test_codegen.py index e76c876ba27..368a12103ac 100644 --- a/tests/test_codegen.py +++ b/tests/test_codegen.py @@ -330,8 +330,8 @@ def test_codegen_with_datamodel_static_info(monkeypatch, rules): f"datamodel_{version}", } datamodel_paths = list((codegen_outdir / f"datamodel_{version}").iterdir()) - assert len(datamodel_paths) == 1 - assert set(p.name for p in datamodel_paths) == {f"{rules}.py"} + assert len(datamodel_paths) == 1 or 2 + assert set(p.name for p in datamodel_paths) == {f"{rules}.py"} or {f"{rules}.pyi"} with open(codegen_outdir / f"datamodel_{version}" / f"{rules}.py", "r") as f: assert f.read().strip() == _expected_datamodel_api_output api_tree_file = get_api_tree_file_name(version)