From 986186e1cbf2f97f30076bace61359c0e15ba303 Mon Sep 17 00:00:00 2001 From: LucasAndradeDias Date: Thu, 5 Oct 2023 11:02:04 -0300 Subject: [PATCH 1/2] Updated trace run to run_file (more intuitive name) --- src/outliner/trace.py | 2 +- tests/test_trace.py | 32 ++++++++++++++++---------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/outliner/trace.py b/src/outliner/trace.py index db0ae6d..9bc6923 100644 --- a/src/outliner/trace.py +++ b/src/outliner/trace.py @@ -15,7 +15,7 @@ def _trace_function(self, frame, event, arg): self.functions_flow[frame.f_code.co_name] = None return self._trace_function - def run(self, module_name: str, func: str, func_params=None): + def run_file(self, module_name: str, func: str, func_params=None): try: trace_import = importlib.util.spec_from_file_location(func, module_name) trace_obj = importlib.util.module_from_spec(trace_import) diff --git a/tests/test_trace.py b/tests/test_trace.py index 948efca..3f8d09e 100644 --- a/tests/test_trace.py +++ b/tests/test_trace.py @@ -20,26 +20,26 @@ def tearDown(self): ) self.parser.functions_flow = collections.OrderedDict() - def test_run_positive_input(self): + def test_run_file_positive_input(self): """ Given a valid module path and a callable function "test", - When Trace.run is called with these parameters, + When Trace.run_file is called with these parameters, Then it should trace the function and produce the expected trace result. """ module_path = self.mock_path + "/module_testing_1.py" - self.parser.run(module_path, "test") + self.parser.run_file(module_path, "test") expected_trace = r"Trace object\nrunned objects:\n __init__ + \n func1 + \n func2 + \n " self.assertEqual(expected_trace, repr(self.parser)) - def test_run_positive_input_with_parameters(self): + def test_run_file_positive_input_with_parameters(self): """ Given a valid module path, a callable function "numberToIp", and function parameters ["000"], - When Trace.run is called with these parameters, + When Trace.run_file is called with these parameters, Then it should trace the function and produce the expected trace result. """ module_path = self.mock_path + "/module_testing_2.py" - self.parser.run(module_path, "numberToIp", ["000"]) + self.parser.run_file(module_path, "numberToIp", ["000"]) expected_trace = ( r"Trace object\nrunned objects:\n numberToIp + \n + \n " @@ -47,40 +47,40 @@ def test_run_positive_input_with_parameters(self): self.assertEqual(expected_trace, repr(self.parser)) - def test_trace_run_negative_no_module(self): + def test_trace_run_file_negative_no_module(self): """ Given an invalid module path, - When Trace.run is called with these parameters, + When Trace.run_file is called with these parameters, Then it should raise an exception with an appropriate error message. """ with self.assertRaises(Exception) as error: - self.parser.run("not_valid_path", "object") + self.parser.run_file("not_valid_path", "object") self.assertTrue("Error on the execution of the module" in str(error.exception)) - def test_trace_run_negative_non_callable_object(self): + def test_trace_run_file_file_negative_non_callable_object(self): """ Given a valid module path and a non-callable object "non_callable", - When Trace.run is called with these parameters, + When Trace.run_file is called with these parameters, Then it should raise an exception with an appropriate error message. """ module_path = ( self.mock_path + "/module_testing_2.py" ) # Contains a non-callable object with self.assertRaises(Exception) as error: - self.parser.run(module_path, "non_callable") + self.parser.run_file(module_path, "non_callable") self.assertEqual( "Object 'non_callable' not found in module" in str(error.exception) ) - def test_trace_run_positive_multiple_runs(self): + def test_trace_run_file_positive_multiple_runs(self): """ Given a valid module path and a callable function "test", - When Trace.run is called twice with these parameters, + When Trace.run_file is called twice with these parameters, Then it should trace the functions individually and produce the expected trace result for each run. """ module_path = self.mock_path + "/module_testing_1.py" - self.parser.run(module_path, "test") - self.parser.run(module_path, "test2") + self.parser.run_file(module_path, "test") + self.parser.run_file(module_path, "test2") expected_trace = r"Trace object\nrunned objects:\n __init__ + \n func1 + \n func2 + \n test2 + \n " self.assertEqual(expected_trace, repr(self.parser)) From 1bd942797c7651851d5b1b6b115b4cf7c4a4a0ab Mon Sep 17 00:00:00 2001 From: LucasAndradeDias Date: Thu, 5 Oct 2023 11:03:36 -0300 Subject: [PATCH 2/2] Adding more arguments to the user on display data --- src/outliner/cli.py | 31 ++++++++++++++++++++++++++----- src/outliner/outliner.py | 11 ++++++++--- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/outliner/cli.py b/src/outliner/cli.py index 01fb6a6..ff14b06 100644 --- a/src/outliner/cli.py +++ b/src/outliner/cli.py @@ -1,13 +1,33 @@ import argparse -from .outliner import Outliner # Import functions from your library +from .outliner import Outliner def parser_arguments(): parser = argparse.ArgumentParser() - parser.add_argument("--file_path", required=True) - parser.add_argument("--object_type", required=False) - parser.add_argument("--object_name", type=str, required=True) - parser.add_argument("--object_args", action="append", type=str, required=False) + parser.add_argument( + "--file_path", help="Path to the file you want to trace", required=True + ) + parser.add_argument( + "--object_name", + help="The name of the object you want to trace", + type=str, + required=True, + ) + parser.add_argument( + "--object_args", + help="A list with the argumments required to run the object", + action="append", + type=str, + required=False, + ) + parser.add_argument( + "--display", + "-d", + help="Select the type of display you want", + choices=["tree", "detailed_data"], + default="tree", + ) + return parser.parse_args() @@ -18,6 +38,7 @@ def main(): args.file_path, args.object_name, args.object_args[0].split() if args.object_args else None, + args.display, ) instance_class.run() diff --git a/src/outliner/outliner.py b/src/outliner/outliner.py index 1611764..a7d7507 100644 --- a/src/outliner/outliner.py +++ b/src/outliner/outliner.py @@ -7,10 +7,11 @@ class Outliner: - def __init__(self, file_path, object_name, object_args=None): + def __init__(self, file_path, object_name, object_args, display_type): self.file = file_path self.obj_name = object_name self.obj_args = object_args + self.display_type = display_type def run(self): """ @@ -21,9 +22,13 @@ def run(self): self._check_file() trace = Trace() - trace.run(self.file, self.obj_name, self.obj_args if self.obj_args else None) + trace.run_file( + self.file, self.obj_name, self.obj_args if self.obj_args else None + ) display_class = Display(trace.detailed_data, trace.functions_flow) - display_class.tree() + + running_display = getattr(display_class, self.display_type) + running_display() def _check_file(self): if not Path(self.file).is_file():