Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions babel/messages/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import os
import sys
import tokenize
import warnings
from collections.abc import (
Callable,
Collection,
Expand Down Expand Up @@ -114,7 +115,35 @@ def _strip(line: str):
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('_'))
Expand Down Expand Up @@ -201,13 +230,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))
]
Expand All @@ -224,7 +259,7 @@ def extract_from_dir(
keywords,
comment_tags,
strip_comment_tags,
dirpath=absname,
dirpath=dirname,
)


Expand Down
5 changes: 3 additions & 2 deletions tests/messages/frontend/test_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ def test_extraction_with_mapping_file(extract_cmd, pot_file):


@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),
],
}
Expand Down
Loading