import os
import sys
import tokenize
+import warnings
from collections.abc import (
Callable,
Collection,
comments[:] = [_strip(c) for c in comments]
-def default_directory_filter(dirpath: str | os.PathLike[str]) -> bool:
+def _make_default_directory_filter(
+ method_map: Iterable[tuple[str, str]],
+ root_dir: str | os.PathLike[str],
+):
+ method_map = tuple(method_map)
+
+ 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 default_directory_filter(dirpath: str | os.PathLike[str]) -> bool: # pragma: no cover
+ warnings.warn(
+ "`default_directory_filter` is deprecated and will be removed in a future version of Babel.",
+ DeprecationWarning,
+ stacklevel=2,
+ )
subdir = os.path.basename(dirpath)
# Legacy default behavior: ignore dot and underscore directories
return not (subdir.startswith('.') or subdir.startswith('_'))
"""
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))
]
keywords,
comment_tags,
strip_comment_tags,
- dirpath=absname,
+ dirpath=dirname,
)
@freeze_time("1994-11-11")
-def test_extraction_with_mapping_dict(extract_cmd, pot_file):
+@pytest.mark.parametrize("ignore_pattern", ['**/ignored/**.*', 'ignored'])
+def test_extraction_with_mapping_dict(extract_cmd, pot_file, ignore_pattern):
extract_cmd.distribution.message_extractors = {
'project': [
- ('**/ignored/**.*', 'ignore', None),
+ (ignore_pattern, 'ignore', None),
('**.py', 'python', None),
],
}