--- a/compare_locales/paths.py
+++ b/compare_locales/paths.py
@@ -263,17 +263,17 @@ class ProjectConfig(object):
key = key[3:]
else:
key = re.escape(key) + '$'
rule['key'] = re.compile(key)
yield rule
class ProjectFiles(object):
- '''Iterator object to get all files and tests for a locale and a
+ '''Iterable object to get all files and tests for a locale and a
list of ProjectConfigs.
'''
def __init__(self, locale, projects, mergebase=None):
self.locale = locale
self.matchers = []
self.mergebase = mergebase
configs = []
for project in projects:
@@ -370,16 +370,41 @@ class ProjectFiles(object):
yield base
return
for d, dirs, files in os.walk(base):
for f in files:
p = mozpath.join(d, f)
if matcher.match(p):
yield p
+ def match(self, path):
+ '''Return the tuple of l10n_path, reference, mergepath, tests
+ if the given path matches any config, otherwise None.
+
+ This routine doesn't check that the files actually exist.
+ '''
+ for matchers in self.matchers:
+ matcher = matchers['l10n']
+ if matcher.match(path):
+ ref = merge = None
+ if 'reference' in matchers:
+ ref = matcher.sub(matchers['reference'], path)
+ if 'merge' in matchers:
+ merge = matcher.sub(matchers['merge'], path)
+ return path, ref, merge, matchers.get('test')
+ if 'reference' not in matchers:
+ continue
+ matcher = matchers['reference']
+ if matcher.match(path):
+ merge = None
+ l10n = matcher.sub(matchers['l10n'], path)
+ if 'merge' in matchers:
+ merge = matcher.sub(matchers['merge'], path)
+ return l10n, path, merge, matchers.get('test')
+
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
@@ -317,20 +317,27 @@ class TestProjectPaths(unittest.TestCase
'/tmp/fr/': [
'good.ftl',
'not/subdir/bad.ftl'
],
}
files = MockProjectFiles(mocks, 'de', [cfg])
self.assertListEqual(
list(files), [('/tmp/de/good.ftl', None, None, set())])
+ self.assertTupleEqual(
+ files.match('/tmp/de/something.ftl'),
+ ('/tmp/de/something.ftl', None, None, set()))
+ self.assertIsNone(files.match('/tmp/fr/something.ftl'))
files = MockProjectFiles(mocks, 'de', [cfg], mergebase='merging')
self.assertListEqual(
list(files),
[('/tmp/de/good.ftl', None, 'merging/de/good.ftl', set())])
+ self.assertTupleEqual(
+ files.match('/tmp/de/something.ftl'),
+ ('/tmp/de/something.ftl', None, 'merging/de/something.ftl', set()))
# 'fr' is not in the locale list, should return no files
files = MockProjectFiles(mocks, 'fr', [cfg])
self.assertListEqual(list(files), [])
def test_reference_path(self):
cfg = ProjectConfig()
cfg.locales.append('de')
cfg.add_paths({
@@ -356,25 +363,47 @@ class TestProjectPaths(unittest.TestCase
self.assertListEqual(
list(files),
[
('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl', None,
set()),
('/tmp/l10n/de/ref.ftl', '/tmp/reference/ref.ftl', None,
set()),
])
+ self.assertTupleEqual(
+ files.match('/tmp/l10n/de/good.ftl'),
+ ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl', None,
+ set()),
+ )
+ self.assertTupleEqual(
+ files.match('/tmp/reference/good.ftl'),
+ ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl', None,
+ set()),
+ )
+ self.assertIsNone(files.match('/tmp/l10n/de/subdir/bad.ftl'))
+ self.assertIsNone(files.match('/tmp/reference/subdir/bad.ftl'))
files = MockProjectFiles(mocks, 'de', [cfg], mergebase='merging')
self.assertListEqual(
list(files),
[
('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl',
'merging/de/good.ftl', set()),
('/tmp/l10n/de/ref.ftl', '/tmp/reference/ref.ftl',
'merging/de/ref.ftl', set()),
])
+ self.assertTupleEqual(
+ files.match('/tmp/l10n/de/good.ftl'),
+ ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl',
+ 'merging/de/good.ftl', set()),
+ )
+ self.assertTupleEqual(
+ files.match('/tmp/reference/good.ftl'),
+ ('/tmp/l10n/de/good.ftl', '/tmp/reference/good.ftl',
+ 'merging/de/good.ftl', set()),
+ )
# 'fr' is not in the locale list, should return no files
files = MockProjectFiles(mocks, 'fr', [cfg])
self.assertListEqual(list(files), [])
def test_partial_l10n(self):
cfg = ProjectConfig()
cfg.locales.extend(['de', 'fr'])
cfg.add_paths({
@@ -401,23 +430,28 @@ class TestProjectPaths(unittest.TestCase
}
files = MockProjectFiles(mocks, 'de', [cfg])
self.assertListEqual(
list(files),
[
('/tmp/de/major/good.ftl', None, None, set()),
('/tmp/de/minor/good.ftl', None, None, set()),
])
+ self.assertTupleEqual(
+ files.match('/tmp/de/major/some.ftl'),
+ ('/tmp/de/major/some.ftl', None, None, set()))
+ self.assertIsNone(files.match('/tmp/de/other/some.ftl'))
# 'fr' is not in the locale list of minor, should only return major
files = MockProjectFiles(mocks, 'fr', [cfg])
self.assertListEqual(
list(files),
[
('/tmp/fr/major/good.ftl', None, None, set()),
])
+ self.assertIsNone(files.match('/tmp/fr/minor/some.ftl'))
class TestProjectConfig(unittest.TestCase):
def test_expand_paths(self):
pc = ProjectConfig()
pc.add_environment(one="first_path")
self.assertEqual(pc.expand('foo'), 'foo')
self.assertEqual(pc.expand('foo{one}bar'), 'foofirst_pathbar')