Skip to content

Commit

Permalink
another important refactoring on making the naming of the fields cons…
Browse files Browse the repository at this point in the history
…istent : ignore_internals is now show_internals

fixed two nasty bugs: first related to capturing class from "osbot_utils.utils.trace.Trace_Call" , second related to how the last node in the tree was fixed
  • Loading branch information
DinisCruz committed Jan 9, 2024
1 parent 7a61c81 commit d55c258
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 36 deletions.
4 changes: 2 additions & 2 deletions osbot_utils/utils/trace/Trace_Call.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def trace_calls(title=None, print=True, show_locals=False, source_code=False, ignore=None, include=None,
max_string=None, show_types=False, show_caller=False, show_class=False, show_path=False,
show_duration=False, duration_bigger_than=0, contains=None, ignore_internals=True,
show_duration=False, duration_bigger_than=0, contains=None, show_internals=False,
extra_data=False):
def decorator(func):
@wraps(func)
Expand All @@ -23,7 +23,7 @@ def wrapper(*args, **kwargs):
show_caller=show_caller, show_source_code_path=show_path,
capture_duration=show_duration, print_duration= show_duration,
with_duration_bigger_than=duration_bigger_than,
trace_capture_contains=contains, trace_ignore_internals=ignore_internals,
trace_capture_contains=contains, trace_show_internals=show_internals,
capture_extra_data=extra_data)

config = Trace_Call__Config(**config_kwargs)
Expand Down
2 changes: 1 addition & 1 deletion osbot_utils/utils/trace/Trace_Call__Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class Trace_Call__Config(Kwargs_To_Self):
trace_capture_source_code : bool
trace_capture_start_with : list
trace_capture_contains : list
trace_ignore_internals : bool = True
trace_show_internals : bool
trace_ignore_start_with : list
with_duration_bigger_than : bool
5 changes: 3 additions & 2 deletions osbot_utils/utils/trace/Trace_Call__Handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def should_capture(self, frame):
code = frame.f_code # Get code object from frame
func_name = code.co_name # Get function name
module = frame.f_globals.get("__name__", "") # Get module name

if module == 'osbot_utils.utils.trace.Trace_Call': # don't trace the trace module
return False # todo: figure out if there is a performance implication of doing this string comparison here (or if there is a better way to detect this)
if module and func_name:
if self.config.trace_capture_all:
capture = True
Expand All @@ -61,7 +62,7 @@ def should_capture(self, frame):
if item in module or item in func_name:
capture = True
break
if self.config.trace_ignore_internals and func_name.startswith('_'): # Skip private functions
if self.config.trace_show_internals is False and func_name.startswith('_'): # Skip private functions
capture = False

for item in self.config.trace_ignore_start_with: # Check if the module should be ignored
Expand Down
4 changes: 2 additions & 2 deletions osbot_utils/utils/trace/Trace_Call__View_Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def create_view_model(self, json_list, level=0, prefix="", view_model=None):
return view_model

def fix_view_mode(self):
if len(self.view_model) > 0:
if len(self.view_model) > 0: # these changes will provide a nice end of tree, for example replacing "│ ├──" with "└────────── "
last_node = self.view_model[-1] # Get the last node in the view model
last_node['prefix'] = '└───' # Update the prefix for the last node
last_node['prefix'] = last_node['prefix'].replace(' ', '─').replace('│', '└') # Update the prefix for the last node
last_node['tree_branch'] = '─── '
8 changes: 4 additions & 4 deletions tests/utils/trace/test_Trace_Call.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test___exit__(self):

