From d0dd3ccf77a133c5c3506c06f46a5a7cc20e4305 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 26 Jun 2024 06:51:13 -0400 Subject: [PATCH 1/6] Add machine-readable output format Passing `--machine-readable` / `-m` to codespell, or setting `machine-readable = true` in the config file, will enable an output format designed to be more easily parsed, and containing the start column of the match (usually not output). Inspired by the `eslint -f compact` format (which is no longer part of standard eslint), but with an added `@` at the start of each line. (Useful when combined with context or summary options, to parse only the lines containing error data). Intended primarily for actions-codespell and other wrapper scripts. --- codespell_lib/_codespell.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index f5070dc2d9..e72b25d81c 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -465,6 +465,13 @@ def parse_options( default=False, help="print summary of fixes", ) + parser.add_argument( + "-m", + "--machine-readable", + action="store_true", + default=False, + help="Use alternate output format intended for parsing", + ) parser.add_argument( "--count", @@ -1018,6 +1025,15 @@ def parse_file( if (not context_shown) and (context is not None): print_context(lines, i, context) + + if options.machine_readable: + mrreason = f" ({reason})" if reason else "" + print( + f"@{filename}: line {i + 1}, col {match.start() + 1}, " + f"{word} ==> {fixword}{mrreason}" + ) + continue + if filename != "-": print( f"{cfilename}:{cline}: {cwrongword} " From bf36798dc61e6d3b97eed98096b81b5794c9c365 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 26 Jun 2024 07:41:04 -0400 Subject: [PATCH 2/6] Add test for machine-readable output --- codespell_lib/tests/test_basic.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 98e5dd41f0..9187bae00f 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -5,6 +5,7 @@ import re import subprocess import sys +import textwrap from io import StringIO from pathlib import Path from shutil import copyfile @@ -325,6 +326,28 @@ def test_summary( assert "abandonned" in stdout.split()[-2] +def test_machine_readable( + tmp_path: Path, + capsys: pytest.CaptureFixture[str], +) -> None: + """Test machine-readable output format.""" + fname = tmp_path / "mrfile.txt" + fname.write_text(textwrap.dedent("""\ + abandonned + the word is abandonned + the word abandonned is wrong + """ + )) + result = cs.main(fname, "--machine-readable", std=True) + assert isinstance(result, tuple) + code, stdout, stderr = result + output_lines = [line for line in stdout.split('\n') if line] + assert all([l.startswith('@') for l in output_lines]) + assert "line 1, col 1, abandonned ==> abandoned" in output_lines[0] + assert "line 2, col 13" in output_lines[1] + assert "line 3, col 10" in output_lines[2] + + def test_ignore_dictionary( tmp_path: Path, capsys: pytest.CaptureFixture[str], From 08ff843da9bd3a04979691ac235e8a91cd9b836d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Jun 2024 12:01:23 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- codespell_lib/tests/test_basic.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 9187bae00f..253dc08927 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -332,17 +332,18 @@ def test_machine_readable( ) -> None: """Test machine-readable output format.""" fname = tmp_path / "mrfile.txt" - fname.write_text(textwrap.dedent("""\ + fname.write_text( + textwrap.dedent("""\ abandonned the word is abandonned the word abandonned is wrong - """ - )) + """) + ) result = cs.main(fname, "--machine-readable", std=True) assert isinstance(result, tuple) code, stdout, stderr = result - output_lines = [line for line in stdout.split('\n') if line] - assert all([l.startswith('@') for l in output_lines]) + output_lines = [line for line in stdout.split("\n") if line] + assert all([l.startswith("@") for l in output_lines]) assert "line 1, col 1, abandonned ==> abandoned" in output_lines[0] assert "line 2, col 13" in output_lines[1] assert "line 3, col 10" in output_lines[2] From bd5c2ba47c7db07c25e7595e251ebbfa96e04421 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 26 Jun 2024 08:08:18 -0400 Subject: [PATCH 4/6] Fix ambiguous variable name --- codespell_lib/tests/test_basic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 253dc08927..e6fc870207 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -342,8 +342,8 @@ def test_machine_readable( result = cs.main(fname, "--machine-readable", std=True) assert isinstance(result, tuple) code, stdout, stderr = result - output_lines = [line for line in stdout.split("\n") if line] - assert all([l.startswith("@") for l in output_lines]) + output_lines = [line for line in stdout.split('\n') if line] + assert all([outline.startswith('@') for outline in output_lines]) assert "line 1, col 1, abandonned ==> abandoned" in output_lines[0] assert "line 2, col 13" in output_lines[1] assert "line 3, col 10" in output_lines[2] From 2dbed9ce619900b70a599a61bfa6ee2f2b9e233b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 26 Jun 2024 12:08:35 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- codespell_lib/tests/test_basic.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index e6fc870207..216663a275 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -342,8 +342,8 @@ def test_machine_readable( result = cs.main(fname, "--machine-readable", std=True) assert isinstance(result, tuple) code, stdout, stderr = result - output_lines = [line for line in stdout.split('\n') if line] - assert all([outline.startswith('@') for outline in output_lines]) + output_lines = [line for line in stdout.split("\n") if line] + assert all([outline.startswith("@") for outline in output_lines]) assert "line 1, col 1, abandonned ==> abandoned" in output_lines[0] assert "line 2, col 13" in output_lines[1] assert "line 3, col 10" in output_lines[2] From d93b5e0ea7c6b183f410cf1af688ededa1bad214 Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Wed, 26 Jun 2024 08:18:28 -0400 Subject: [PATCH 6/6] Bump ruff length and branch limits --- pyproject.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 87fa3c4b2e..c2827981e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -169,6 +169,6 @@ max-complexity = 45 [tool.ruff.lint.pylint] allow-magic-value-types = ["bytes", "int", "str",] max-args = 13 -max-branches = 46 +max-branches = 48 max-returns = 11 -max-statements = 119 +max-statements = 125