Skip to content

Commit b5c2f1c

Browse files
akxstkao05
andcommitted
Improve extract performance via ignoring directories early during os.walk
Co-authored-by: Steven Kao <st.kao.05@gmail.com>
1 parent fe82fbc commit b5c2f1c

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

babel/messages/extract.py

+29-8
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,25 @@ def _strip(line: str):
109109
comments[:] = map(_strip, comments)
110110

111111

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

117132

118133
def extract_from_dir(
@@ -196,13 +211,19 @@ def extract_from_dir(
196211
"""
197212
if dirname is None:
198213
dirname = os.getcwd()
214+
199215
if options_map is None:
200216
options_map = {}
217+
218+
dirname = os.path.abspath(dirname)
219+
201220
if directory_filter is None:
202-
directory_filter = default_directory_filter
221+
directory_filter = make_default_directory_filter(
222+
method_map=method_map,
223+
root_dir=dirname,
224+
)
203225

204-
absname = os.path.abspath(dirname)
205-
for root, dirnames, filenames in os.walk(absname):
226+
for root, dirnames, filenames in os.walk(dirname):
206227
dirnames[:] = [
207228
subdir for subdir in dirnames
208229
if directory_filter(os.path.join(root, subdir))
@@ -220,7 +241,7 @@ def extract_from_dir(
220241
keywords,
221242
comment_tags,
222243
strip_comment_tags,
223-
dirpath=absname,
244+
dirpath=dirname,
224245
)
225246

226247

0 commit comments

Comments
 (0)