diff options
author | Aarni Koskela <akx@iki.fi> | 2023-02-07 16:42:25 +0200 |
---|---|---|
committer | Aarni Koskela <akx@iki.fi> | 2023-02-07 16:43:32 +0200 |
commit | fc8fca49eb78db2b05c7dd766ac3aad97499e96e (patch) | |
tree | 5b7682865c3028b230f0e27a1bd34a33257f05d3 | |
parent | 08af5e2bab184c1b5d357ebde8c0efdbe6288e2c (diff) | |
download | babel-p694-redux.tar.gz |
Improve extract performance via ignoring directories early during os.walkp694-redux
Co-authored-by: Steven Kao <st.kao.05@gmail.com>
-rw-r--r-- | babel/messages/extract.py | 37 |
1 files changed, 29 insertions, 8 deletions
diff --git a/babel/messages/extract.py b/babel/messages/extract.py index 5a34f64..d97e947 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -102,10 +102,25 @@ def _strip_comment_tags(comments: MutableSequence[str], tags: Iterable[str]): comments[:] = map(_strip, comments) -def default_directory_filter(dirpath: str | os.PathLike[str]) -> bool: - subdir = os.path.basename(dirpath) - # Legacy default behavior: ignore dot and underscore directories - return not (subdir.startswith('.') or subdir.startswith('_')) +def make_default_directory_filter( + method_map: Iterable[tuple[str, str]], + root_dir: str | os.PathLike[str], +): + def directory_filter(dirpath: str | os.PathLike[str]) -> bool: + subdir = os.path.basename(dirpath) + # Legacy default behavior: ignore dot and underscore directories + if subdir.startswith('.') or subdir.startswith('_'): + return False + + dir_rel = os.path.relpath(dirpath, root_dir).replace(os.sep, '/') + + for pattern, method in method_map: + if method == "ignore" and pathmatch(pattern, dir_rel): + return False + + return True + + return directory_filter def extract_from_dir( @@ -189,13 +204,19 @@ def extract_from_dir( """ if dirname is None: dirname = os.getcwd() + if options_map is None: options_map = {} + + dirname = os.path.abspath(dirname) + if directory_filter is None: - directory_filter = default_directory_filter + directory_filter = make_default_directory_filter( + method_map=method_map, + root_dir=dirname, + ) - absname = os.path.abspath(dirname) - for root, dirnames, filenames in os.walk(absname): + for root, dirnames, filenames in os.walk(dirname): dirnames[:] = [ subdir for subdir in dirnames if directory_filter(os.path.join(root, subdir)) @@ -213,7 +234,7 @@ def extract_from_dir( keywords, comment_tags, strip_comment_tags, - dirpath=absname, + dirpath=dirname, ) |