mock_stop.assert_called_with()
self.trace_call.create_view_model() # this is populated by the self.trace_view_model.view_model object
assert self.trace_view_model.view_model == [{ 'duration': 0.0, 'extra_data': {}, 'prefix': '└───', 'tree_branch': '─── ', 'emoji': '📦 ',
assert self.trace_view_model.view_model == [{ 'duration': 0.0, 'extra_data': {}, 'prefix': '', 'tree_branch': '─── ', 'emoji': '📦 ',
'method_name': '', 'method_parent': '',
'parent_info': '', 'locals': {}, 'source_code': '',
'source_code_caller': '', 'source_code_location': ''}]
Expand All @@ -84,7 +84,7 @@ def method_b() :
call('\x1b[1m│ └── 🔗️ method_b\x1b[0m'),
call('\x1b[1m│ └── 🔗️ pprint\x1b[0m'),
call('\x1b[1m│ └── 🔗️ pprint\x1b[0m'),
call('\x1b[1m└────── 🧩️ format\x1b[0m')] != []
call('\x1b[1m└────────────────────── 🧩️ format\x1b[0m')] != []


def test_stop(self):
Expand Down Expand Up @@ -141,7 +141,7 @@ def test___enter__exit__(self, builtins_print):
call('\x1b[1m📦 Trace Session\x1b[0m'),
call('\x1b[1m│ ├── 🧩️ dummy_function\x1b[0m test_Trace_Call'),
call('\x1b[1m│ └── 🔗️ another_function\x1b[0m test_Trace_Call'),
call('\x1b[1m└────── 🧩️ dummy_function\x1b[0m test_Trace_Call')] != []
call('\x1b[1m└────────── 🧩️ dummy_function\x1b[0m test_Trace_Call')] != []

# todo: figure out why we are getting these two different values
if in_github_action():
Expand Down Expand Up @@ -217,7 +217,7 @@ def an_temp_file():
call('\x1b[1m│ ├── 🔗️ random_filename\x1b[0m'),
call('\x1b[1m│ │ └── 🧩️ file_extension_fix\x1b[0m'),
call('\x1b[1m│ └── 🔗️ random_filename\x1b[0m'),
call('\x1b[1m└────── 🧩️ file_extension_fix\x1b[0m')] != []
call('\x1b[1m└────────── 🧩️ file_extension_fix\x1b[0m')] != []

assert list_set(self.handler.stats.frames_stats()) == ['codecs', 'genericpath', 'os',
'osbot_utils', 'posixpath', 'random', 'shutil',
Expand Down
2 changes: 1 addition & 1 deletion tests/utils/trace/test_Trace_Call__Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test__kwargs__(self):
'trace_capture_source_code': False ,
'trace_capture_start_with' : [] ,
'trace_capture_contains' : [] ,
'trace_ignore_internals' : True ,
'trace_ignore_start_with' : [] ,
'trace_show_internals' : False ,
'with_duration_bigger_than': False }
assert Trace_Call__Config.__cls_kwargs__ () == expected_data
assert Trace_Call__Config.__default_kwargs__() == expected_data
Expand Down
10 changes: 5 additions & 5 deletions tests/utils/trace/test_Trace_Call__Handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_should_capture(self):
# check default config values that impact logic

assert config.trace_capture_all is False
assert config.trace_ignore_internals is True
assert config.trace_show_internals is False

# case 1: with invalid values on frame
assert should_capture(frame=None) is False
Expand All @@ -137,12 +137,12 @@ def test_should_capture(self):


# case 4: with trace_ignore_internals set for False
config.trace_ignore_internals = False
config.trace_show_internals = False
assert should_capture(frame=sample_frame) is True
assert should_capture(frame=sample_frame) is True

# case 5: with trace_ignore_start_with set
config.trace_ignore_internals = True
config.trace_show_internals = False
config.trace_ignore_start_with = ['test']
config.trace_capture_start_with = ['test']
assert should_capture(frame=sample_frame) is False
Expand Down Expand Up @@ -172,14 +172,14 @@ def test_should_capture(self):

# case 9: nteraction Between trace_ignore_internals and trace_capture_start_with/trace_ignore_start_with
config.trace_capture_start_with = ['mod']
config.trace_ignore_internals = True
config.trace_show_internals = False
assert should_capture(frame=sample_frame) is False # todo: improve the logic of this (since it has lost a bit of the meaning after the refactoring to should_capture(frame=sample_frame) )

