1
- import glob
2
1
from typing import List
3
2
4
3
# metadata
5
- __version__ = "0.2.2"
4
+ __version__ = "0.3.0"
5
+
6
+ from flake8 .discover_files import expand_paths
7
+
6
8
CODE_PREFIX = "IFI" # stands for "In File Ignores"
7
9
8
10
9
11
IFI_TAG = "# flake8-in-file-ignores:"
10
12
IFI_FULL_TAG = "# flake8-in-file-ignores: noqa:"
11
13
12
14
13
- def get_cwd_py_files () -> List [str ]:
14
- return glob .glob ("**/*.py" , recursive = True )
15
-
16
-
17
15
def read_first_line (filepath : str ) -> str :
18
16
with open (filepath , errors = "ignore" ) as f :
19
17
first_line = f .readline ().rstrip ()
@@ -25,15 +23,15 @@ def build_pfi_str(filepath: str, error_codes_csv: str):
25
23
return f"{ filepath } :{ error_codes_csv } "
26
24
27
25
28
- def get_cwd_pfi_noqa ( ) -> List [str ]:
29
- pfi_noqa_item = []
30
- for file_path in get_cwd_py_files () :
26
+ def get_ifi_noqa ( filepaths : List [ str ] ) -> List [str ]:
27
+ ifi_noqa_item = []
28
+ for file_path in filepaths :
31
29
# print(f"Checking {file_path}")
32
30
first_line = read_first_line (file_path )
33
31
if IFI_TAG in first_line :
34
32
error_codes = parse_ifi_error_codes (first_line )
35
- pfi_noqa_item .append (build_pfi_str (file_path , error_codes ))
36
- return pfi_noqa_item
33
+ ifi_noqa_item .append (build_pfi_str (file_path , error_codes ))
34
+ return ifi_noqa_item
37
35
38
36
39
37
def parse_ifi_error_codes (line : str ) -> str :
@@ -61,17 +59,32 @@ def __init__(self, tree, filename: str):
61
59
@classmethod
62
60
def parse_options (cls , option_manager , options , args ):
63
61
# print(option_manager, options, args)
64
- pfi_noqa_strs = get_cwd_pfi_noqa ()
65
- if pfi_noqa_strs :
66
- prev_pfi = options .per_file_ignores
67
- concat_pfi = " " .join (pfi_noqa_strs )
62
+ excludes = (* options .exclude , * options .extend_exclude )
63
+ filepaths = cls .get_files (options , excludes )
64
+ # print(f"{filepaths=}")
65
+ ifi_noqa_strs = get_ifi_noqa (filepaths )
66
+ if ifi_noqa_strs :
67
+ # original_pfi = options.per_file_ignores
68
+ concat_pfi = " " .join (ifi_noqa_strs )
68
69
options .per_file_ignores += " " + concat_pfi
69
70
# print(
70
- # f"flake8-in-file-ignores - Patching options.per_file_ignores from `{prev_pfi }` to `{options.per_file_ignores}`"
71
+ # f"flake8-in-file-ignores - Patching options.per_file_ignores from `{original_pfi }` to `{options.per_file_ignores}`"
71
72
# )
72
73
else :
73
74
# print(f"flake8-in-file-ignores - No match found")
74
75
pass
75
76
77
+ @staticmethod
78
+ def get_files (options , excludes ):
79
+ # inspired from flake8 codebase
80
+ return tuple (
81
+ expand_paths (
82
+ paths = options .filenames ,
83
+ stdin_display_name = options .stdin_display_name ,
84
+ filename_patterns = options .filename ,
85
+ exclude = excludes ,
86
+ )
87
+ )
88
+
76
89
def run (self ):
77
90
yield from []
0 commit comments