Skip to content

Commit 4f964a9

Browse files
committed
Add option extract_from_dir and frontend to follow symbolic links when traversing a directory
1 parent d7a7589 commit 4f964a9

4 files changed

Lines changed: 19 additions & 2 deletions

File tree

babel/messages/extract.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def extract_from_dir(
126126
callback: Callable[[str, str, dict[str, Any]], object] | None = None,
127127
strip_comment_tags: bool = False,
128128
directory_filter: Callable[[str], bool] | None = None,
129+
follow_links: bool = False,
129130
) -> Generator[_FileExtractionResult, None, None]:
130131
"""Extract messages from any source files found in the given directory.
131132
@@ -194,6 +195,8 @@ def extract_from_dir(
194195
:param directory_filter: a callback to determine whether a directory should
195196
be recursed into. Receives the full directory path;
196197
should return True if the directory is valid.
198+
:param follow_links: Whether symbolic links should be followed in OS's that
199+
support them. By default they are not followed.
197200
:see: `pathmatch`
198201
"""
199202
if dirname is None:
@@ -204,7 +207,7 @@ def extract_from_dir(
204207
directory_filter = default_directory_filter
205208

206209
absname = os.path.abspath(dirname)
207-
for root, dirnames, filenames in os.walk(absname):
210+
for root, dirnames, filenames in os.walk(absname, followlinks=follow_links):
208211
dirnames[:] = [
209212
subdir for subdir in dirnames
210213
if directory_filter(os.path.join(root, subdir))

babel/messages/frontend.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,12 @@ class ExtractMessages(CommandMixin):
347347
'header comment for the catalog'),
348348
('last-translator=', None,
349349
'set the name and email of the last translator in output'),
350+
('follow-links', 'l',
351+
'follow symbolic links when traversing directories'),
350352
]
351353
boolean_options = [
352354
'no-default-keywords', 'no-location', 'omit-header', 'no-wrap',
353-
'sort-output', 'sort-by-file', 'strip-comments',
355+
'sort-output', 'sort-by-file', 'strip-comments', 'follow-links'
354356
]
355357
as_args = 'input-paths'
356358
multiple_value_options = (
@@ -390,6 +392,7 @@ def initialize_options(self):
390392
self.version = None
391393
self.add_comments = None
392394
self.strip_comments = False
395+
self.follow_links = False
393396
self.include_lineno = True
394397
self.ignore_dirs = None
395398
self.header_comment = None
@@ -518,6 +521,7 @@ def run(self):
518521
callback=callback,
519522
strip_comment_tags=self.strip_comments,
520523
directory_filter=self.directory_filter,
524+
follow_links=self.follow_links,
521525
)
522526
for filename, lineno, message, comments, context in extracted:
523527
if os.path.isfile(path):

docs/cmdline.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ a collection of source files::
129129
Patterns for directories to ignore when scanning for
130130
messages. Separate multiple patterns with spaces
131131
(default ".* ._")
132+
-f, --follow-links
133+
follow symbolic links when traversing directories
132134
--header-comment=HEADER_COMMENT
133135
header comment for the catalog
134136

tests/messages/test_frontend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,14 @@ def test_extract_cli_knows_dash_s():
16081608
assert cmdinst.strip_comments
16091609

16101610

1611+
1612+
def test_extract_cli_knows_follow_links():
1613+
# This tests the follow-links command line argument
1614+
cmdinst = configure_cli_command("extract --follow-links -o foo babel")
1615+
assert isinstance(cmdinst, ExtractMessages)
1616+
assert cmdinst.follow_links
1617+
1618+
16111619
def test_extract_cli_knows_dash_dash_last_dash_translator():
16121620
cmdinst = configure_cli_command('extract --last-translator "FULL NAME EMAIL@ADDRESS" -o foo babel')
16131621
assert isinstance(cmdinst, ExtractMessages)

0 commit comments

Comments
 (0)