Skip to content

Commit c68ca01

Browse files
akxstkao05
andcommitted
Improve extract performance via ignoring directories early during os.walk
Co-authored-by: Steven Kao <[email protected]>
1 parent 8e10fb5 commit c68ca01

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

babel/messages/extract.py

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,25 @@ def _strip(line: str):
111111
comments[:] = map(_strip, comments)
112112

113113

114-
def default_directory_filter(dirpath: str | os.PathLike[str]) -> bool:
115-
subdir = os.path.basename(dirpath)
116-
# Legacy default behavior: ignore dot and underscore directories
117-
return not (subdir.startswith('.') or subdir.startswith('_'))
114+
def make_default_directory_filter(
115+
method_map: Iterable[tuple[str, str]],
116+
root_dir: str | os.PathLike[str],
117+
):
118+
def directory_filter(dirpath: str | os.PathLike[str]) -> bool:
119+
subdir = os.path.basename(dirpath)
120+
# Legacy default behavior: ignore dot and underscore directories
121+
if subdir.startswith('.') or subdir.startswith('_'):
122+
return False
123+
124+
dir_rel = os.path.relpath(dirpath, root_dir).replace(os.sep, '/')
125+
126+
for pattern, method in method_map:
127+
if method == "ignore" and pathmatch(pattern, dir_rel):
128+
return False
129+
130+
return True
131+
132+
return directory_filter
118133

119134

120135
def extract_from_dir(
@@ -198,13 +213,19 @@ def extract_from_dir(
198213
"""
199214
if dirname is None:
200215
dirname = os.getcwd()
216+
201217
if options_map is None:
202218
options_map = {}
219+
220+
dirname = os.path.abspath(dirname)
221+
203222
if directory_filter is None:
204-
directory_filter = default_directory_filter
223+
directory_filter = make_default_directory_filter(
224+
method_map=method_map,
225+
root_dir=dirname,
226+
)
205227

206-
absname = os.path.abspath(dirname)
207-
for root, dirnames, filenames in os.walk(absname):
228+
for root, dirnames, filenames in os.walk(dirname):
208229
dirnames[:] = [
209230
subdir for subdir in dirnames
210231
if directory_filter(os.path.join(root, subdir))
@@ -222,7 +243,7 @@ def extract_from_dir(
222243
keywords,
223244
comment_tags,
224245
strip_comment_tags,
225-
dirpath=absname,
246+
dirpath=dirname,
226247
)
227248

228249

0 commit comments

Comments
 (0)