Skip to content

Commit

Permalink
Merge pull request #6 from bagerard/fix_scanning_outside_flake8_filep…
Browse files Browse the repository at this point in the history
…aths

fix_scanning_outside_flake8_filepaths
  • Loading branch information
bagerard authored Oct 9, 2024
2 parents 21b35ff + 33aa9b9 commit 21e908d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/github-actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.7, 3.8, 3.9, '3.10', 'pypy3.9']
python-version: [3.8, 3.9, '3.10', 'pypy3.9']
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
36 changes: 0 additions & 36 deletions .gitlab-ci.yml

This file was deleted.

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ This plugin uses `IFI` as error code (but it will never raise any error)

## Changes

[v0.2.3]
[v0.3.0]
* Fix issue with non-utf8 first line in the files (UnicodeDecodeError) #3
* Limit the files it scans to the files processed by the flake8 invocation #5

[v0.2.x]
* Fix and pypi doc improvement
Expand Down
45 changes: 29 additions & 16 deletions flake8_in_file_ignores/flake8_plugin.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import glob
from typing import List

# metadata
__version__ = "0.2.2"
__version__ = "0.3.0"

from flake8.discover_files import expand_paths

CODE_PREFIX = "IFI" # stands for "In File Ignores"


IFI_TAG = "# flake8-in-file-ignores:"
IFI_FULL_TAG = "# flake8-in-file-ignores: noqa:"


def get_cwd_py_files() -> List[str]:
return glob.glob("**/*.py", recursive=True)


def read_first_line(filepath: str) -> str:
with open(filepath, errors="ignore") as f:
first_line = f.readline().rstrip()
Expand All @@ -25,15 +23,15 @@ def build_pfi_str(filepath: str, error_codes_csv: str):
return f"{filepath}:{error_codes_csv}"


def get_cwd_pfi_noqa() -> List[str]:
pfi_noqa_item = []
for file_path in get_cwd_py_files():
def get_ifi_noqa(filepaths: List[str]) -> List[str]:
ifi_noqa_item = []
for file_path in filepaths:
# print(f"Checking {file_path}")
first_line = read_first_line(file_path)
if IFI_TAG in first_line:
error_codes = parse_ifi_error_codes(first_line)
pfi_noqa_item.append(build_pfi_str(file_path, error_codes))
return pfi_noqa_item
ifi_noqa_item.append(build_pfi_str(file_path, error_codes))
return ifi_noqa_item


def parse_ifi_error_codes(line: str) -> str:
Expand Down Expand Up @@ -61,17 +59,32 @@ def __init__(self, tree, filename: str):
@classmethod
def parse_options(cls, option_manager, options, args):
# print(option_manager, options, args)
pfi_noqa_strs = get_cwd_pfi_noqa()
if pfi_noqa_strs:
prev_pfi = options.per_file_ignores
concat_pfi = " ".join(pfi_noqa_strs)
excludes = (*options.exclude, *options.extend_exclude)
filepaths = cls.get_files(options, excludes)
# print(f"{filepaths=}")
ifi_noqa_strs = get_ifi_noqa(filepaths)
if ifi_noqa_strs:
# original_pfi = options.per_file_ignores
concat_pfi = " ".join(ifi_noqa_strs)
options.per_file_ignores += " " + concat_pfi
# print(
# f"flake8-in-file-ignores - Patching options.per_file_ignores from `{prev_pfi}` to `{options.per_file_ignores}`"
# f"flake8-in-file-ignores - Patching options.per_file_ignores from `{original_pfi}` to `{options.per_file_ignores}`"
# )
else:
# print(f"flake8-in-file-ignores - No match found")
pass

@staticmethod
def get_files(options, excludes):
# inspired from flake8 codebase
return tuple(
expand_paths(
paths=options.filenames,
stdin_display_name=options.stdin_display_name,
filename_patterns=options.filename,
exclude=excludes,
)
)

def run(self):
yield from []

0 comments on commit 21e908d

Please sign in to comment.