-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multiple changes to Flow and Task helpers
Added Flow__Events main class Refactored Flow related model objects into a models folder
- Loading branch information
Showing
9 changed files
with
109 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from osbot_utils.base_classes.Type_Safe import Type_Safe | ||
from osbot_utils.helpers.flows.models.Flow__Event import Flow__Event | ||
from osbot_utils.helpers.flows.models.Flow__Event_Type import Flow__Event_Type | ||
|
||
|
||
class Flow_Events(Type_Safe): | ||
event_listeners : list | ||
|
||
def on__flow__start(self, flow): | ||
flow_event = Flow__Event(event_type=Flow__Event_Type.FLOW_START, event_source=flow) | ||
self.raise_event(flow_event) | ||
|
||
def on__flow__stop(self, flow): # todo: see of flow_ended or flow_completed are better names | ||
flow_event = Flow__Event(event_type=Flow__Event_Type.FLOW_STOP , event_source=flow) | ||
self.raise_event(flow_event) | ||
|
||
def on__task__start(self, task): | ||
flow_event = Flow__Event(event_type=Flow__Event_Type.TASK_START, event_source=task) | ||
self.raise_event(flow_event) | ||
|
||
def on__task__stop(self, task): # todo: see of flow_ended or flow_completed are better names | ||
flow_event = Flow__Event(event_type=Flow__Event_Type.TASK_STOP , event_source=task) | ||
self.raise_event(flow_event) | ||
|
||
def raise_event(self, flow_event): | ||
for listener in self.event_listeners: | ||
try: | ||
listener(flow_event) | ||
except Exception as error: | ||
print(f"Error in listener: {error}") | ||
|
||
flow_events = Flow_Events() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from osbot_utils.base_classes.Type_Safe import Type_Safe | ||
|
||
class Flow__Event(Type_Safe): | ||
event_type : str | ||
event_source: object | ||
event_data : dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from osbot_utils.base_classes.Type_Safe import Type_Safe | ||
|
||
|
||
class Flow__Event_Type(Type_Safe): | ||
FLOW_START : str = 'flow_start' | ||
FLOW_STOP : str = 'flow_stop' | ||
TASK_START : str = 'task_start' | ||
TASK_STOP : str = 'task_stop' |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from unittest import TestCase | ||
from osbot_utils.helpers.flows.Flow import Flow | ||
from osbot_utils.helpers.flows.decorators.flow import flow | ||
from osbot_utils.helpers.flows.Flow__Events import Flow__Event_Type, Flow_Events, flow_events | ||
|
||
|
||
class test_Flow__Events(TestCase): | ||
|
||
def setUp(self): | ||
self.flow_events = Flow_Events() | ||
|
||
def test_on__flow__start(self): | ||
def event_listener(flow_event): | ||
assert flow_event.event_type == Flow__Event_Type.FLOW_START | ||
assert flow_event.event_source == 'flow' | ||
assert flow_event.event_data == {} | ||
self.flow_events.event_listeners.append(event_listener) | ||
self.flow_events.on__flow__start('flow') | ||
|
||
def test_global_flow_events(self): | ||
def event_listener(flow_event): | ||
assert flow_event.event_type == Flow__Event_Type.FLOW_START | ||
assert flow_event.event_type == Flow__Event_Type.FLOW_START | ||
assert type(flow_event.event_source) is Flow | ||
|
||
@flow() | ||
def an_flow() -> Flow: | ||
print('inside the flow') | ||
|
||
flow_events.event_listeners.append(event_listener) | ||
assert event_listener in flow_events.event_listeners | ||
|
||
an_flow().execute_flow() | ||
|
||
flow_events.event_listeners.remove(event_listener) | ||
assert event_listener not in flow_events.event_listeners | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters