Skip to content
Closed
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
8 changes: 6 additions & 2 deletions babel/messages/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def _strip(line):

def extract_from_dir(dirname=None, method_map=DEFAULT_MAPPING,
options_map=None, keywords=DEFAULT_KEYWORDS,
comment_tags=(), callback=None, strip_comment_tags=False):
comment_tags=(), callback=None, strip_comment_tags=False,
directory_filter=('.','_')):
"""Extract messages from any source files found in the given directory.

This function generates tuples of the form ``(filename, lineno, message,
Expand Down Expand Up @@ -128,6 +129,9 @@ def extract_from_dir(dirname=None, method_map=DEFAULT_MAPPING,
positional arguments, in that order
:param strip_comment_tags: a flag that if set to `True` causes all comment
tags to be removed from the collected comments.
:param directory_filter: a list of strings that identify the starting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, this might be more useful if it declared a list of fnmatch compatible patterns.

Also, now that I think of it, it could maybe just accept a callable? After all there's no command-line support for this argument yet, so it needn't be a "command line compatible". The default could be default_directory_filter, defined as

def default_directory_filter(path):
  subdir = os.path.basename(path)
  return not (subdir.startswith('.') or subdir.startswith('_'))

and the implementation in the walk loop simply

dirnames[:] = [dirname for dirname in dirnames if directory_filter(os.path.join(root, dirname)]

characters of directories to ignore. (optional)
(defaults to ('.', '_'))
:see: `pathmatch`
"""
if dirname is None:
Expand All @@ -138,7 +142,7 @@ def extract_from_dir(dirname=None, method_map=DEFAULT_MAPPING,
absname = os.path.abspath(dirname)
for root, dirnames, filenames in os.walk(absname):
for subdir in dirnames:
if subdir.startswith('.') or subdir.startswith('_'):
if any(subdir.startswith(chars) for chars in directory_filter):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

starstwith can also accept a tuple of strings to try .. so if subdir.startswith(directory_filter) should suffice

Also while at it, extract_messages command should be modified to accept the directory_filter argument in order to be passed to extract_from_dir

dirnames.remove(subdir)
dirnames.sort()
filenames.sort()
Expand Down