diff options
author | Stéphane Wirtel <stephane.wirtel@gmail.com> | 2014-10-13 22:50:04 +0100 |
---|---|---|
committer | Stéphane Wirtel <stephane.wirtel@gmail.com> | 2014-10-13 22:50:04 +0100 |
commit | 781e771c8e63c37910ef09e051238a3c5b42ffd5 (patch) | |
tree | 8802c3a171138e777c143a2815971ebd9b60308a | |
parent | 11593cd65c8e0eb3b5bef93d488e81cc44373aa2 (diff) | |
parent | ef2a63c1b66206a0ad9a58b11bbac172465f3fff (diff) | |
download | babel-1.x-maintenance.tar.gz |
Merge pull request #86 from prmtl/1.x-maintenance-fixed-hidden-dirs1.x-maintenance
fix for filtering unwanted directories
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | babel/messages/extract.py | 7 | ||||
-rw-r--r-- | babel/util.py | 21 | ||||
-rw-r--r-- | tests/test_util.py | 9 |
4 files changed, 35 insertions, 4 deletions
@@ -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 ----------- diff --git a/babel/messages/extract.py b/babel/messages/extract.py index 2f8084a..22c44cb 100644 --- a/babel/messages/extract.py +++ b/babel/messages/extract.py @@ -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, '/'), diff --git a/babel/util.py b/babel/util.py index f46ee2b..1d1b152 100644 --- a/babel/util.py +++ b/babel/util.py @@ -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) diff --git a/tests/test_util.py b/tests/test_util.py index 321014c..2f61790 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -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'] |