Skip to content

Commit 23ad3e7

Browse files
committed
BLD: refactor validate_unwanted_patterns.py
Extract common code for checking a single file path.
1 parent 6929e26 commit 23ad3e7

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

scripts/validate_unwanted_patterns.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def main(
416416
function : Callable
417417
Function to execute for the specified validation type.
418418
source_path : str
419-
Source path representing path to a file/directory.
419+
Source file_path representing file_path to a file/directory.
420420
output_format : str
421421
Output format of the error message.
422422
file_extensions_to_check : str
@@ -437,16 +437,19 @@ def main(
437437
if not os.path.exists(source_path):
438438
raise ValueError("Please enter a valid path, pointing to a file/directory.")
439439

440-
is_failed: bool = False
441-
file_path: str = ""
442-
443440
FILE_EXTENSIONS_TO_CHECK: FrozenSet[str] = frozenset(
444441
file_extensions_to_check.split(",")
445442
)
446-
PATHS_TO_IGNORE = frozenset(excluded_file_paths.split(","))
443+
PATHS_TO_IGNORE = frozenset(
444+
os.path.abspath(os.path.normpath(path))
445+
for path in excluded_file_paths.split(",")
446+
)
447+
448+
is_failed: bool = False
449+
450+
def check_file(file_path: str):
451+
nonlocal is_failed
447452

448-
if os.path.isfile(source_path):
449-
file_path = source_path
450453
with open(file_path, "r") as file_obj:
451454
for line_number, msg in function(file_obj):
452455
is_failed = True
@@ -456,24 +459,21 @@ def main(
456459
)
457460
)
458461

459-
for subdir, _, files in os.walk(source_path):
460-
if any(path in subdir for path in PATHS_TO_IGNORE):
461-
continue
462-
for file_name in files:
463-
if not any(
464-
file_name.endswith(extension) for extension in FILE_EXTENSIONS_TO_CHECK
465-
):
462+
if os.path.isfile(source_path):
463+
check_file(source_path)
464+
else:
465+
for subdir, _, files in os.walk(source_path):
466+
if any(path in subdir for path in PATHS_TO_IGNORE):
466467
continue
467-
468-
file_path = os.path.join(subdir, file_name)
469-
with open(file_path, "r") as file_obj:
470-
for line_number, msg in function(file_obj):
471-
is_failed = True
472-
print(
473-
output_format.format(
474-
source_path=file_path, line_number=line_number, msg=msg
475-
)
476-
)
468+
for file_name in files:
469+
if not any(
470+
file_name.endswith(extension)
471+
for extension in FILE_EXTENSIONS_TO_CHECK
472+
):
473+
continue
474+
475+
file_path = os.path.join(subdir, file_name)
476+
check_file(file_path)
477477

478478
return is_failed
479479

0 commit comments

Comments
 (0)