Coding style.

- Tidy up gut tool.
This commit is contained in:
Andrew Hamilton 2021-05-08 02:08:09 +10:00
parent 2da603c5bf
commit f32a97cbbf

View file

@ -335,7 +335,7 @@ def python_coverage(path):
return status, _colorize_coverage_report(lines)
def _function_body_lines(module_contents):
def _function_body_lines(python_source):
ranges = []
class FuncNodeVisitor(ast.NodeVisitor):
@ -347,28 +347,22 @@ def _function_body_lines(module_contents):
def visit_AsyncFunctionDef(self, node):
ranges.append(self._line_range(node.body))
visitor = FuncNodeVisitor()
tree = ast.parse(module_contents)
visitor.visit(tree)
tree = ast.parse(python_source)
FuncNodeVisitor().visit(tree)
return ranges
def _gut_module(module_contents):
ranges = _function_body_lines(module_contents)
lines = module_contents.splitlines(keepends=True)
deleted = 0
for start_line, end_line in ranges:
del lines[start_line-deleted:end_line-deleted]
deleted += (end_line - start_line)
return "".join(lines)
@deps(url="https://github.com/ahamilton/eris")
def python_gut(path):
with open(path) as module_file:
output = _gut_module(module_file.read())
source_widget = _syntax_highlight_using_path(_fix_input(output), path)
return Status.normal, source_widget
python_source = module_file.read()
lines = python_source.splitlines(keepends=True)
deleted = 0
for start_line, end_line in _function_body_lines(python_source):
del lines[start_line-deleted:end_line-deleted]
deleted += (end_line - start_line)
gutted_source = "".join(lines)
return Status.normal, _syntax_highlight_using_path(gutted_source, path)
def _get_mccabe_line_score(line):