bug 1392232, refactor ProjectFiles._filter from path to Matcher, r=flod
When subclassing _filter, you may need all the information in the
Matcher object, not just the base path.
Thus, pass in the matcher instead of matcher.prefix, and move the
.match() piece into ._filter, too.
MozReview-Commit-ID: C1xxcp9ZVFh
--- a/compare_locales/paths.py
+++ b/compare_locales/paths.py
@@ -328,55 +328,57 @@ class ProjectFiles(object):
drops = sorted(drops, reverse=True)
for i in drops:
del self.matchers[i]
def __iter__(self):
known = {}
for matchers in self.matchers:
matcher = matchers['l10n']
- for path in self._files(matcher.prefix):
- if matcher.match(path) and path not in known:
+ for path in self._files(matcher):
+ if path not in known:
known[path] = {'test': matchers.get('test')}
if 'reference' in matchers:
known[path]['reference'] = matcher.sub(
matchers['reference'], path)
if 'merge' in matchers:
known[path]['merge'] = matcher.sub(
matchers['merge'], path)
if 'reference' not in matchers:
continue
matcher = matchers['reference']
- for path in self._files(matcher.prefix):
- if not matcher.match(path):
- continue
+ for path in self._files(matcher):
l10npath = matcher.sub(matchers['l10n'], path)
if l10npath not in known:
known[l10npath] = {
'reference': path,
'test': matchers.get('test')
}
if 'merge' in matchers:
known[l10npath]['merge'] = \
matcher.sub(matchers['merge'], path)
for path, d in sorted(known.items()):
yield (path, d.get('reference'), d.get('merge'), d['test'])
- def _files(self, base):
+ def _files(self, matcher):
'''Base implementation of getting all files in a hierarchy
using the file system.
Subclasses might replace this method to support different IO
patterns.
'''
+ base = matcher.prefix
if os.path.isfile(base):
- yield base
+ if matcher.match(base):
+ yield base
return
for d, dirs, files in os.walk(base):
for f in files:
- yield mozpath.join(d, f)
+ p = mozpath.join(d, f)
+ if matcher.match(p):
+ yield p
class ConfigNotFound(EnvironmentError):
def __init__(self, path):
super(ConfigNotFound, self).__init__(
errno.ENOENT,
'Configuration file not found',
path)
--- a/compare_locales/tests/test_paths.py
+++ b/compare_locales/tests/test_paths.py
@@ -288,19 +288,22 @@ class TestConfigRules(SetupMixin, unitte
class MockProjectFiles(ProjectFiles):
def __init__(self, mocks, locale, projects, mergebase=None):
(super(MockProjectFiles, self)
.__init__(locale, projects, mergebase=mergebase))
self.mocks = mocks
- def _files(self, base):
+ def _files(self, matcher):
+ base = matcher.prefix
for path in self.mocks.get(base, []):
- yield mozpath.join(base, path)
+ p = mozpath.join(base, path)
+ if matcher.match(p):
+ yield p
class TestProjectPaths(unittest.TestCase):
def test_l10n_path(self):
cfg = ProjectConfig()
cfg.locales.append('de')
cfg.add_paths({
'l10n': '{l10n_base}/{locale}/*'