diff --git a/trace_commentor/flags.py b/trace_commentor/flags.py index ec5837d..783d7ea 100644 --- a/trace_commentor/flags.py +++ b/trace_commentor/flags.py @@ -13,11 +13,12 @@ COMMENT = 2 NORMAL = 0 BREAK = 8 CONTINUE = 16 +RAISE = 32 REG = lambda i: f"__REG{i}" APPEND_SOURCE_BY_THEMSELVES = [ - ast.If, ast.For + ast.If, ast.For, ast.With ] ASSIGN_SILENT = [ diff --git a/trace_commentor/handlers/__init__.py b/trace_commentor/handlers/__init__.py index c63b467..b78732b 100644 --- a/trace_commentor/handlers/__init__.py +++ b/trace_commentor/handlers/__init__.py @@ -3,4 +3,4 @@ from .statements import Pass, Assign, AnnAssign, AugAssign from .expressions import Expr, UnaryOp, BinOp, Call, Compare, Attribute, Subscript, Slice, keyword, IfExp from .literals import Constant, Tuple, List, Set, Dict, FormattedValue, JoinedStr from .variables import Name -from .control_flow import If, For, Continue, Break +from .control_flow import If, For, Continue, Break, With, Raise diff --git a/trace_commentor/handlers/control_flow.py b/trace_commentor/handlers/control_flow.py index ce36e88..2e702cb 100644 --- a/trace_commentor/handlers/control_flow.py +++ b/trace_commentor/handlers/control_flow.py @@ -108,3 +108,25 @@ def Break(self, cmtor): def Continue(self, cmtor): cmtor._lines[-1] += " # True" cmtor._stack_event = flags.CONTINUE + + +def With(self, cmtor): + for wi in self.items: + optional_vars = getattr(wi, "optional_vars", None) + if optional_vars is not None: + cmtor.process(wi.optional_vars) + + cmtor.indent += flags.INDENT + for stmt in self.body: + if type(stmt) not in flags.APPEND_SOURCE_BY_THEMSELVES: + cmtor.append_source(cmtor.to_source(stmt)) + if cmtor._stack_event == flags.NORMAL: + cmtor.process(stmt) + else: + cmtor._lines[-1] += " # skipped" + cmtor.append_source("") + cmtor.indent -= flags.INDENT + + +def Raise(self, cmtor): + cmtor._stack_event = flags.RAISE diff --git a/trace_commentor/handlers/definitions.py b/trace_commentor/handlers/definitions.py index ca2e0f5..554c6e6 100644 --- a/trace_commentor/handlers/definitions.py +++ b/trace_commentor/handlers/definitions.py @@ -14,11 +14,11 @@ def FunctionDef(self, cmtor): if type(stmt) not in flags.APPEND_SOURCE_BY_THEMSELVES: cmtor.append_source(cmtor.to_source(stmt)) - if self is cmtor.root: + if self is cmtor.root and cmtor._stack_event == flags.NORMAL: cmtor.process(stmt) cmtor.append_source() - + cmtor.indent -= flags.INDENT diff --git a/trace_commentor/handlers/expressions.py b/trace_commentor/handlers/expressions.py index 8abea14..a4dcd0d 100644 --- a/trace_commentor/handlers/expressions.py +++ b/trace_commentor/handlers/expressions.py @@ -60,3 +60,7 @@ def keyword(self, cmtor): def IfExp(self, cmtor): cmtor.append_comment(cmtor.eval(self.test)) cmtor.append_comment(cmtor.eval(self)) + + +def Index(self, cmtor): + cmtor.process(self.value)