From: Aarni Koskela Date: Thu, 27 Jan 2022 13:53:26 +0000 (+0200) Subject: extract_from_dir: make directory filter customizable X-Git-Tag: v2.10.0~20^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6d4ab52b5b14f71717fb645f1f8f34f75bbae392;p=thirdparty%2Fbabel.git extract_from_dir: make directory filter customizable --- diff --git a/babel/messages/extract.py b/babel/messages/extract.py index 1c94b78c..c23a924b 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -60,9 +60,22 @@ def _strip_comment_tags(comments, tags): comments[:] = map(_strip, comments) -def extract_from_dir(dirname=None, method_map=DEFAULT_MAPPING, - options_map=None, keywords=DEFAULT_KEYWORDS, - comment_tags=(), callback=None, strip_comment_tags=False): +def default_directory_filter(dirpath): + subdir = os.path.basename(dirpath) + # Legacy default behavior: ignore dot and underscore directories + return not (subdir.startswith('.') or subdir.startswith('_')) + + +def extract_from_dir( + dirname=None, + method_map=DEFAULT_MAPPING, + options_map=None, + keywords=DEFAULT_KEYWORDS, + comment_tags=(), + callback=None, + strip_comment_tags=False, + directory_filter=None, +): """Extract messages from any source files found in the given directory. This function generates tuples of the form ``(filename, lineno, message, @@ -127,18 +140,23 @@ 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 callback to determine whether a directory should + be recursed into. Receives the full directory path; + should return True if the directory is valid. :see: `pathmatch` """ if dirname is None: dirname = os.getcwd() if options_map is None: options_map = {} + if directory_filter is None: + directory_filter = default_directory_filter absname = os.path.abspath(dirname) for root, dirnames, filenames in os.walk(absname): dirnames[:] = [ subdir for subdir in dirnames - if not (subdir.startswith('.') or subdir.startswith('_')) + if directory_filter(os.path.join(root, subdir)) ] dirnames.sort() filenames.sort()