Skip to content

Commit 3a9b24d

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 14e7c00 commit 3a9b24d

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
@@ -102,10 +102,25 @@ def _strip(line: str):
102102
comments[:] = map(_strip, comments)
103103

104104

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

110125

111126
def extract_from_dir(
@@ -189,13 +204,19 @@ def extract_from_dir(
189204
"""
190205
if dirname is None:
191206
dirname = os.getcwd()
207+
192208
if options_map is None:
193209
options_map = {}
210+
211+
dirname = os.path.abspath(dirname)
212+
194213
if directory_filter is None:
195-
directory_filter = default_directory_filter
214+
directory_filter = make_default_directory_filter(
215+
method_map=method_map,
216+
root_dir=dirname,
217+
)
196218

197-
absname = os.path.abspath(dirname)
198-
for root, dirnames, filenames in os.walk(absname):
219+
for root, dirnames, filenames in os.walk(dirname):
199220
dirnames[:] = [
200221
subdir for subdir in dirnames
201222
if directory_filter(os.path.join(root, subdir))
@@ -213,7 +234,7 @@ def extract_from_dir(
213234
keywords,
214235
comment_tags,
215236
strip_comment_tags,
216-
dirpath=absname,
237+
dirpath=dirname,
217238
)
218239

219240

0 commit comments

Comments
 (0)