Skip to content

Commit

Permalink
Merge pull request #40 from LucasAndradeDias/update-cli
Browse files Browse the repository at this point in the history
Update cli arguments
  • Loading branch information
LucasAndradeDias authored Oct 5, 2023
2 parents 43c633c + 1bd9427 commit 41ed409
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
31 changes: 26 additions & 5 deletions src/outliner/cli.py
Original file line number Diff line number Diff line change
@@ -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()


Expand All @@ -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()
11 changes: 8 additions & 3 deletions src/outliner/outliner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion src/outliner/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 16 additions & 16 deletions tests/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,67 +20,67 @@ 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 <listcomp> + \n "
)

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))

Expand Down

0 comments on commit 41ed409

Please sign in to comment.