Skip to content

Commit

Permalink
Merge pull request #68 from sola-st/aryaz/docs
Browse files Browse the repository at this point in the history
Aryaz/docs
  • Loading branch information
AryazE authored Jul 3, 2024
2 parents e9c831a + 80c2bde commit bceb20e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 55 deletions.
14 changes: 10 additions & 4 deletions docs/hierarchy2md.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,23 @@ def to_string_list(root):
if len(root.items()) == 0:
return []
res = []
its = len(root.items())
count = 0
for k, v in root.items():
count += 1
if len(v.items()) > 0:
res.append("└ " + k)
res.append(f"└ [{k}](#TraceAll.{k}) ")
inner_res = to_string_list(v)
for i in range(len(inner_res)):
inner_res[i] = "│ " + inner_res[i]
inner_res[i] = (
("│ " + "&nbsp; " * 3) if count < its else "&nbsp; " * 5
) + inner_res[i]
res.extend(inner_res)
else:
res.append("└ " + k)
res.append(f"└ [{k}](#TraceAll.{k}) ")
return res


with open(Path(__file__).parent.resolve() / "hooks.md", "w") as f:
f.write("```bash\n" + "\n".join(to_string_list(hierarchy)) + "\n```\n")
# f.write("```\n" + "\n".join(to_string_list(hierarchy)) + "\n```\n")
f.write("\n".join(to_string_list(hierarchy)) + "\n")
118 changes: 67 additions & 51 deletions src/dynapyt/analyses/TraceAll.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import logging
from types import TracebackType
from typing import Any, Callable, ContextManager, Dict, Iterable, List, Optional, Tuple, Union
from typing import (
Any,
Callable,
ContextManager,
Dict,
Iterable,
List,
Optional,
Tuple,
Union,
)
import libcst.matchers as m
from .BaseAnalysis import BaseAnalysis
from ..utils.nodeLocator import get_node_by_location
Expand All @@ -20,8 +30,8 @@ def __init__(self, **kwargs) -> None:
root_logger.addHandler(handler)

def log(self, iid: int, *args, **kwargs):
args_str = ' '.join([str(x) for x in args])
kwargs_str = ' '.join([f"{k}={v}" for k, v in kwargs.items()])
args_str = " ".join([str(x) for x in args])
kwargs_str = " ".join([f"{k}={v}" for k, v in kwargs.items()])
logging.info(f"{iid}: {args_str} {kwargs_str}")

# Literals
Expand Down Expand Up @@ -66,6 +76,7 @@ def _float(self, dyn_ast: str, iid: int, val: Any) -> Any:
-------
Any
If provided, overwrites the value of the float literal.
@public
"""
self.log(iid, " Float", "value:", val)

Expand Down Expand Up @@ -210,7 +221,7 @@ def _list(self, dyn_ast: str, iid: int, value: List) -> List:
-------
List
If provided, overwrites the value of the list.
@public
"""
self.log(iid, "List", value)

Expand Down Expand Up @@ -239,7 +250,7 @@ def _tuple(self, dyn_ast: str, iid: int, items: List[Any], value: tuple) -> tupl
-------
tuple
If provided, overwrites the value of the tuple.
@public
"""
self.log(iid, "Tuple", "items:", items)

Expand Down Expand Up @@ -268,7 +279,7 @@ def _set(self, dyn_ast: str, iid: int, items: List[Any], value: set) -> set:
-------
set
If provided, overwrites the value of the set.
@public
"""
self.log(iid, "Set", "items:", items)

