Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion codegen/datamodelgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,15 @@ def _write_static_info(self, name: str, info: Any, f: FileIO, level: int = 0):
for k in singletons:
self._write_static_info(k, info.singletons[k], f, level + 1)
for k in parameters:
f.write(f"{indent} class {k}(PyMenu):\n")
k_type = _PY_TYPE_BY_DM_TYPE[info.parameters[k].type]
if k_type in ["str", "List[str]"]:
f.write(f"{indent} class {k}(PyTextual):\n")
elif k_type in ["int", "float"]:
f.write(f"{indent} class {k}(PyNumerical):\n")
elif k_type in ["Dict", "Dict[str, Any]"]:
f.write(f"{indent} class {k}(PyDictionary):\n")
else:
f.write(f"{indent} class {k}(PyParameter):\n")
f.write(f'{indent} """\n')
f.write(
f"{indent} "
Expand Down Expand Up @@ -273,6 +281,10 @@ def write_static_info(self) -> None:
f.write("# pylint: disable=line-too-long\n\n")
f.write("from ansys.fluent.core.services.datamodel_se import (\n")
f.write(" PyMenu,\n")
f.write(" PyParameter,\n")
f.write(" PyTextual,\n")
f.write(" PyNumerical,\n")
f.write(" PyDictionary,\n")
f.write(" PyNamedObjectContainer,\n")
f.write(" PyCommand\n")
f.write(")\n\n\n")
Expand Down
99 changes: 78 additions & 21 deletions src/ansys/fluent/core/services/datamodel_se.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ def initialize_datamodel(
def get_attribute_value(
self, request: DataModelProtoModule.GetAttributeValueRequest
) -> DataModelProtoModule.GetAttributeValueResponse:
return self.__stub.getAttributeValue(request, metadata=self.__metadata)
ret = self.__stub.getAttributeValue(request, metadata=self.__metadata)
try:
return ret.item
except AttributeError:
return ret

@catch_grpc_error
def get_state(
Expand Down Expand Up @@ -295,6 +299,10 @@ def get_attrib_value(self, attrib: str) -> Any:

getAttribValue = get_attrib_value

def is_active(self):
"""Returns true if the parameter is active."""
return true_if_none(self.get_attrib_value(Attribute.IS_ACTIVE.value))

def help(self) -> None:
"""Print help string."""
request = DataModelProtoModule.GetSpecsRequest()
Expand All @@ -315,31 +323,12 @@ class PyMenu(PyBasicStateContainer):
-------
__setattr__(name, value)
Set state of the child object
update_dict(dict_state)
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
updateDict(dict_state)
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 (same as update_dict(dict_state))
create_command_arguments(command)
"""

def __init__(self, service: DatamodelService, rules: str, path: Path = None):
super().__init__(service, rules, path)

def update_dict(self, dict_state: Dict[str, Any]) -> None:
request = DataModelProtoModule.UpdateDictRequest()
request.rules = self.rules
request.path = _convert_path_to_se_path(self.path)
_convert_value_to_variant(dict_state, request.dicttomerge)
self.service.update_dict(request)

updateDict = update_dict

def __setattr__(self, name: str, value: Any):
"""Set state of the child object.

Expand All @@ -350,7 +339,9 @@ def __setattr__(self, name: str, value: Any):
value : Any
state
"""
if hasattr(self, name) and isinstance(getattr(self, name), PyMenu):
if hasattr(self, name) and isinstance(
getattr(self, name), PyBasicStateContainer
):
getattr(self, name).set_state(value)
else:
super().__setattr__(name, value)
Expand Down Expand Up @@ -383,6 +374,72 @@ def create_command_arguments(self, command):
"""


class PyParameter(PyBasicStateContainer):
"""Object class using StateEngine based DatamodelService as backend.

Use this class instead of directly calling DatamodelService's
method.
"""

def default_value(self):
"""Get default value of the parameter."""
return self.get_attrib_value(Attribute.DEFAULT.value)

def is_read_only(self):
return true_if_none(self.get_attrib_value(Attribute.IS_READ_ONLY.value))


def true_if_none(val):
"""Returns true if 'val' is true or None, else returns false."""
if val in [True, False, None]:
return True if val is None else val
else:
raise RuntimeError(f"In-correct value passed")


class PyTextual(PyParameter):
"""Provides interface for textual parameters."""

def allowed_values(self):
return self.get_attrib_value(Attribute.ALLOWED_VALUES.value)


class PyNumerical(PyParameter):
"""Provides interface for numerical parameters."""

def min(self):
return self.get_attrib_value(Attribute.MIN.value)

def max(self):
return self.get_attrib_value(Attribute.MAX.value)


class PyDictionary(PyParameter):
"""Provides interface for dictionaries.
Methods
-------
update_dict(dict_state)
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
updateDict(dict_state)
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 (same as update_dict(dict_state))
"""

def update_dict(self, dict_state: Dict[str, Any]) -> None:
request = DataModelProtoModule.UpdateDictRequest()
request.rules = self.rules
request.path = _convert_path_to_se_path(self.path)
_convert_value_to_variant(dict_state, request.dicttomerge)
self.service.update_dict(request)

updateDict = update_dict


class PyNamedObjectContainer:
"""Container class using the StateEngine-based DatamodelService as the
backend. Use this class instead of directly calling the DatamodelService's
Expand Down