Skip to content

Commit 2316f7a

Browse files
authored
fix: Use _search() for failed calls and disable printing of API objects during codegen (#2901)
* fix: Disable printing of API objects during codegen * return results * update tests * fix: Use pyfluent.search() for failed calls * update warning message * version fix * remove function * use _search * use _search 1 * set 24.2 version * Revert "set 24.2 version" This reverts commit fa7a761. * Reapply "set 24.2 version" This reverts commit a3d9546. * use list instead of set * test fix search_root * test fix conduction * write_api_tree_data bool * skip 23.2 test * skip 24.1 * update package
1 parent 0e97f28 commit 2316f7a

File tree

10 files changed

+124
-132
lines changed

10 files changed

+124
-132
lines changed

codegen/allapigen.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@
6060
allapigen.generate(version, static_infos)
6161
t2 = time()
6262
print(f"Time to generate APIs: {t2 - t1:.2f} seconds")
63-
_search("", version=version)
63+
_search("", version=version, write_api_tree_data=True)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ include = [
2727
{ path = "src/ansys/fluent/core/generated/solver/settings_*/*.py", format = ["sdist", "wheel"] },
2828
{ path = "src/ansys/fluent/core/generated/solver/settings_*/*.pyi", format = ["sdist", "wheel"] },
2929
{ path = "src/ansys/fluent/core/generated/*.pickle", format = ["sdist", "wheel"] },
30-
{ path = "src/ansys/fluent/core/generated/api_tree/*.txt", format = ["sdist", "wheel"] },
30+
{ path = "src/ansys/fluent/core/generated/api_tree/*.json", format = ["sdist", "wheel"] },
3131
]
3232

3333
packages = [

src/ansys/fluent/core/session_base_meshing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import logging
44

5+
import ansys.fluent.core as pyfluent
56
from ansys.fluent.core.fluent_connection import FluentConnection
67
from ansys.fluent.core.meshing.meshing_workflow import (
78
CreateWorkflow,
@@ -14,7 +15,6 @@
1415
_make_datamodel_module,
1516
_make_tui_module,
1617
)
17-
from ansys.fluent.core.utils import load_module
1818
from ansys.fluent.core.utils.fluent_version import (
1919
FluentVersion,
2020
get_version_for_file_name,
@@ -91,7 +91,7 @@ def _meshing_utilities_root(self):
9191
if self.get_fluent_version() >= FluentVersion.v242:
9292
from ansys.fluent.core import CODEGEN_OUTDIR
9393

94-
meshing_utilities_module = load_module(
94+
meshing_utilities_module = pyfluent.utils.load_module(
9595
f"MeshingUtilities_{self._version}",
9696
CODEGEN_OUTDIR
9797
/ f"datamodel_{self._version}"

src/ansys/fluent/core/session_shared.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import logging
44

5+
import ansys.fluent.core as pyfluent
56
from ansys.fluent.core.services.datamodel_se import PyMenuGeneric
67
from ansys.fluent.core.services.datamodel_tui import TUIMenu
7-
from ansys.fluent.core.utils import load_module
88

99
_CODEGEN_MSG_DATAMODEL = (
1010
"Currently calling the datamodel API in a generic manner. "
@@ -26,7 +26,7 @@ def _make_tui_module(session, module_name):
2626
try:
2727
from ansys.fluent.core import CODEGEN_OUTDIR
2828

29-
tui_module = load_module(
29+
tui_module = pyfluent.utils.load_module(
3030
f"{module_name}_tui_{session._version}",
3131
CODEGEN_OUTDIR / module_name / f"tui_{session._version}.py",
3232
)
@@ -42,7 +42,7 @@ def _make_datamodel_module(session, module_name):
4242
try:
4343
from ansys.fluent.core import CODEGEN_OUTDIR
4444

45-
module = load_module(
45+
module = pyfluent.utils.load_module(
4646
f"{module_name}_{session._version}",
4747
CODEGEN_OUTDIR / f"datamodel_{session._version}" / f"{module_name}.py",
4848
)

src/ansys/fluent/core/solver/error_message.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ def closest_allowed_names(trial_name: str, allowed_names: str) -> List[str]:
1212

1313

1414
def allowed_name_error_message(
15-
allowed_values: Any,
15+
allowed_values: Optional[Any] = None,
1616
context: Optional[str] = None,
1717
trial_name: Optional[str] = None,
1818
message: Optional[str] = None,
19+
search_results: Optional[list] = None,
1920
) -> str:
2021
"""Provide an error message with the closest names matching the 'trial_name' from
2122
the 'allowed_values' list."""
@@ -30,6 +31,10 @@ def allowed_name_error_message(
3031
message += f"The most similar names are: {', '.join(matches)}."
3132
else:
3233
message += f"The allowed values are: {allowed_values}."
34+
elif search_results:
35+
message = message + "\nThe most similar API names are:\n"
36+
for search_result in search_results:
37+
message += search_result + "\n"
3338

3439
return message
3540

src/ansys/fluent/core/solver/flobject.py

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
PyFluentDeprecationWarning = FutureWarning
6363
PyFluentUserWarning = UserWarning
6464

65+
import ansys.fluent.core as pyfluent
66+
6567
from .error_message import allowed_name_error_message, allowed_values_error
6668
from .settings_external import expand_api_file_argument
6769

@@ -1036,28 +1038,6 @@ def get_completer_info(self, prefix="", excluded=None) -> List[List[str]]:
10361038
ret.extend(items)
10371039
return ret
10381040

1039-
def _get_parent_of_active_child_names(self, name):
1040-
with warnings.catch_warnings():
1041-
warnings.filterwarnings(action="ignore", category=UnstableSettingWarning)
1042-
parents = ""
1043-
path_list = []
1044-
for parent in self.get_active_child_names():
1045-
try:
1046-
if hasattr(getattr(self, parent), str(name)):
1047-
path_list.append(f" {self.python_path}.{parent}.{str(name)}")
1048-
if len(parents) != 0:
1049-
parents += ", " + parent
1050-
else:
1051-
parents += parent
1052-
except AttributeError:
1053-
pass
1054-
if len(path_list):
1055-
print(f"\n {str(name)} can be accessed from the following paths: \n")
1056-
for path in path_list:
1057-
print(path)
1058-
if len(parents):
1059-
return f"\n {name} is a child of {parents} \n"
1060-
10611041
def __getattribute__(self, name):
10621042
if name in super().__getattribute__("child_names"):
10631043
if self.is_active() is False:
@@ -1077,11 +1057,18 @@ def __getattribute__(self, name):
10771057
attr._check_stable()
10781058
return attr
10791059
except AttributeError as ex:
1080-
self._get_parent_of_active_child_names(name)
1060+
modified_search_results = []
1061+
for search_result in pyfluent.utils._search(
1062+
word=name, search_root=self, match_case=False, match_whole_word=False
1063+
):
1064+
search_result = search_result.replace(
1065+
"<search_root>", self.__class__.__name__
1066+
)
1067+
modified_search_results.append(search_result)
10811068
error_msg = allowed_name_error_message(
10821069
trial_name=name,
1083-
allowed_values=super().__getattribute__("child_names"),
10841070
message=ex.args[0],
1071+
search_results=modified_search_results,
10851072
)
10861073
ex.args = (error_msg,)
10871074
raise

src/ansys/fluent/core/utils/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
logger = logging.getLogger("pyfluent.general")
88

9+
from .search import _search # noqa: F401
10+
911

1012
def load_module(module_name, file_path):
1113
"""Load a module from a file path."""

src/ansys/fluent/core/utils/search.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ def _search(
149149
match_case: bool = False,
150150
version: Optional[str] = None,
151151
search_root: Optional[Any] = None,
152+
write_api_tree_data: Optional[bool] = False,
152153
):
153154
"""Search for a word through the Fluent's object hierarchy.
154155
@@ -167,6 +168,8 @@ def _search(
167168
The root object within which the search is performed.
168169
It can be a session object or any API object within a session.
169170
The default is ``None``. If ``None``, it searches everything.
171+
write_api_tree_data: bool, optional
172+
Whether to write the API tree data.
170173
171174
Examples
172175
--------
@@ -189,14 +192,13 @@ def _search(
189192
"""
190193
api_objects = []
191194
api_tui_objects = []
192-
api_object_names = set()
195+
api_object_names = []
196+
results = []
193197
if version:
194198
version = get_version_for_file_name(version)
195199
root_version, root_path, prefix = _get_version_path_prefix_from_obj(search_root)
196200
if search_root and not prefix:
197201
return
198-
if not version:
199-
version = root_version
200202
if not version:
201203
for fluent_version in FluentVersion:
202204
version = get_version_for_file_name(fluent_version.value)
@@ -245,13 +247,13 @@ def inner(tree, path, root_path):
245247
else:
246248
next_path = f"{path}.{k}"
247249
type_ = "Object" if isinstance(v, Mapping) else v
248-
api_object_names.add(k)
250+
api_object_names.append(k)
249251
if "tui" in next_path:
250252
api_tui_objects.append(f"{next_path} ({type_})")
251253
else:
252254
api_objects.append(f"{next_path} ({type_})")
253255
if _match(k, word, match_whole_word, match_case):
254-
print(f"{next_path} ({type_})")
256+
results.append(f"{next_path} ({type_})")
255257
if isinstance(v, Mapping):
256258
inner(v, next_path, root_path)
257259

@@ -286,9 +288,11 @@ def _write_api_tree_file(api_tree_data: dict, api_object_names: list):
286288
with open(api_tree_file, "w") as json_file:
287289
json.dump(api_tree_data, json_file)
288290

289-
_write_api_tree_file(
290-
api_tree_data=api_tree_data, api_object_names=list(api_object_names)
291-
)
291+
if write_api_tree_data:
292+
_write_api_tree_file(
293+
api_tree_data=api_tree_data, api_object_names=list(api_object_names)
294+
)
295+
return results
292296

293297

294298
@functools.cache
@@ -643,17 +647,17 @@ def search(
643647
)
644648
elif language and match_whole_word:
645649
warnings.warn(
646-
"``match_whole_word=True`` matches the given word, and it's capitalize case.",
650+
"``match_whole_word=True`` matches the whole word (case insensitive).",
647651
UserWarning,
648652
)
649653
elif match_whole_word:
650654
warnings.warn(
651-
"``match_whole_word=True`` matches the given word, and it's capitalize case.",
655+
"``match_whole_word=True`` matches the whole word (case insensitive).",
652656
UserWarning,
653657
)
654658
elif match_case:
655659
warnings.warn(
656-
"``match_case=True`` matches the given word.",
660+
"``match_case=True`` matches the whole word (case sensitive).",
657661
UserWarning,
658662
)
659663

@@ -678,8 +682,6 @@ def search(
678682
root_version, root_path, prefix = _get_version_path_prefix_from_obj(search_root)
679683
if search_root and not prefix:
680684
return
681-
if not version:
682-
version = root_version
683685
if not version:
684686
for fluent_version in FluentVersion:
685687
version = get_version_for_file_name(fluent_version.value)

0 commit comments

Comments
 (0)