# case 10: Functionality When All Configs are Empty or Default
config.trace_capture_all = False
config.trace_capture_start_with = []
config.trace_ignore_start_with = []
config.trace_ignore_internals = True
config.trace_show_internals = False
assert should_capture(frame=sample_frame) is False # Assuming default behavior is to not capture

# Case 11: Overlapping Patterns
Expand Down
34 changes: 17 additions & 17 deletions tests/utils/trace/test_Trace_Call__Print_Traces.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def test_print_traces(self):
call('Here are the 3 traces captured\n'),
call('\x1b[1m📦 Trace Session\x1b[0m'),
call('\x1b[1m│ └── 🔗️ another_function\x1b[0m tests.utils.trace.test_Trace_Call'),
call('\x1b[1m└────── 🧩️ dummy_function\x1b[0m tests.utils.trace.test_Trace_Call')]
call('\x1b[1m└────────── 🧩️ dummy_function\x1b[0m tests.utils.trace.test_Trace_Call')]


with patch('builtins.print') as mock_print:
Expand All @@ -89,7 +89,7 @@ def test_print_traces(self):
call('Here are the 3 traces captured\n'),
call('\x1b[1m📦 \x1b[38;2;138;148;138m\x1b[0m.\x1b[1mTrace Session\x1b[0m\x1b[0m'),
call('\x1b[1m│ └── 🔗️ \x1b[38;2;138;148;138mtest_Trace_Call\x1b[0m.\x1b[1manother_function\x1b[0m\x1b[0m'),
call('\x1b[1m└────── 🧩️ \x1b[38;2;138;148;138mtest_Trace_Call\x1b[0m.\x1b[1mdummy_function\x1b[0m\x1b[0m')]
call('\x1b[1m└────────── 🧩️ \x1b[38;2;138;148;138mtest_Trace_Call\x1b[0m.\x1b[1mdummy_function\x1b[0m\x1b[0m')]

#handler.stack.add_node(title=DEFAULT_ROOT_NODE_NODE_TITLE) # add a root node
handler.trace_capture_start_with = ['test']
Expand All @@ -111,8 +111,8 @@ def test_print_traces(self):
call('➡️📦 \x1b[90m\x1b[38;2;138;148;138m\x1b[0m.\x1b[1mTrace Session\x1b[0m\x1b[0m'),
call('│ └── 🔼️\x1b[1manother_function()\x1b[0m'),
call('│ └── ➡️🔗️ \x1b[90mdef another_function():\x1b[0m'),
call('└────── 🔼️\x1b[1mdummy_function()\x1b[0m'),
call('└────── ➡️🧩️ \x1b[90mdef dummy_function():\x1b[0m')]
call('└────────── 🔼️\x1b[1mdummy_function()\x1b[0m'),
call('└────────── ➡️🧩️ \x1b[90mdef dummy_function():\x1b[0m')]


trace_call.trace_call_print_traces.config.show_caller = False
Expand All @@ -123,7 +123,7 @@ def test_print_traces(self):
call('Here are the 3 traces captured\n'),
call('➡️📦 \x1b[1m\x1b[38;2;138;148;138m\x1b[0m.\x1b[1mTrace Session\x1b[0m\x1b[0m'),
call('│ └── ➡️🔗️ \x1b[1mdef another_function():\x1b[0m'),
call('└────── ➡️🧩️ \x1b[1mdef dummy_function():\x1b[0m')]
call('└────────── ➡️🧩️ \x1b[1mdef dummy_function():\x1b[0m')]

trace_call.trace_call_print_traces.config.capture_locals = True
trace_call.trace_call_print_traces.config.print_locals = True
Expand All @@ -139,8 +139,8 @@ def test_print_traces(self):
call('Here are the 3 traces captured\n'),
call('➡️📦 \x1b[1m\x1b[38;2;138;148;138m\x1b[0m.\x1b[1mTrace Session\x1b[0m\x1b[0m'),
call('│ └── ➡️🔗️ \x1b[1mdef another_function():\x1b[0m'),
call('└────── ➡️🧩️ \x1b[1mdef dummy_function():\x1b[0m'),
call('│ 🔖 \x1b[1m\x1b[38;2;120;120;120ma\x1b[0m = \x1b[92m12\x1b[0m')]
call('└────────── ➡️🧩️ \x1b[1mdef dummy_function():\x1b[0m'),
call('│ 🔖 \x1b[1m\x1b[38;2;120;120;120ma\x1b[0m = \x1b[92m12\x1b[0m')]



