Source code for statick_tool.plugins.tool.isort_tool_plugin
"""Apply isort tool and gather results.
The isort tool will only find if a file has issues with imports. To automatically fix
the issues you can run `isort <file>`.
"""
import logging
import subprocess
from typing import List, Optional
from statick_tool.issue import Issue
from statick_tool.package import Package
from statick_tool.tool_plugin import ToolPlugin
[docs]class IsortToolPlugin(ToolPlugin):
"""Apply isort tool and gather results."""
[docs] def get_file_types(self) -> List[str]:
"""Return a list of file types the plugin can scan."""
return ["python_src"]
[docs] def process_files(
self, package: Package, level: str, files: List[str], user_flags: List[str]
) -> Optional[List[str]]:
"""Run tool and gather output."""
tool_bin = "isort"
flags: List[str] = ["--check-only"]
flags += user_flags
total_output: List[str] = []
try:
subproc_args = [tool_bin] + flags + files
output = subprocess.check_output(
subproc_args, stderr=subprocess.STDOUT, universal_newlines=True
)
total_output.append(output)
except (IOError, OSError) as ex:
logging.warning("isort binary failed: %s", tool_bin)
logging.warning("Error = %s", ex.strerror)
return []
except subprocess.CalledProcessError as ex:
logging.warning("isort binary failed: %s.", tool_bin)
logging.warning("Returncode: %d", ex.returncode)
logging.warning("%s exception: %s", self.get_name(), ex.output)
total_output.append(ex.output)
logging.debug("%s", total_output)
return total_output
[docs] def parse_output(
self, total_output: List[str], package: Optional[Package] = None
) -> List[Issue]:
"""Parse tool output and report issues."""
issues: List[Issue] = []
for output in total_output:
# Skip output that contain only an empty string.
if not output:
continue
msg = "Imports are incorrectly sorted and/or formatted."
issues.append(
Issue(
output,
"0",
self.get_name(),
"formatting",
"3",
msg,
None,
)
)
return issues