This commit is contained in:
Yuyao Huang 2024-04-19 14:17:29 +08:00
parent 5de5cfdcb8
commit 3879263121
9 changed files with 50 additions and 31 deletions

View File

@ -13,7 +13,7 @@
"editor.wordBasedSuggestions": "matchingDocuments",
"editor.formatOnType": false,
"editor.formatOnSave": true,
"editor.formatOnSaveMode": "modifications"
"editor.formatOnSaveMode": "file"
},
"yapf.args": ["--style", "{based_on_style: google, column_limit: 88, indent_width: 4}"]
}

10
tests/test_expressions.py Normal file
View File

@ -0,0 +1,10 @@
from trace_commentor import Commentor
def test_binop():
@Commentor()
def target():
1 + 1
print(target())

View File

@ -1,6 +1,5 @@
import inspect
import ast
import astor
from inspect import getfullargspec
from functools import wraps
@ -8,6 +7,7 @@ from functools import wraps
from . import handlers
from . import formatters
from . import flags
from .utils import sign, to_source
class Commentor(object):
@ -26,7 +26,7 @@ class Commentor(object):
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)
@ -45,9 +45,9 @@ class Commentor(object):
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)
src = to_source(node)
obj = eval(src, self._globals, self._locals)
fmt = self.get_formatter(obj)
return f"{fmt(obj)} : {src}"
@ -58,18 +58,20 @@ class Commentor(object):
return fmt
else:
return repr
def append(self, line):
def __append(self, line):
self._lines.append(" " * self.indent + str(line))
def append_source(self, line):
def append_source(self, line=None):
if self.state == flags.COMMENT:
self.append('"""')
self.append(line)
def append_comment(self, line):
self.__append('"""')
self.state = flags.SOURCE
if line is not None:
self.__append(sign(line, 2))
def append_comment(self, line=None):
if self.state == flags.SOURCE:
self.append('"""')
self.append(line)
self.__append('"""')
self.state = flags.COMMENT
if line is not None:
self.__append(sign(line, 2))

View File

@ -1,4 +1,4 @@
DEBUG = True
DEBUG = False
INDENT = 4
SOURCE = 1
COMMENT = 2

View File

@ -1,4 +1,4 @@
from .definitions import FunctionDef
from .statements import Pass
from .expressions import Expr
from .expressions import Expr, BinOp
from .literals import Constant

View File

@ -1,13 +1,17 @@
from .. import flags
from ..utils import sign, to_source
from ..utils import to_source
def FunctionDef(self, cmtor):
cmtor.append_source(sign("def function():"))
cmtor.append_source("def function():")
cmtor.indent += flags.INDENT
if self is cmtor.root:
for stmt in self.body:
cmtor.append_source(sign(to_source(stmt)))
for stmt in self.body:
cmtor.append_source(to_source(stmt))
if self is cmtor.root:
cmtor.process(stmt)
cmtor.append_source()
cmtor.indent -= flags.INDENT

View File

@ -1,2 +1,7 @@
def Expr(self, cmtor):
cmtor.process(self.value)
def BinOp(self, cmtor):
cmtor.process(self.left)
cmtor.process(self.right)
cmtor.append_comment(cmtor.eval(self))

View File

@ -1,6 +1,2 @@
import ast
from .comments import comment
def Constant(self, cmtor):
pass

View File

@ -2,9 +2,11 @@ import astor
import inspect
from . import flags
def sign(line: str):
def sign(line: str, depth=1):
if flags.DEBUG:
debug_msg = inspect.currentframe().f_back.f_code.co_name
currentframe = inspect.currentframe()
outerframe = inspect.getouterframes(currentframe, depth)
debug_msg = outerframe[1][3]
return f"{line} --- {debug_msg}"
else:
return line