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
6 changes: 6 additions & 0 deletions codegen/settingsgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ def _populate_classes(parent_dir):
f.write(
"from ansys.fluent.core.solver.flobject import _ChildNamedObjectAccessorMixin\n\n"
)
f.write(
"from ansys.fluent.core.solver.flobject import _CreatableNamedObjectMixin\n\n"
)
f.write(
"from ansys.fluent.core.solver.flobject import _NonCreatableNamedObjectMixin\n\n"
)
if children_hash:
for child in children_hash:
pchild_name = hash_dict.get(child)[0].__name__
Expand Down
61 changes: 24 additions & 37 deletions src/ansys/fluent/core/services/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,26 +204,6 @@ def resize_list_object(self, path: str, size: int):
request.size = size
return self.__service_impl.resize_list_object(request)

@_trace
def _extract_info(self, info):
ret = {}
ret["type"] = info.type
if info.children:
ret["children"] = {
k: self._extract_info(v) for k, v in info.children.items()
}
if info.commands:
ret["commands"] = {
k: self._extract_info(v) for k, v in info.commands.items()
}
if info.arguments:
ret["arguments"] = {
k: self._extract_info(v) for k, v in info.arguments.items()
}
if info.HasField("object_type"):
ret["object-type"] = self._extract_info(info.object_type)
return ret

@_trace
def _extract_static_info(self, info):
ret = {}
Expand All @@ -249,30 +229,37 @@ def _extract_static_info(self, info):
ret["object-type"] = self._extract_static_info(info.object_type)
if info.help:
ret["help"] = info.help
return ret

@_trace
def get_obj_static_info(self):
request = SettingsModule.GetObjectStaticInfoRequest()
request.root = "fluent"
response = self.__service_impl.get_obj_static_info(request)
try:
if info.include_child_named_objects:
ret["include_child_named_objects"] = info.include_child_named_objects
except AttributeError:
pass

return self._extract_info(response.info)
try:
if info.list_size:
ret["list_size"] = info.list_size
except AttributeError:
pass

try:
if info.user_creatable:
ret["user_creatable"] = info.user_creatable
except AttributeError:
ret["user_creatable"] = True

return ret

@_trace
def get_static_info(self):
request = SettingsModule.GetStaticInfoRequest()
request.root = "fluent"
# temporary code to fall back to get_obj_static_info()
try:
response = self.__service_impl.get_static_info(request)
# The rpc calls no longer raise an exception. Force an exception if
# type is empty
if not response.info.type:
raise RuntimeError
return self._extract_static_info(response.info)
except Exception:
return self.get_obj_static_info()
response = self.__service_impl.get_static_info(request)
# The rpc calls no longer raise an exception. Force an exception if
# type is empty
if not response.info.type:
raise RuntimeError
return self._extract_static_info(response.info)

@_trace
def execute_cmd(self, path: str, command: str, **kwds) -> Any:
Expand Down
80 changes: 48 additions & 32 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,21 +505,6 @@ def items(self):
self._update_objects()
return self._objects.items()

def create(self, name: str):
"""Create a named object with given name.

Parameters
----------
name: str
Name of new object

Returns
-------
The object that has been created
"""
self.flproxy.create(self.path, name)
return self._create_child_object(name)

def get_object_names(self):
"""Object names."""
return self.flproxy.get_object_names(self.path)
Expand All @@ -532,14 +517,6 @@ def __getitem__(self, name: str) -> ChildTypeT:
obj = self._create_child_object(name)
return obj

def __setitem__(self, name: str, value):
if name not in self.get_object_names():
self.flproxy.create(self.path, name)
child = self._objects.get(name)
if not child:
child = self._create_child_object(name)
child.set_state(value)


class ListObject(SettingsBase[ListStateType], Generic[ChildTypeT]):
"""A ListObject container.
Expand Down Expand Up @@ -759,6 +736,43 @@ def __len__(self):
return l


class _CreatableNamedObjectMixin(collections.abc.MutableMapping, Generic[ChildTypeT]):
def create(self, name: str) -> ChildTypeT:
"""Create a named object with given name.

Parameters
----------
name: str
Name of new object

Returns
-------
The object that has been created
"""
self.flproxy.create(self.path, name)
return self._create_child_object(name)

def __setitem__(self, name: str, value):
if name not in self.get_object_names():
self.flproxy.create(self.path, name)
child = self._objects.get(name)
if not child:
child = self._create_child_object(name)
child.set_state(value)


class _NonCreatableNamedObjectMixin(
collections.abc.MutableMapping, Generic[ChildTypeT]
):
def __setitem__(self, name: str, value):
if name not in self.get_object_names():
raise KeyError(name)
child = self._objects.get(name)
if not child:
child = self._create_child_object(name)
child.set_state(value)


def get_cls(name, info, parent=None):
"""Create a class for the object identified by "path"."""
try:
Expand Down Expand Up @@ -790,16 +804,18 @@ def get_cls(name, info, parent=None):
else:
dct["__doc__"] = f"'{pname.strip('_')}' child."

include_child_named_objects = obj_type == "group" and pname in [
"boundary_conditions",
"cell_zone_conditions",
"report_definitions",
]
# include_child_name_objects = info.get("include_child_named_objects", False)
include_child_named_objects = info.get("include_child_named_objects", False)
user_creatable = info.get("user_creatable", False)

bases = (base,)
if include_child_named_objects:
cls = type(pname, (base, _ChildNamedObjectAccessorMixin), dct)
else:
cls = type(pname, (base,), dct)
bases = bases + (_ChildNamedObjectAccessorMixin,)
if obj_type == "named-object" and user_creatable:
bases = bases + (_CreatableNamedObjectMixin,)
elif obj_type == "named-object":
bases = bases + (_NonCreatableNamedObjectMixin,)

cls = type(pname, bases, dct)

taboo = set(dir(cls))
taboo |= set(
Expand Down
5 changes: 5 additions & 0 deletions tests/test_flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ def get_static_info(cls):
ret["object-type"] = cls.child_object_type.get_static_info()
if cls.commands:
ret["commands"] = {c: v.get_static_info() for c, v in cls.commands.items()}
try:
if cls.user_creatable:
ret["user_creatable"] = cls.user_creatable
except AttributeError:
ret["user_creatable"] = True
return ret


Expand Down