Skip to content

Commit

Permalink
Merge branch 'main' into feat/fluent_licensing_exception
Browse files Browse the repository at this point in the history
  • Loading branch information
hpohekar authored Sep 26, 2024
2 parents c9dbf6c + fe5937d commit 50d87a7
Show file tree
Hide file tree
Showing 6 changed files with 577 additions and 355 deletions.
5 changes: 5 additions & 0 deletions src/ansys/fluent/core/services/datamodel_tui.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,14 @@ def __dir__(self) -> list[str]:
for x in PyMenu(
self._service, self._version, self._mode, self._path
).get_child_names()
if x != "exit"
]

def __getattribute__(self, name) -> Any:
if name == "exit" and not self._path:
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute 'exit'"
)
try:
attr = super().__getattribute__(name)
if isinstance(attr, TUIMethod):
Expand Down
3 changes: 3 additions & 0 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,9 @@ def _while_executing_command(self):
"""Avoid additional processing while executing a command."""
return nullcontext()

def __eq__(self, other):
return self.flproxy == other.flproxy and self.path == other.path


StateT = TypeVar("StateT")

Expand Down
137 changes: 102 additions & 35 deletions src/ansys/fluent/core/solver/settings_builtin_bases.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,113 @@
"""Base classes for builtin setting classes."""

from typing import Optional
from typing import Protocol, runtime_checkable

from ansys.fluent.core.session_solver import Solver
from ansys.fluent.core.solver.flobject import SettingsBase
from ansys.fluent.core.solver.settings_builtin_data import DATA
from ansys.fluent.core.utils.fluent_version import FluentVersion


@runtime_checkable
class Solver(Protocol):
"""Solver session class for type hinting."""

settings: SettingsBase


def _get_settings_root(settings_source: SettingsBase | Solver):
def is_root_obj(obj):
return isinstance(obj, SettingsBase) and obj.parent is None

if is_root_obj(settings_source):
return settings_source
elif isinstance(settings_source, Solver) and is_root_obj(settings_source.settings):
return settings_source.settings
else:
raise TypeError(
f"{settings_source} is not a Settings root or a Solver session object."
)


def _get_settings_obj(settings_source: SettingsBase | Solver, cls_name: str):
obj = _get_settings_root(settings_source)
path = DATA[cls_name][1]
if isinstance(path, dict):
version = FluentVersion(obj.version)
path = path.get(version)
if path is None:
raise RuntimeError(
f"{cls_name} is not supported in Fluent version {version}."
)
for comp in path.split("."):
obj = SettingsBase.__getattribute__(obj, comp) # bypass InactiveObjectError
return obj


class _SingletonSetting:
def __new__(cls, solver: Solver):
obj = solver.settings
path = DATA[cls.__name__][1]
if isinstance(path, dict):
version = solver.get_fluent_version()
path = path.get(version)
if path is None:
raise RuntimeError(
f"{cls.__name__} is not supported in Fluent version {version}."
)
for comp in path.split("."):
obj = getattr(obj, comp)
return obj


class _CreatableNamedObjectSetting(_SingletonSetting):
def __new__(
cls,
solver: Solver,
name: Optional[str] = None,
new_instance_name: Optional[str] = None,
def __init__(self, settings_source: SettingsBase | Solver | None = None):
self.__dict__.update(dict(settings_source=None))
if settings_source is not None:
self.settings_source = settings_source

def __setattr__(self, name, value):
if name == "settings_source":
obj = _get_settings_obj(value, self.__class__.__name__)
self.__class__ = obj.__class__
self.__dict__.clear()
self.__dict__.update(
obj.__dict__ | dict(settings_source=_get_settings_root(value))
)
else:
super().__setattr__(name, value)


class _NonCreatableNamedObjectSetting:
def __init__(self, name: str, settings_source: SettingsBase | Solver | None = None):
self.__dict__.update(dict(settings_source=None, name=name))
if settings_source is not None:
self.settings_source = settings_source

def __setattr__(self, name, value):
if name == "settings_source":
obj = _get_settings_obj(value, self.__class__.__name__)
obj = obj[self.name]
self.__class__ = obj.__class__
self.__dict__.clear()
self.__dict__.update(
obj.__dict__ | dict(settings_source=_get_settings_root(value))
)
else:
super().__setattr__(name, value)


class _CreatableNamedObjectSetting:
def __init__(
self,
settings_source: SettingsBase | Solver | None = None,
name: str | None = None,
new_instance_name: str | None = None,
):
if name and new_instance_name:
raise ValueError("Cannot specify both name and new_instance_name.")
obj = super().__new__(cls, solver)
if name:
return obj[name]
elif new_instance_name:
return obj.create(new_instance_name)
else:
return obj.create()
self.__dict__.update(
dict(settings_source=None, name=name, new_instance_name=new_instance_name)
)
if settings_source is not None:
self.settings_source = settings_source


class _NonCreatableNamedObjectSetting(_SingletonSetting):
def __new__(cls, solver: Solver, name: str):
obj = super().__new__(cls, solver)
return obj[name]
def __setattr__(self, name, value):
if name == "settings_source":
obj = _get_settings_obj(value, self.__class__.__name__)
if self.name:
obj = obj[self.name]
elif self.new_instance_name:
obj = obj.create(self.new_instance_name)
else:
obj = obj.create()
self.__class__ = obj.__class__
self.__dict__.clear()
self.__dict__.update(
obj.__dict__ | dict(settings_source=_get_settings_root(value))
)
else:
super().__setattr__(name, value)
Loading

0 comments on commit 50d87a7

Please sign in to comment.