Skip to content

Commit

Permalink
feat: Stub file for MeshingUtilities (#3334)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* Remove typing import

* error fix

* close stub file

* remove test code

* Use with syntax

* test fix

* update type checking

---------

Co-authored-by: Sean Pearson <[email protected]>
  • Loading branch information
hpohekar and seanpearsonuk authored Oct 17, 2024
1 parent 702436e commit ed11995
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 13 deletions.
61 changes: 51 additions & 10 deletions src/ansys/fluent/core/codegen/datamodelgen.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(
[
Expand All @@ -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",
}

Expand Down Expand Up @@ -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}."

Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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")
Expand All @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions src/ansys/fluent/core/session_meshing.pyi
Original file line number Diff line number Diff line change
@@ -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,
)
Expand All @@ -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: ...
Expand Down
5 changes: 4 additions & 1 deletion src/ansys/fluent/core/session_pure_meshing.pyi
Original file line number Diff line number Diff line change
@@ -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,
)
Expand All @@ -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): ...
Expand Down
4 changes: 2 additions & 2 deletions tests/test_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ed11995

Please sign in to comment.