Skip to content

Commit

Permalink
fix: Update arguments behaviour. (#3332)
Browse files Browse the repository at this point in the history
* fix: Update arguments behaviour.

* Minor fix.

* Update behaviour.

* Fix 'update_dict' w.r.t. server.
  • Loading branch information
prmukherj committed Oct 9, 2024
1 parent d4748f4 commit f7d4e9c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
13 changes: 10 additions & 3 deletions src/ansys/fluent/core/services/datamodel_se.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,11 @@ def fix_state(self, rules, path) -> None:
self._impl.fix_state(request)

def update_dict(
self, rules: str, path: str, dict_state: dict[str, _TValue]
self,
rules: str,
path: str,
dict_state: dict[str, _TValue],
recursive=False,
) -> None:
"""Update the dict."""
request = DataModelProtoModule.UpdateDictRequest(
Expand Down Expand Up @@ -1333,7 +1337,7 @@ class PyDictionary(PyParameter):
to dict.update semantics (same as update_dict(dict_state))]
"""

def update_dict(self, dict_state: dict[str, Any]) -> None:
def update_dict(self, dict_state: dict[str, Any], recursive=False) -> None:
"""Update the state of the current object if the current object is a Dict in the
data model, else throws RuntimeError (currently not showing up in Python).
Update is executed according to dict.update semantics.
Expand All @@ -1343,6 +1347,9 @@ def update_dict(self, dict_state: dict[str, Any]) -> None:
dict_state : dict[str, Any]
Incoming dict state
recursive: bool
Flag to update the nested dictionary structure.
Raises
------
ReadOnlyObjectError
Expand All @@ -1351,7 +1358,7 @@ def update_dict(self, dict_state: dict[str, Any]) -> None:
if self.get_attr(Attribute.IS_READ_ONLY.value):
raise ReadOnlyObjectError(type(self).__name__)
self.service.update_dict(
self.rules, convert_path_to_se_path(self.path), dict_state
self.rules, convert_path_to_se_path(self.path), dict_state, recursive
)

updateDict = update_dict
Expand Down
32 changes: 25 additions & 7 deletions src/ansys/fluent/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ def __setattr__(self, attr, value):
logger.debug(f"BaseTask.__setattr__({attr}, {value})")
if attr in self.__dict__:
self.__dict__[attr] = value
elif attr in self.arguments():
elif attr in self.arguments() or attr == "arguments":
getattr(self, attr).set_state(value)
else:
setattr(self._task, attr, value)
Expand Down Expand Up @@ -759,9 +759,31 @@ def _assign(self, args: dict, fn) -> None:
# TODO: Figure out proper way to implement "add_child".
if "add_child" in args:
self._snake_to_camel_map["add_child"] = "AddChild"

cmd_args = self._task._command_arguments
for key, val in args.items():
camel_args[self._snake_to_camel_map[key] if key.islower() else key] = val
getattr(self._task.Arguments, fn)(camel_args)
camel_arg = self._snake_to_camel_map[key] if key.islower() else key
# TODO: Implement enhanced meshing workflow to hide away internal info.
if isinstance(
getattr(cmd_args, camel_arg), PySingletonCommandArgumentsSubItem
):
updated_dict = {}
for attr, attr_val in val.items():
camel_attr = snake_to_camel_case(
str(attr),
getattr(
self, camel_to_snake_case(key)
)._get_camel_case_arg_keys()
or [],
)
updated_dict[camel_attr] = attr_val
camel_args[camel_arg] = updated_dict
else:
camel_args[camel_arg] = val
if fn == "update_dict":
self._task.Arguments.update_dict(camel_args, recursive=True)
else:
getattr(self._task.Arguments, fn)(camel_args)
try:
self._refresh_command_after_changing_args(old_state)
except Exception as ex:
Expand Down Expand Up @@ -891,10 +913,6 @@ def __setattr__(self, attr, value):
if attr in self.__dict__:
self.__dict__[attr] = value
else:
camel_attr = snake_to_camel_case(
str(attr), self._get_camel_case_arg_keys() or []
)
attr = camel_attr or attr
self.set_state({attr: value})

def __dir__(self):
Expand Down
18 changes: 13 additions & 5 deletions tests/test_new_meshing_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,18 +597,26 @@ def test_updating_state_in_new_meshing_workflow(new_meshing_session):
"mixing_elbow.pmdb", "pyfluent/mixing_elbow"
)
watertight = new_meshing_session.watertight()
watertight.import_geometry.file_name.set_state(import_file_name)
assert watertight.import_geometry.length_unit() == "mm"
watertight.import_geometry.length_unit = "in"
assert watertight.import_geometry.length_unit.get_state() == "in"
assert watertight.import_geometry.cad_import_options.feature_angle() == 40.0
watertight.import_geometry.cad_import_options.feature_angle.set_state(25.0)
assert watertight.import_geometry.cad_import_options.feature_angle() == 25.0
assert (
watertight.import_geometry.cad_import_options.one_zone_per.allowed_values()
== ["body", "face", "object"]
)
assert watertight.import_geometry.cad_import_options.one_zone_per() == "body"
watertight.import_geometry.arguments = {
"file_name": import_file_name,
"length_unit": "in",
"cad_import_options": {"feature_angle": 35, "one_zone_per": "object"},
}
assert watertight.import_geometry.cad_import_options.feature_angle() == 35.0
assert (
watertight.import_geometry.cad_import_options.one_zone_per.get_state()
== "object"
)
assert watertight.import_geometry.length_unit.get_state() == "in"
watertight.import_geometry.cad_import_options.feature_angle = 25.0
assert watertight.import_geometry.cad_import_options.feature_angle() == 25.0
watertight.import_geometry.cad_import_options.one_zone_per = "face"
assert watertight.import_geometry.cad_import_options.one_zone_per() == "face"
watertight.import_geometry()
Expand Down

0 comments on commit f7d4e9c

Please sign in to comment.