Skip to content

Commit

Permalink
feat: datamodel cbs for other events
Browse files Browse the repository at this point in the history
  • Loading branch information
mkundu1 committed Jan 9, 2025
1 parent d797892 commit c7409e5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 44 deletions.
86 changes: 57 additions & 29 deletions src/ansys/fluent/core/services/datamodel_se.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def cb_grpc(child_type: str, child_name: str):
return subscription

def add_on_deleted(
self, rules: str, path: str, obj, cb: Callable
self, rules: str, path: str, obj, cb: Callable[[], None]
) -> EventSubscription:
"""Add on deleted."""
request_dict = {
Expand All @@ -765,7 +765,7 @@ def add_on_deleted(
return subscription

def add_on_changed(
self, rules: str, path: str, obj, cb: Callable
self, rules: str, path: str, obj, cb: Callable[[Any], None]
) -> EventSubscription:
"""Add on changed."""
request_dict = {
Expand All @@ -781,7 +781,7 @@ def add_on_changed(
return subscription

def add_on_affected(
self, rules: str, path: str, obj, cb: Callable
self, rules: str, path: str, obj, cb: Callable[[], None]
) -> EventSubscription:
"""Add on affected."""
request_dict = {
Expand All @@ -797,7 +797,7 @@ def add_on_affected(
return subscription

def add_on_affected_at_type_path(
self, rules: str, path: str, child_type: str, obj, cb: Callable
self, rules: str, path: str, child_type: str, obj, cb: Callable[[], None]
) -> EventSubscription:
"""Add on affected at type path."""
request_dict = {
Expand All @@ -816,7 +816,7 @@ def add_on_affected_at_type_path(
return subscription

def add_on_command_executed_old(
self, rules: str, path: str, command: str, obj, cb: Callable
self, rules: str, path: str, command: str, obj, cb: Callable[[str, Any], None]
) -> EventSubscription:
"""Add on command executed."""
request_dict = {
Expand All @@ -835,7 +835,7 @@ def add_on_command_executed_old(
return subscription

def add_on_command_executed(
self, rules: str, path: str, obj, cb: Callable
self, rules: str, path: str, obj, cb: Callable[[str, Any], None]
) -> EventSubscription:
"""Add on command executed."""
request_dict = {
Expand All @@ -853,7 +853,7 @@ def add_on_command_executed(
return subscription

def add_on_attribute_changed(
self, rules: str, path: str, attribute: str, obj, cb: Callable
self, rules: str, path: str, attribute: str, obj, cb: Callable[[Any], None]
) -> EventSubscription:
"""Add on attribute changed."""
request_dict = {
Expand All @@ -872,7 +872,13 @@ def add_on_attribute_changed(
return subscription

def add_on_command_attribute_changed(
self, rules: str, path: str, command: str, attribute: str, obj, cb: Callable
self,
rules: str,
path: str,
command: str,
attribute: str,
obj,
cb: Callable[[Any], None],
) -> EventSubscription:
"""Add on command attribute changed."""
request_dict = {
Expand Down Expand Up @@ -1098,15 +1104,15 @@ def __call__(self, *args, **kwargs) -> Any:
return self.get_state()

def add_on_attribute_changed(
self, attribute: str, cb: Callable
self, attribute: str, cb: Callable[[Any], None]
) -> EventSubscription:
"""Register a callback for when an attribute is changed.
Parameters
----------
attribute : str
attribute name
cb : Callable
cb : Callable[[Any], None]
Callback function
Returns
Expand All @@ -1119,7 +1125,7 @@ def add_on_attribute_changed(
)

def add_on_command_attribute_changed(
self, command: str, attribute: str, cb: Callable
self, command: str, attribute: str, cb: Callable[[Any], None]
) -> EventSubscription:
"""Register a callback for when an attribute is changed.
Expand All @@ -1129,7 +1135,7 @@ def add_on_command_attribute_changed(
command name
attribute : str
attribute name
cb : Callable
cb : Callable[[Any], None]
Callback function
Returns
Expand Down Expand Up @@ -1313,12 +1319,12 @@ def cb_service(child_path: str):
self.rules, convert_path_to_se_path(self.path), child_type, self, cb_service
)

def add_on_deleted(self, cb: Callable) -> EventSubscription:
def add_on_deleted(self, cb: Callable[[], None]) -> EventSubscription:
"""Register a callback for when the object is deleted.
Parameters
----------
cb : Callable
cb : Callable[[], None]
Callback function
Returns
Expand All @@ -1330,97 +1336,119 @@ def add_on_deleted(self, cb: Callable) -> EventSubscription:
self.rules, convert_path_to_se_path(self.path), self, cb
)

def add_on_changed(self, cb: Callable) -> EventSubscription:
def add_on_changed(self, cb: Callable[[PyMenuT], None]) -> EventSubscription:
"""Register a callback for when the object is modified.
Parameters
----------
cb : Callable
cb : Callable[[PyMenuT], None]
Callback function
Returns
-------
EventSubscription
EventSubscription instance which can be used to unregister the callback
"""

def cb_service(value: Any):
cb(self)

return self.service.add_on_changed(
self.rules, convert_path_to_se_path(self.path), self, cb
self.rules, convert_path_to_se_path(self.path), self, cb_service
)

def add_on_affected(self, cb: Callable) -> EventSubscription:
def add_on_affected(self, cb: Callable[[PyMenuT], None]) -> EventSubscription:
"""Register a callback for when the object is affected.
Parameters
----------
cb : Callable
cb : Callable[[PyMenuT], None]
Callback function
Returns
-------
EventSubscription
EventSubscription instance which can be used to unregister the callback
"""

def cb_service():
cb(self)

return self.service.add_on_affected(
self.rules, convert_path_to_se_path(self.path), self, cb
self.rules, convert_path_to_se_path(self.path), self, cb_service
)

def add_on_affected_at_type_path(
self, child_type: str, cb: Callable
self, child_type: str, cb: Callable[[PyMenuT], None]
) -> EventSubscription:
"""Register a callback for when the object is affected at child type.
Parameters
----------
child_type : str
child type
cb : Callable
cb : Callable[[PyMenuT], None]
Callback function
Returns
-------
EventSubscription
EventSubscription instance which can be used to unregister the callback
"""

def cb_service():
cb(self)

return self.service.add_on_affected_at_type_path(
self.rules, convert_path_to_se_path(self.path), child_type, self, cb
self.rules, convert_path_to_se_path(self.path), child_type, self, cb_service
)

def add_on_command_executed_old(
self, command: str, cb: Callable
self, command: str, cb: Callable[[PyMenuT, str, Any], None]
) -> EventSubscription:
"""Register a callback for when a command is executed.
Parameters
----------
command : str
Command name
cb : Callable
cb : Callable[[PyMenuT, str, Any], None]
Callback function
Returns
-------
EventSubscription
EventSubscription instance which can be used to unregister the callback
"""

def cb_service(command: str, args: Any):
cb(self, command, args)

return self.service.add_on_command_executed_old(
self.rules, convert_path_to_se_path(self.path), command, self, cb
self.rules, convert_path_to_se_path(self.path), command, self, cb_service
)

def add_on_command_executed(self, cb: Callable) -> EventSubscription:
def add_on_command_executed(
self, cb: Callable[[PyMenuT, str, Any], None]
) -> EventSubscription:
"""Register a callback for when a command is executed.
Parameters
----------
cb : Callable
cb : Callable[[PyMenuT, str, Any], None]
Callback function
Returns
-------
EventSubscription
EventSubscription instance which can be used to unregister the callback
"""

def cb_service(command: str, args: Any):
cb(self, command, args)

return self.service.add_on_command_executed(
self.rules, convert_path_to_se_path(self.path), self, cb
self.rules, convert_path_to_se_path(self.path), self, cb_service
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,18 @@ def _process_streaming(self, id, stream_begin_method, started_evt, *args, **kwar
elif response.HasField("commandAttributeChangedEventResponse"):
value = response.commandAttributeChangedEventResponse.value
cb[1](_convert_variant_to_value(value))
elif response.HasField(
"modifiedEventResponse"
) or response.HasField("affectedEventResponse"):
cb[1](cb[0])
elif response.HasField("modifiedEventResponse"):
value = response.modifiedEventResponse.value
cb[1](_convert_variant_to_value(value))
elif response.HasField("affectedEventResponse"):
cb[1]()
elif response.HasField("deletedEventResponse"):
cb[1]()
elif response.HasField("commandExecutedEventResponse"):
command = response.commandExecutedEventResponse.command
args = _convert_variant_to_value(
response.commandExecutedEventResponse.args
)
cb[1](cb[0], command, args)
cb[1](command, args)
except StopIteration:
break
14 changes: 7 additions & 7 deletions tests/test_datamodel_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ def test_datamodel_api_on_changed(datamodel_api_version_all, new_solver_session)
called_obj = 0
state_obj = None

def cb(obj):
def cb(value):
nonlocal called
nonlocal state
state = obj()
state = value
called += 1

def cb_obj(obj):
def cb_obj(value):
nonlocal called_obj
nonlocal state_obj
state_obj = obj()
state_obj = value
called_obj += 1

subscription = service.add_on_changed(app_name, "/A/X", root.A.X, cb)
Expand Down Expand Up @@ -145,7 +145,7 @@ def test_datamodel_api_on_affected(datamodel_api_version_all, new_solver_session
root = create_root_using_datamodelgen(service, app_name)
called = 0

def cb(obj):
def cb():
nonlocal called
called += 1

Expand Down Expand Up @@ -177,7 +177,7 @@ def test_datamodel_api_on_affected_at_type_path(
root = create_root_using_datamodelgen(service, app_name)
called = 0

def cb(obj):
def cb():
nonlocal called
called += 1

Expand Down Expand Up @@ -327,7 +327,7 @@ def test_datamodel_api_on_command_executed(
command = None
arguments = None

def cb(obj, cmd, args):
def cb(cmd, args):
nonlocal executed
nonlocal command
nonlocal arguments
Expand Down
6 changes: 3 additions & 3 deletions tests/test_mapped_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def test_datamodel_api_on_command_executed_mapped_args(
command = None
arguments = None

def cb(obj, cmd, args):
def cb(cmd, args):
nonlocal executed
nonlocal command
nonlocal arguments
Expand Down Expand Up @@ -728,7 +728,7 @@ def delete_cb():
delete_count += 1

def changed_cb(value):
changes.append(value())
changes.append(value)

service.add_on_child_created(app_name, "/", "eee", root, create_cb)
service.set_state(app_name, "/", {"eee:b": {}})
Expand Down Expand Up @@ -757,7 +757,7 @@ def test_datamodel_api_on_changed_with_mapped_names(
changes = []

def changed_cb(value):
changes.append(value())
changes.append(value)

service.set_state(app_name, "/", {"eee:b": {}})
service.add_on_changed(app_name, "/eee:b/yyy", root.eee["b"].yyy, changed_cb)
Expand Down

0 comments on commit c7409e5

Please sign in to comment.