Skip to content

Commit

Permalink
Update to tool plugins to match new structure [sscpac/statick#423]. P…
Browse files Browse the repository at this point in the history
…inning pycodestyle version to match flake8 version. (#60)
  • Loading branch information
xydesa authored Oct 10, 2022
1 parent 988e3be commit 1eb966d
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 54 deletions.
24 changes: 11 additions & 13 deletions src/statick_md/plugins/tool/markdownlint_tool_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ def get_name(self) -> str:
"""Get name of tool."""
return "markdownlint"

def get_file_types(self) -> List[str]:
"""Return a list of file types the plugin can scan."""
return ["md_src"]

# pylint: disable=too-many-locals
def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
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 = "markdownlint"

Expand All @@ -33,13 +39,8 @@ def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
flags: List[str] = []
if format_file_name is not None:
flags += ["-c", format_file_name]
user_flags = self.get_user_flags(level)
flags += user_flags

files: List[str] = []
if "md_src" in package:
files += package["md_src"]

total_output: List[str] = []

for src in files:
Expand Down Expand Up @@ -67,16 +68,13 @@ def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
for output in total_output:
logging.debug("%s", output)

with open(self.get_name() + ".log", "w", encoding="utf8") as fid:
for output in total_output:
fid.write(output)

issues: List[Issue] = self.parse_output(total_output)
return issues
return total_output

# pylint: enable=too-many-locals

def parse_output(self, total_output: List[str]) -> List[Issue]:
def parse_output(
self, total_output: List[str], package: Optional[Package] = None
) -> List[Issue]:
"""Parse tool output and report issues."""
markdownlint_re = r"(.+):(\d+)\s([^\s]+)\s(.+)"
markdownlint_re_with_col = r"(.+):(\d+):(\d+)\s([^\s]+)\s(.+)"
Expand Down
4 changes: 2 additions & 2 deletions src/statick_md/plugins/tool/proselint_tool_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
combined = key + value
fid.write(combined)

issues: List[Issue] = self.parse_output(output)
issues: List[Issue] = self.parse_tool_output(output)
return issues

def parse_output(self, output: Dict[str, Any]) -> List[Issue]:
def parse_tool_output(self, output: Dict[str, Any]) -> List[Issue]:
"""Parse tool output and report issues."""
issues: List[Issue] = []
for key, value in output.items():
Expand Down
24 changes: 11 additions & 13 deletions src/statick_md/plugins/tool/rstcheck_tool_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ def get_name(self) -> str:
"""Get name of tool."""
return "rstcheck"

def get_file_types(self) -> List[str]:
"""Return a list of file types the plugin can scan."""
return ["rst_src"]

# pylint: disable=too-many-locals
def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
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 = "rstcheck"

flags: List[str] = []
user_flags = self.get_user_flags(level)
flags += user_flags

files: List[str] = []
if "rst_src" in package:
files += package["rst_src"]

total_output: List[str] = []

for src in files:
Expand Down Expand Up @@ -57,16 +58,13 @@ def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
for output in total_output:
logging.debug("%s", str(output))

with open(self.get_name() + ".log", "w", encoding="utf8") as fid:
for output in total_output:
fid.write(str(output))

issues: List[Issue] = self.parse_output(total_output)
return issues
return total_output

# pylint: enable=too-many-locals

def parse_output(self, total_output: List[str]) -> List[Issue]:
def parse_output(
self, total_output: List[str], package: Optional[Package] = None
) -> List[Issue]:
"""Parse tool output and report issues."""
rstcheck_re = r"(.+):(\d+):\s\((.+)/(\d)\)\s(.+)"
parse: Pattern[str] = re.compile(rstcheck_re)
Expand Down
4 changes: 2 additions & 2 deletions src/statick_md/plugins/tool/rstlint_tool_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
for output in total_output:
fid.write(str(output))

issues: List[Issue] = self.parse_output(total_output)
issues: List[Issue] = self.parse_tool_output(total_output)
return issues

# pylint: enable=too-many-locals

def parse_output(self, total_output: List[SystemMessage]) -> List[Issue]:
def parse_tool_output(self, total_output: List[SystemMessage]) -> List[Issue]:
"""Parse tool output and report issues."""
issues: List[Issue] = []

Expand Down
27 changes: 12 additions & 15 deletions src/statick_md/plugins/tool/writegood_tool_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,20 @@ def get_name(self) -> str:
"""Get name of tool."""
return "writegood"

def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
def get_file_types(self) -> List[str]:
"""Return a list of file types the plugin can scan."""
return ["md_src", "rst_src"]

# pylint: disable=too-many-locals
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 = "write-good"

flags: List[str] = ["--parse"]
user_flags = self.get_user_flags(level)
flags += user_flags

files: List[str] = []
if "md_src" in package:
files += package["md_src"]
if "rst_src" in package:
files += package["rst_src"]

total_output: List[str] = []

try:
Expand Down Expand Up @@ -61,14 +61,11 @@ def scan(self, package: Package, level: str) -> Optional[List[Issue]]:
for output in total_output:
logging.debug("%s", output)

with open(self.get_name() + ".log", "w", encoding="utf8") as fid:
for output in total_output:
fid.write(output)

issues: List[Issue] = self.parse_output(total_output)
return issues
return total_output

def parse_output(self, total_output: List[str]) -> List[Issue]:
def parse_output(
self, total_output: List[str], package: Optional[Package] = None
) -> List[Issue]:
"""Parse tool output and report issues."""
writegood_re = r"(.+):(\d+):(\d+):(.+)"
parse: Pattern[str] = re.compile(writegood_re)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def setup_markdownlint_tool_plugin():
)
config = Config(resources.get_file("config.yaml"))
plugin_context = PluginContext(arg_parser.parse_args([]), resources, config)
plugin_context.args.output_directory = os.path.dirname(__file__)
plugin = MarkdownlintToolPlugin()
plugin.set_plugin_context(plugin_context)
return plugin
Expand Down
16 changes: 8 additions & 8 deletions tests/tool/proselint_tool_plugin/test_proselint_tool_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def test_proselint_tool_plugin_parse_valid():
output = {}
errors = {"data": {"errors": [{"check": "lexical_illusions.misc", "column": 48, "end": 5231, "extent": 12, "line": 154, "message": "There's a lexical illusion here: a word is repeated.", "replacements": None, "severity": "warning", "start": 5219}]}, "status": "success"}
output["test.md"] = json.dumps(errors)
issues = plugin.parse_output(output)
issues = plugin.parse_tool_output(output)
assert len(issues) == 1
assert issues[0].filename == "test.md"
assert issues[0].line_number == "154"
Expand All @@ -128,30 +128,30 @@ def test_proselint_tool_plugin_parse_valid():

errors = {"data": {"errors": []}, "status": "success"}
output["test.md"] = json.dumps(errors)
issues = plugin.parse_output(output)
issues = plugin.parse_tool_output(output)
assert not issues

errors = {"data": {"errors": [{"severity": "suggestion", "check": None, "line": None, "message": None}]}, "status": "success"}
output["test.md"] = json.dumps(errors)
issues = plugin.parse_output(output)
issues = plugin.parse_tool_output(output)
assert len(issues) == 1
assert issues[0].severity == "1"

errors = {"data": {"errors": [{"severity": "warning", "check": None, "line": None, "message": None}]}, "status": "success"}
output["test.md"] = json.dumps(errors)
issues = plugin.parse_output(output)
issues = plugin.parse_tool_output(output)
assert len(issues) == 1
assert issues[0].severity == "3"

errors = {"data": {"errors": [{"severity": "error", "check": None, "line": None, "message": None}]}, "status": "success"}
output["test.md"] = json.dumps(errors)
issues = plugin.parse_output(output)
issues = plugin.parse_tool_output(output)
assert len(issues) == 1
assert issues[0].severity == "5"

errors = {"data": {"errors": [{"severity": "not_a_valid_severity", "check": None, "line": None, "message": None}]}, "status": "success"}
output["test.md"] = json.dumps(errors)
issues = plugin.parse_output(output)
issues = plugin.parse_tool_output(output)
assert len(issues) == 1
assert issues[0].severity == "3"

Expand All @@ -162,10 +162,10 @@ def test_proselint_tool_plugin_parse_invalid():
output = {}
errors = {}
output["test.md"] = json.dumps(errors)
issues = plugin.parse_output(output)
issues = plugin.parse_tool_output(output)
assert not issues

errors = {"data": {"errors": [{}]}, "status": "success"}
output["test.md"] = json.dumps(errors)
issues = plugin.parse_output(output)
issues = plugin.parse_tool_output(output)
assert not issues
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def setup_rstcheck_tool_plugin():
)
config = Config(resources.get_file("config.yaml"))
plugin_context = PluginContext(arg_parser.parse_args([]), resources, config)
plugin_context.args.output_directory = os.path.dirname(__file__)
plugin = RstcheckToolPlugin()
plugin.set_plugin_context(plugin_context)
return plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def setup_writegood_tool_plugin():
)
config = Config(resources.get_file("config.yaml"))
plugin_context = PluginContext(arg_parser.parse_args([]), resources, config)
plugin_context.args.output_directory = os.path.dirname(__file__)
plugin = WriteGoodToolPlugin()
plugin.set_plugin_context(plugin_context)
return plugin
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ deps =
codecov
flake8<5 # Pin until https://github.com/tholo/pytest-flake8/issues/87 is fixed.
flake8-pep3101
pycodestyle
pycodestyle<2.9.0 # Pin until https://github.com/tholo/pytest-flake8/issues/87 is fixed.
pydocstyle
pytest
pytest-cov
Expand Down

0 comments on commit 1eb966d

Please sign in to comment.