From 6ada6ee29351558bbbcc922a757794916d73945b Mon Sep 17 00:00:00 2001 From: Steven Kao Date: Tue, 11 Feb 2020 15:00:51 +0800 Subject: [PATCH] Improve extract performance via ignoring directory early during os.walk --- babel/messages/extract.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/babel/messages/extract.py b/babel/messages/extract.py index e7d7ad70f..b4118e086 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -136,11 +136,22 @@ def extract_from_dir(dirname=None, method_map=DEFAULT_MAPPING, options_map = {} absname = os.path.abspath(dirname) + + def dir_filter(method_map, dirname): + if dirname.startswith('.') or dirname.startswith('_'): + return False + + absdir = os.path.join(root, dirname).replace(os.sep, '/') + for pattern, method in method_map: + if method == "ignore" and pathmatch(pattern, absdir): + return False + + return True + + for root, dirnames, filenames in os.walk(absname): - dirnames[:] = [ - subdir for subdir in dirnames - if not (subdir.startswith('.') or subdir.startswith('_')) - ] + dirnames[:] = [subdir for subdir in dirnames if dir_filter(method_map, subdir)] + dirnames.sort() filenames.sort() for filename in filenames: