Skip to content

Commit

Permalink
feat: refact
Browse files Browse the repository at this point in the history
  • Loading branch information
mkundu1 committed Nov 26, 2024
1 parent 9b0b5de commit fff8325
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 23 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ addopts = """
-v
--durations=0
--show-capture=all
-n 4
"""
markers = [
"settings_only: Read and modify the case settings only, without loading the mesh, initializing, or solving the case",
Expand Down
55 changes: 37 additions & 18 deletions src/ansys/fluent/core/solver/flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,31 +172,36 @@ def to_python_name(fluent_name: str) -> str:
return name


def _get_python_path_comps(python_path: str):
def _get_python_path_comps(obj):
"""Get python path components for traversing class hierarchy."""
python_path = python_path.removeprefix("<session>")
python_path = python_path.removeprefix(".settings")
python_path = python_path.removeprefix(".")
comps = python_path.split(".")
comps = [comp.split("[")[0] for comp in comps]
return comps
comps = []
while obj:
python_name = obj._python_name
obj = obj._parent
if isinstance(obj, (NamedObject, ListObject)):
comps.append(obj._python_name)
obj = obj._parent
else:
comps.append(python_name)
comps.reverse()
return comps[1:]


def _get_alias_class(root_cls, path: list[str], alias_path: list[str]):
def _get_class_from_paths(root_cls, some_path: list[str], other_path: list[str]):
"""Get the class for the given alias path."""
parent_count = 0
while alias_path[0] == "..":
while other_path[0] == "..":
parent_count += 1
alias_path.pop(0)
other_path.pop(0)
for _ in range(parent_count):
path.pop()
path = path + alias_path
some_path.pop()
full_path = some_path + other_path
cls = root_cls
for comp in path:
for comp in full_path:
cls = cls._child_classes[comp]
if issubclass(cls, (NamedObject, ListObject)):
cls = cls.child_object_type
return cls, path
return cls, full_path


class Base:
Expand Down Expand Up @@ -467,6 +472,20 @@ def _while_executing_command(self):
def __eq__(self, other):
return self.flproxy == other.flproxy and self.path == other.path

def _get_path_comps(self, path):
"""Get path components."""
if self._parent is None:
if FluentVersion(self.version).number < 251:
return "<session>"
else:
return "<session>.settings"
ppath = self._parent.python_path
if not ppath:
return self.python_name
if self.python_name[0] == "[":
return ppath + self.python_name
return ppath + "." + self.python_name


StateT = TypeVar("StateT")

Expand Down Expand Up @@ -724,7 +743,7 @@ def set_state(self, state: StateT | None = None, **kwargs):
self.to_scheme_keys(
kwargs or state,
self._root.__class__,
_get_python_path_comps(self.python_path),
_get_python_path_comps(self),
),
)

Expand Down Expand Up @@ -968,7 +987,7 @@ def to_scheme_keys(cls, value, root_cls, path: list[str]):
ret[ccls.fluent_name] = ccls.to_scheme_keys(v, root_cls, path + [k])
elif k in cls._child_aliases:
alias, scm_alias_name = cls._child_aliases[k]
alias_cls, alias_path = _get_alias_class(
alias_cls, alias_path = _get_class_from_paths(
root_cls, path.copy(), alias.split("/")
)
ret[scm_alias_name] = alias_cls.to_scheme_keys(
Expand Down Expand Up @@ -1658,7 +1677,7 @@ def execute_command(self, *args, **kwds):
scmKwds[argument.fluent_name] = argument.to_scheme_keys(
value,
argument._root.__class__,
_get_python_path_comps(argument.python_path),
_get_python_path_comps(argument),
)
ret = self._execute_command(*args, **scmKwds)
for arg, value in kwds.items():
Expand Down Expand Up @@ -1748,7 +1767,7 @@ def __call__(self, **kwds):
scmKwds[argument.fluent_name] = argument.to_scheme_keys(
value,
argument._root.__class__,
_get_python_path_comps(argument.python_path),
_get_python_path_comps(argument),
)
return self.flproxy.execute_query(self._parent.path, self.obj_name, **scmKwds)

Expand Down
5 changes: 0 additions & 5 deletions tests/test_flobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,13 @@ def cb(self, a1, a2):
}


class _SchemeEval:
version = "25.1.0"


class Proxy:
"""Proxy class."""

root = Root

def __init__(self):
self.r = self.root(None)
self._scheme_eval = _SchemeEval()

def get_obj(self, path):
if not path:
Expand Down

0 comments on commit fff8325

Please sign in to comment.