bug 1478613, extract path variable expansion into Matcher, r?stas draft
authorAxel Hecht <axel@pike.org>
Tue, 15 May 2018 12:10:41 +0200
changeset 635 2c0059ceac6d62228f76d2eae884348c868df29c
parent 634 281e2c99449b4dca9e56365c73ba6134e250e8a2
child 636 5cce4152d97ec19fd4960eca5a7fc81f67d66a00
push id197
push useraxel@mozilla.com
push dateThu, 26 Jul 2018 16:18:26 +0000
reviewersstas
bugs1478613
bug 1478613, extract path variable expansion into Matcher, r?stas MozReview-Commit-ID: 6Xg8GKiKpUW
compare_locales/paths.py
--- a/compare_locales/paths.py
+++ b/compare_locales/paths.py
@@ -73,16 +73,28 @@ class Matcher(object):
         '''
         Replace the wildcard matches in this pattern into the
         pattern of the other Match object.
         '''
         if not self.match(path):
             return None
         return self.regex.sub(other.placable, path)
 
+    variable = re.compile('{ *([\w]+) *}')
+
+    @staticmethod
+    def expand(pattern, *envs):
+        def _expand(m):
+            _var = m.group(1)
+            for env in envs:
+                if _var in env:
+                    return Matcher.expand(env[_var], *envs)
+            return '{{{}}}'.format(_var)
+        return Matcher.variable.sub(_expand, pattern)
+
 
 class ProjectConfig(object):
     '''Abstraction of l10n project configuration data.
     '''
 
     def __init__(self):
         self.filter_py = None  # legacy filter code
         # {
@@ -93,29 +105,21 @@ class ProjectConfig(object):
         # }
         self.paths = []
         self.rules = []
         self.locales = []
         self.environ = {}
         self.children = []
         self._cache = None
 
-    variable = re.compile('{ *([\w]+) *}')
-
     def expand(self, path, env=None):
-        if env is None:
-            env = {}
-
-        def _expand(m):
-            _var = m.group(1)
-            for _env in (env, self.environ):
-                if _var in _env:
-                    return self.expand(_env[_var], env)
-            return '{{{}}}'.format(_var)
-        return self.variable.sub(_expand, path)
+        envs = [self.environ]
+        if env:
+            envs.insert(0, env)
+        return Matcher.expand(path, *envs)
 
     def lazy_expand(self, pattern):
         def lazy_l10n_expanded_pattern(env):
             return Matcher(self.expand(pattern, env))
         return lazy_l10n_expanded_pattern
 
     def add_global_environment(self, **kwargs):
         self.add_environment(**kwargs)