Expr(); Constant();
This commit is contained in:
parent
e3094a6522
commit
5de5cfdcb8
10
tests/test_literals.py
Normal file
10
tests/test_literals.py
Normal file
@ -0,0 +1,10 @@
|
||||
from trace_commentor import Commentor
|
||||
|
||||
|
||||
def test_constant():
|
||||
|
||||
@Commentor()
|
||||
def target():
|
||||
1
|
||||
|
||||
print(target())
|
||||
@ -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)
|
||||
|
||||
|
||||
|
||||
@ -1,2 +1,4 @@
|
||||
DEBUG = True
|
||||
INDENT = 4
|
||||
SOURCE = 1
|
||||
COMMENT = 2
|
||||
|
||||
@ -1,2 +1,4 @@
|
||||
from .definitions import FunctionDef
|
||||
from .statements import Pass
|
||||
from .expressions import Expr
|
||||
from .literals import Constant
|
||||
|
||||
@ -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
|
||||
|
||||
2
trace_commentor/handlers/expressions.py
Normal file
2
trace_commentor/handlers/expressions.py
Normal file
@ -0,0 +1,2 @@
|
||||
def Expr(self, cmtor):
|
||||
cmtor.process(self.value)
|
||||
6
trace_commentor/handlers/literals.py
Normal file
6
trace_commentor/handlers/literals.py
Normal file
@ -0,0 +1,6 @@
|
||||
import ast
|
||||
|
||||
from .comments import comment
|
||||
|
||||
def Constant(self, cmtor):
|
||||
pass
|
||||
13
trace_commentor/utils.py
Normal file
13
trace_commentor/utils.py
Normal 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")
|
||||
Loading…
x
Reference in New Issue
Block a user