add Pass(), FunctionDef();
This commit is contained in:
parent
11a5cc3719
commit
e3094a6522
BIN
conda/env.yml
BIN
conda/env.yml
Binary file not shown.
10
tests/test_definitions.py
Normal file
10
tests/test_definitions.py
Normal file
@ -0,0 +1,10 @@
|
||||
from trace_commentor import Commentor
|
||||
|
||||
|
||||
def test_function_def():
|
||||
|
||||
@Commentor()
|
||||
def target():
|
||||
pass
|
||||
|
||||
print(target())
|
||||
@ -0,0 +1 @@
|
||||
from .commentor import Commentor
|
||||
62
trace_commentor/commentor.py
Normal file
62
trace_commentor/commentor.py
Normal file
@ -0,0 +1,62 @@
|
||||
import inspect
|
||||
import ast
|
||||
import astor
|
||||
|
||||
from inspect import getfullargspec
|
||||
from functools import wraps
|
||||
|
||||
from . import handlers
|
||||
from . import formatters
|
||||
from . import flags
|
||||
|
||||
|
||||
class Commentor(object):
|
||||
|
||||
def __init__(self, _formatters=[]) -> None:
|
||||
self._locals = dict()
|
||||
self._globals = dict()
|
||||
self._formatters = _formatters + formatters.LIST
|
||||
self._lines = []
|
||||
self.indent = 0
|
||||
|
||||
def __call__(self, func):
|
||||
|
||||
raw_lines, start_lineno = inspect.getsourcelines(func)
|
||||
self.indent = len(raw_lines[0]) - len(raw_lines[0].lstrip())
|
||||
unindented_source = ''.join([l[self.indent:] for l in raw_lines])
|
||||
self.root = ast.parse(unindented_source).body[0]
|
||||
|
||||
if flags.DEBUG:
|
||||
with open("test.log", "wt") as f:
|
||||
print(ast.dump(self.root, indent=4), file=f)
|
||||
|
||||
@wraps(func)
|
||||
def proxy_func(*args, **kwargs):
|
||||
self._locals = kwargs
|
||||
self.process(self.root)
|
||||
return "\n".join(self._lines)
|
||||
|
||||
return proxy_func
|
||||
|
||||
def process(self, node: ast.AST):
|
||||
node_type = node.__class__.__name__
|
||||
handler = getattr(handlers, node_type, None)
|
||||
if handler is None:
|
||||
raise NotImplementedError(f"Unknown how to handle {node_type} node.")
|
||||
return handler(node, self)
|
||||
|
||||
def eval(self, node: ast.Expr):
|
||||
src = astor.code_gen.to_source(node)
|
||||
obj = eval(src, self._globals, self._locals)
|
||||
fmt = self.get_formatter(obj)
|
||||
return f"{fmt(obj)} : {src}"
|
||||
|
||||
def get_formatter(self, obj):
|
||||
for typ, fmt in self._formatters:
|
||||
if isinstance(obj, typ):
|
||||
return fmt
|
||||
else:
|
||||
return repr
|
||||
|
||||
def append(self, line):
|
||||
self._lines.append(" " * self.indent + str(line))
|
||||
2
trace_commentor/flags.py
Normal file
2
trace_commentor/flags.py
Normal file
@ -0,0 +1,2 @@
|
||||
DEBUG = True
|
||||
INDENT = 4
|
||||
1
trace_commentor/formatters/__init__.py
Normal file
1
trace_commentor/formatters/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
LIST = []
|
||||
2
trace_commentor/handlers/__init__.py
Normal file
2
trace_commentor/handlers/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from .definitions import FunctionDef
|
||||
from .statements import Pass
|
||||
9
trace_commentor/handlers/definitions.py
Normal file
9
trace_commentor/handlers/definitions.py
Normal file
@ -0,0 +1,9 @@
|
||||
from ..flags import INDENT
|
||||
|
||||
def FunctionDef(self, cmtor):
|
||||
cmtor.append("def function():")
|
||||
cmtor.indent += INDENT
|
||||
|
||||
if self is cmtor.root:
|
||||
for stmt in self.body:
|
||||
cmtor.process(stmt)
|
||||
2
trace_commentor/handlers/statements.py
Normal file
2
trace_commentor/handlers/statements.py
Normal file
@ -0,0 +1,2 @@
|
||||
def Pass(self, cmtor):
|
||||
cmtor.append("pass")
|
||||
@ -1,32 +0,0 @@
|
||||
import inspect
|
||||
import ast
|
||||
import astor
|
||||
|
||||
# from .interpretor import exec_ast
|
||||
|
||||
|
||||
class Parser(object):
|
||||
|
||||
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
|
||||
def __call__(self, func, **_globals):
|
||||
|
||||
raw_lines, start_lineno = inspect.getsourcelines(func)
|
||||
indent_size = len(raw_lines[0]) - len(raw_lines[0].lstrip())
|
||||
unindented_source = ''.join([l[indent_size:] for l in raw_lines])
|
||||
root = ast.parse(unindented_source).body[0]
|
||||
|
||||
def proxy_func(*args, **kwargs):
|
||||
_locals = kwargs
|
||||
# exec_ast(root, _locals, _globals)
|
||||
import ipdb; ipdb.set_trace()
|
||||
root = ast.parse(unindented_source).body[0]
|
||||
ast.dump(root)
|
||||
...
|
||||
|
||||
return proxy_func
|
||||
|
||||
|
||||
analyse = Parser()
|
||||
Loading…
x
Reference in New Issue
Block a user