1818import warnings
1919from pathlib import Path
2020
21+ _EXCLUDE_DIRS = {"docs" , "docker" , "dist" , "tests" }
22+
2123
2224def read_in_source_file (file_path ):
25+ """
26+ Reads the contents of a Python source file and returns it as a string.
27+
28+ Args:
29+ file_path (str or Path): The path to the Python source file.
30+
31+ Returns:
32+ str: The contents of the file.
33+ """
34+
2335 # Ensure file_path is a Path object
2436 file_path = Path (file_path )
2537
@@ -34,8 +46,7 @@ def read_in_source_file(file_path):
3446 sys .exit (1 )
3547
3648 try :
37- with file_path .open ("r" , encoding = "utf-8" ) as f :
38- return f .read ()
49+ return file_path .read_text (encoding = "utf-8" )
3950 except Exception as e :
4051 print (f"Failed to read file: { e } " )
4152 sys .exit (1 )
@@ -53,32 +64,33 @@ def collect_python_source_files(directory):
5364 Returns:
5465 list: A list of absolute paths to valid Python source files.
5566 """
56- EXCLUDE_DIRS = { "docs" , "docker" , "dist" , "tests" }
67+ directory = Path ( directory )
5768 python_files = []
5869
59- for root , dirs , files in os .walk (directory ):
60- # Filter out unwanted directories
61- dirs [:] = [
62- d
63- for d in dirs
64- if not (d .startswith ("." ) or d .startswith ("_" ) or d in EXCLUDE_DIRS )
65- ]
66-
67- for file in files :
68- if file .endswith (".py" ) and not file .startswith ("." ):
69- full_path = os .path .join (root , file )
70- if os .path .isfile (full_path ):
71- python_files .append (os .path .abspath (full_path ))
72- # check if the file can be parsed using the AST
73- final_file_list = []
74- for python_file in python_files :
75- if is_ast_parsable (python_file ):
76- final_file_list .append (python_file )
77- else :
70+ for file in directory .rglob ("**/*.py" ):
71+ # Skip hidden directories (start with '.') or underscore-prefixed directories
72+ if any (
73+ part .startswith ("_" ) or part .startswith ("." ) for part in file .parts [:- 1 ]
74+ ):
75+ continue
76+
77+ # Skip directories in the exclusion list
78+ if any (part in _EXCLUDE_DIRS for part in file .parts [:- 1 ]):
79+ continue
80+
81+ # Skip hidden files (start with '.') or underscore-prefixed files
82+ if file .name .startswith ("_" ) or file .name .startswith ("." ) and not file .exists ():
83+ continue
84+
85+ # Skip files that cannot be parsed into AST
86+ if not is_ast_parsable (file ):
7887 print (
79- f"Error: { python_file } will be skipped due to syntax error while parsing into AST."
88+ f"Error: { file } will be skipped due to syntax error while parsing into AST."
8089 )
81- return final_file_list
90+ continue
91+ python_files .append (file .absolute ().as_posix ())
92+
93+ return python_files
8294
8395
8496def get_filename_from_path (file_path ):
0 commit comments