Skip to content

Commit

Permalink
Merge dev into main
Browse files Browse the repository at this point in the history
  • Loading branch information
DinisCruz committed Oct 9, 2024
2 parents 60260a5 + 60705c8 commit 14e2103
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Powerful Python util methods and classes that simplify common apis and tasks.

![Current Release](https://img.shields.io/badge/release-v1.55.0-blue)
![Current Release](https://img.shields.io/badge/release-v1.55.1-blue)
[![codecov](https://codecov.io/gh/owasp-sbot/OSBot-Utils/graph/badge.svg?token=GNVW0COX1N)](https://codecov.io/gh/owasp-sbot/OSBot-Utils)


Expand Down
32 changes: 22 additions & 10 deletions osbot_utils/helpers/flows/Flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def execute_flow(self):
if self.flow_config.log_to_memory:
self.logger.add_memory_logger() # todo: move to method that does pre-execute tasks

self.log_debug(f"Executing flow run '{self.f__flow_id()}''")
self.log_debug(f"Executing flow run '{self.f__flow_id()}'")
try:
with Stdout() as stdout:
self.invoke_flow_target()
Expand Down Expand Up @@ -102,17 +102,29 @@ def log_captured_stdout(self, stdout):
print()
self.print_log_messages()

def log_debug(self, message):
if self.flow_config.logging_enabled:
self.logger.debug(message)

def log_error(self, message):
if self.flow_config.logging_enabled:
self.logger.error(message)
def log_debug(self, message, task_run_id=None):
self.logger_add_message(log_level=logging.DEBUG, message=message, task_run_id=task_run_id)

def log_error(self, message, task_run_id=None):
self.logger_add_message(log_level=logging.ERROR, message=message, task_run_id=task_run_id)

def log_info(self, message):
def log_info(self, message, task_run_id=None):
self.logger_add_message(log_level=logging.INFO, message=message, task_run_id=task_run_id)

def logger_add_message(self, log_level, message, task_run_id=None):
if self.flow_config.logging_enabled:
self.logger.info(message)
kwargs = dict(log_level = log_level ,
message = message ,
flow_run_id = self.flow_id,
task_run_id = task_run_id )
flow_events.on__flow_run__message(self, **kwargs)
if log_level == logging.DEBUG:
self.logger.debug(message)
elif log_level == logging.ERROR:
self.logger.error(message)
else:
self.logger.info(message)

def log_messages(self):
return ansis_to_texts(self.log_messages_with_colors())
Expand All @@ -135,7 +147,7 @@ def print_log_messages(self, use_colors=True):

def print_flow_finished_message(self):
if self.flow_config.print_finished_message:
self.log_debug(f"Finished flow run '{self.f__flow_id()}''")
self.log_debug(f"Finished flow run '{self.f__flow_id()}'")

def print_flow_return_value(self):
if self.flow_config.print_none_return_value is False and self.flow_return_value is None:
Expand Down
13 changes: 13 additions & 0 deletions osbot_utils/helpers/flows/Flow__Events.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from osbot_utils.utils.Str import ansis_to_texts, ansi_to_text

from osbot_utils.utils.Dev import pprint

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
Expand All @@ -14,6 +18,15 @@ def on__flow__stop(self, flow):
flow_event = Flow__Event(event_type=Flow__Event_Type.FLOW_STOP , event_source=flow)
self.raise_event(flow_event)

def on__flow_run__message(self, flow, log_level, flow_run_id, task_run_id, message):
event_data = dict(flow_run_id = flow_run_id ,
log_level = log_level ,
message = message ,
message_text = ansi_to_text(message) ,
task_run_id = task_run_id )
flow_event = Flow__Event(event_type=Flow__Event_Type.FLOW_MESSAGE, event_source=flow, event_data=event_data)
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)
Expand Down
11 changes: 8 additions & 3 deletions osbot_utils/helpers/flows/Task.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ class Task(Type_Safe):
task_error : Exception = None
raise_on_error : bool = True

def log_info(self, message):
self.task_flow.log_info(message, self.task_id)

def log_debug(self, message):
self.task_flow.log_debug(message)
self.task_flow.log_debug(message, self.task_id)

def log_error(self, message):
self.task_flow.log_error(message)
self.task_flow.log_error(message, self.task_id)

def execute__sync(self):
self.execute__before()
Expand All @@ -54,6 +57,8 @@ def execute__before(self):
if not self.task_id:
self.task_id = self.random_task_id()

flow_events.on__task__start(self)

self.task_flow.executed_tasks.append(self)
self.log_debug(f"Executing task '{f_blue(self.task_name)}'")
dependency_manager = Dependency_Manager()
Expand All @@ -62,7 +67,7 @@ def execute__before(self):
dependency_manager.add_dependency('task_data', self.data )
dependency_manager.add_dependency('flow_data', self.task_flow.data)
self.resolved_args, self.resolved_kwargs = dependency_manager.resolve_dependencies(self.task_target, *self.task_args, **self.task_kwargs)
flow_events.on__task__start(self)


def execute__task_target__sync(self):
try:
Expand Down
10 changes: 5 additions & 5 deletions osbot_utils/helpers/flows/models/Flow__Event_Type.py
Original file line number Diff line number Diff line change
@@ -1,8 +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'
FLOW_MESSAGE: str = 'flow_message'
FLOW_START : str = 'flow_start'
FLOW_STOP : str = 'flow_stop'
TASK_START : str = 'task_start'
TASK_STOP : str = 'task_stop'
2 changes: 1 addition & 1 deletion osbot_utils/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.55.0
v1.55.1
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "osbot_utils"
version = "v1.55.0"
version = "v1.55.1"
description = "OWASP Security Bot - Utils"
authors = ["Dinis Cruz <[email protected]>"]
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/helpers/flows/test_Flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def just_print_a_message():
_.flow_config.print_none_return_value = True
_.flow_config.print_finished_message = True
_.execute_flow()
assert ansis_to_texts(_.captured_exec_logs) == ["Executing flow run 'AN-FLOW-ID''",
assert ansis_to_texts(_.captured_exec_logs) == ["Executing flow run 'AN-FLOW-ID'",
'this is inside the flow',
'Flow return value: None',
"Finished flow run 'AN-FLOW-ID''"]
"Finished flow run 'AN-FLOW-ID'"]



6 changes: 3 additions & 3 deletions tests/unit/helpers/flows/test_decorator__flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def an_method_with_flow(name):

with disable_root_loggers():
flow_1 = an_method_with_flow('world').execute()
assert flow_1.flow_return_value == [ "Executing flow run 'THE-FLOW-ID''" ,
assert flow_1.flow_return_value == [ "Executing flow run 'THE-FLOW-ID'" ,
'hello world' ,
'this is from an TASK that found the flow' ]

Expand Down Expand Up @@ -73,7 +73,7 @@ def an_method_with_flow(self, value):
assert flow_1.flow_return_value == 42
assert type(flow_1) is Flow
flow_id = flow_1.flow_id
assert flow_1.captured_logs() == [ f"Executing flow run '{flow_id}''",
assert flow_1.captured_logs() == [ f"Executing flow run '{flow_id}'",
"Executing task 'exec_task_1'",
'inside task 1 with 1',
'Task return value: 2',
Expand All @@ -84,6 +84,6 @@ def an_method_with_flow(self, value):
"Finished task 'exec_task_2'",
'inside the flow!',
'Flow return value: 42',
f"Finished flow run '{flow_id}''"]
f"Finished flow run '{flow_id}'"]


0 comments on commit 14e2103

Please sign in to comment.