Expr(); Constant();

This commit is contained in:
Yuyao Huang 2024-04-19 13:57:21 +08:00
parent e3094a6522
commit 5de5cfdcb8
8 changed files with 55 additions and 3 deletions

10
tests/test_literals.py Normal file
View File

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

View File

@ -18,6 +18,7 @@ class Commentor(object):
self._formatters = _formatters + formatters.LIST
self._lines = []
self.indent = 0
self.state = flags.SOURCE
def __call__(self, func):
@ -60,3 +61,15 @@ class Commentor(object):
def append(self, line):
self._lines.append(" " * self.indent + str(line))
def append_source(self, line):
if self.state == flags.COMMENT:
self.append('"""')
self.append(line)
def append_comment(self, line):
if self.state == flags.SOURCE:
self.append('"""')
self.append(line)

View File

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

View File

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

View File

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

View File

@ -0,0 +1,2 @@
def Expr(self, cmtor):
cmtor.process(self.value)

View File

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

13
trace_commentor/utils.py Normal file
View File

@ -0,0 +1,13 @@
import astor
import inspect
from . import flags
def sign(line: str):
if flags.DEBUG:
debug_msg = inspect.currentframe().f_back.f_code.co_name
return f"{line} --- {debug_msg}"
else:
return line
def to_source(node):
return astor.to_source(node).rstrip("\n")