Expand Down Expand Up @@ -179,14 +179,14 @@ def even_more_slower():
trace_call.print()
assert mock_print.call_args_list == [call(),
call('--------- CALL TRACER ----------'),
call('Here are the 17 traces captured\n'),
call('Here are the 16 traces captured\n'),
call('\x1b[1m📦 Trace Session\x1b[0m'),
call('\x1b[1m│ ├── 🔗️ an_fast_function\x1b[0m test_Trace_Call__Print_Traces'),
call('\x1b[1m│ │ └── 🧩️ wait\x1b[0m osbot_utils.utils.Misc'),
call('\x1b[1m│ ├── 🔗️ a_bit_slower\x1b[0m test_Trace_Call__Print_Traces'),
call('\x1b[1m│ │ └── 🧩️ wait\x1b[0m osbot_utils.utils.Misc'),
call('\x1b[1m│ ── 🔗️ even_more_slower\x1b[0m test_Trace_Call__Print_Traces'),
call('\x1b[1m│ │ └── 🧩️ wait\x1b[0m osbot_utils.utils.Misc')]
call('\x1b[1m│ ── 🔗️ even_more_slower\x1b[0m test_Trace_Call__Print_Traces'),
call('\x1b[1m└────────── 🧩️ wait\x1b[0m osbot_utils.utils.Misc')]



Expand All @@ -195,27 +195,27 @@ def even_more_slower():
trace_call.print()
assert mock_print.call_args_list == [call(),
call('--------- CALL TRACER ----------'),
call('Here are the 17 traces captured\n'),
call('Here are the 16 traces captured\n'),
call('\x1b[1m📦 Trace Session\x1b[0m'),
call('\x1b[1m│ ├── 🔗️ a_bit_slower\x1b[0m test_Trace_Call__Print_Traces'),
call('\x1b[1m│ │ └── 🧩️ wait\x1b[0m osbot_utils.utils.Misc'),
call('\x1b[1m│ ── 🔗️ even_more_slower\x1b[0m test_Trace_Call__Print_Traces'),
call('\x1b[1m│ │ └── 🧩️ wait\x1b[0m osbot_utils.utils.Misc')]
call('\x1b[1m│ ── 🔗️ even_more_slower\x1b[0m test_Trace_Call__Print_Traces'),
call('\x1b[1m└────────── 🧩️ wait\x1b[0m osbot_utils.utils.Misc')]

config.with_duration_bigger_than = 50 / 1000
with patch('builtins.print') as mock_print:
trace_call.print()
assert mock_print.call_args_list == [call(),
call('--------- CALL TRACER ----------'),
call('Here are the 17 traces captured\n'),
call('Here are the 16 traces captured\n'),
call('\x1b[1m📦 Trace Session\x1b[0m'),
call('\x1b[1m│ ── 🔗️ even_more_slower\x1b[0m test_Trace_Call__Print_Traces'),
call('\x1b[1m│ │ └── 🧩️ wait\x1b[0m osbot_utils.utils.Misc')] != []
call('\x1b[1m│ ── 🔗️ even_more_slower\x1b[0m test_Trace_Call__Print_Traces'),
call('\x1b[1m└────────── 🧩️ wait\x1b[0m osbot_utils.utils.Misc')]

config.with_duration_bigger_than = 150 / 1000
with patch('builtins.print') as mock_print:
trace_call.print()
assert mock_print.call_args_list == [call(),
call('--------- CALL TRACER ----------'),
call('Here are the 17 traces captured\n')] != []
call('Here are the 16 traces captured\n')] != []

Loading

0 comments on commit d55c258

Please sign in to comment.