]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
fix for filtering unwanted directories
authorSebastian Kalinowski <sebastian.kalinowski@stxnext.pl>
Wed, 19 Mar 2014 12:01:06 +0000 (13:01 +0100)
committerSebastian Kalinowski <sebastian.kalinowski@stxnext.pl>
Wed, 19 Mar 2014 12:14:21 +0000 (13:14 +0100)
* extracted subdir filter option to a function and add a failing test
* addd a fix for filtering unwanted dirs

CHANGES
babel/messages/extract.py
babel/util.py
tests/test_util.py

diff --git a/CHANGES b/CHANGES
index 219f7cfe9cf433ca4b7eab0dfb0f0f32d04fb569..b8e501cb904d8066007ef0acec5ecbf833782e3f 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -12,6 +12,8 @@ Version 1.4
   which now properly resolves to ``en_GB``.
 - Fixed a bug that made it impossible to import the CLDR data
   from scratch on windows systems.
+- Fixed a bug with extracting messages from "hidden" dirs when
+  they where scanned one after another.
 
 Version 1.3
 -----------
index 2f8084af5378083a7161b9bd23c2f1c24fb3085f..84b758b493576990e1f1cc5c591966985422a105 100644 (file)
@@ -21,7 +21,7 @@ import os
 import sys
 from tokenize import generate_tokens, COMMENT, NAME, OP, STRING
 
-from babel.util import parse_encoding, pathmatch, relpath
+from babel.util import filter_dirs, parse_encoding, pathmatch, relpath
 from babel._compat import PY2, text_type
 from textwrap import dedent
 
@@ -135,11 +135,10 @@ 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('_'):
-                dirnames.remove(subdir)
+        dirnames = filter_dirs(dirnames)
         dirnames.sort()
         filenames.sort()
+
         for filename in filenames:
             filename = relpath(
                 os.path.join(root, filename).replace(os.sep, '/'),
index f46ee2b7073e1d403dafcf32f49116d1affcd601..1d1b152e9a9f70718b5c5760f9b0e22b90c01d24 100644 (file)
@@ -38,6 +38,27 @@ def distinct(iterable):
             yield item
             seen.add(item)
 
+
+def filter_dirs(dirnames):
+    """
+    Filters out all unwanted directories from `diranmes`
+
+    Examples:
+
+    >>> filter_dirs(['dir_a', '.dir_b'])
+    ['dir_a', ]
+
+    >>> filter_dirs(['dir_a', 'dir_b', '_dir_c', '.dir_d'])
+    ['dir_a', 'dir_b']
+
+
+    :param dirnames: list of directories
+    """
+    return [
+        subdir for subdir in dirnames
+        if not subdir.startswith('.') and not subdir.startswith('_')
+    ]
+
 # Regexp to match python magic encoding line
 PYTHON_MAGIC_COMMENT_re = re.compile(
     br'[ \t\f]* \# .* coding[=:][ \t]*([-\w.]+)', re.VERBOSE)
index 321014c90bd28465f2984784c58bddecd6878519..2f6179089455a88cca61f62b768456db95049ce1 100644 (file)
@@ -17,6 +17,15 @@ import unittest
 from babel import util
 
 
+def test_filter_dirs():
+    assert util.filter_dirs(['dir_a', ]) == ['dir_a', ]
+    assert util.filter_dirs(['dir_a', '.dir_b']) == ['dir_a', ]
+    assert util.filter_dirs(['.dir_a', '_dir_b', 'dir_c', 'dir_d']) == [
+        'dir_c',
+        'dir_d'
+    ]
+
+
 def test_distinct():
     assert list(util.distinct([1, 2, 1, 3, 4, 4])) == [1, 2, 3, 4]
     assert list(util.distinct('foobar')) == ['f', 'o', 'b', 'a', 'r']