Expand Down Expand Up @@ -420,9 +431,11 @@ def subtract(
self.log(iid, "Binary Operation", left, right, "->", result)

def _and(self, dyn_ast: str, iid: int, left: Any, right: Any, result: Any) -> Any:
"""@public"""
self.log(iid, "Binary Operation", left, right, "->", result)

def _or(self, dyn_ast: str, iid: int, left: Any, right: Any, result: Any) -> Any:
"""@public"""
self.log(iid, "Binary Operation", left, right, "->", result)

def unary_operation(
Expand Down Expand Up @@ -464,6 +477,7 @@ def minus(self, dyn_ast: str, iid: int, arg: Any, result: Any) -> Any:
self.log(iid, "Unary Operation", arg, "->", result)

def _not(self, dyn_ast: str, iid: int, arg: Any, result: Any) -> Any:
"""@public"""
self.log(iid, "Unary Operation", arg, "->", result)

def plus(self, dyn_ast: str, iid: int, arg: Any, result: Any) -> Any:
Expand Down Expand Up @@ -518,9 +532,11 @@ def greater_than_equal(
self.log(iid, "Comparison", left, right, "->", result)

def _in(self, dyn_ast: str, iid: int, left: Any, right: Any, result: Any) -> Any:
"""@public"""
self.log(iid, "Comparison", left, right, "->", result)

def _is(self, dyn_ast: str, iid: int, left: Any, right: Any, result: Any) -> Any:
"""@public"""
self.log(iid, "Comparison", left, right, "->", result)

def less_than(
Expand Down Expand Up @@ -789,7 +805,7 @@ def _return(
value : Any
The value returned.
@public
"""
self.log(iid, " Returning", value)

Expand All @@ -815,7 +831,7 @@ def _yield(
value : Any
The value yielded.
@public
"""
self.log(iid, " Yielding", value)

Expand Down Expand Up @@ -1023,7 +1039,7 @@ def _raise(
-------
Exception
If provided, changes the exception raised.
@public
"""
self.log(iid, "Exception raised", exc, "because of", cause)

Expand Down Expand Up @@ -1052,7 +1068,7 @@ def _assert(
-------
bool
If provided, changes the condition of assert.
@public
"""
self.log(iid, "Asserting", condition, "with message", message)

Expand Down Expand Up @@ -1258,7 +1274,7 @@ def _break(self, dyn_ast: str, iid: int) -> Optional[bool]:
-------
bool
If False, cancels the break.
@public
"""
self.log(iid, "Break")

Expand All @@ -1279,7 +1295,7 @@ def _continue(self, dyn_ast: str, iid: int) -> Optional[bool]:
-------
bool
If False, cancels continue.
@public
"""
self.log(iid, "Continue")

Expand Down Expand Up @@ -1341,6 +1357,45 @@ def exception(
"""
self.log(iid, "Caught", caught, "from", exceptions)

def enter_with(self, dyn_ast: str, iid: int, ctx_manager: ContextManager) -> None:
"""Hook for entering a with statement.
Parameters
----------
dyn_ast : str
The path to the original code. Can be used to extract the syntax tree.
iid : int
Unique ID of the syntax tree node.
func : ContextManager
The context manager.
"""
self.log(iid, "Entered with")

def exit_with(self, dyn_ast: str, iid: int, is_suppressed: bool, exc_value):
"""Hook for exiting a with statement.
Parameters
----------
dyn_ast : str
The path to the original code. Can be used to extract the syntax tree.
iid : int
Unique ID of the syntax tree node.
is_suppressed : bool
Whether the exception, if any, inside the with block should be suppressed or not.
exc_value : Any
The exception value, if any, raised inside the with block.
"""
self.log(iid, "Exited with")

# Top level

def runtime_event(self, dyn_ast: str, iid: int) -> None:
Expand Down Expand Up @@ -1380,42 +1435,3 @@ def begin_execution(self) -> None:
def end_execution(self) -> None:
"""Hook for the end of execution."""
self.log(-1, "Execution ended")

def enter_with(self, dyn_ast: str, iid: int, ctx_manager: ContextManager) -> None:
"""Hook for entering a with statement.
Parameters
----------
dyn_ast : str
The path to the original code. Can be used to extract the syntax tree.
iid : int
Unique ID of the syntax tree node.
func : ContextManager
The context manager.
"""
self.log(iid, "Entered with")

def exit_with(self, dyn_ast: str, iid: int, is_suppressed: bool, exc_value):
"""Hook for exiting a with statement.
Parameters
----------
dyn_ast : str
The path to the original code. Can be used to extract the syntax tree.
iid : int
Unique ID of the syntax tree node.
is_suppressed : bool
Whether the exception, if any, inside the with block should be suppressed or not.
exc_value : Any
The exception value, if any, raised inside the with block.
"""
self.log(iid, "Exited with")

0 comments on commit bceb20e

Please sign in to comment.