templater: extract function that loads template map file
authorYuya Nishihara <yuya@tcha.org>
Sun, 03 Apr 2016 23:18:30 +0900
changeset 28953 7f6b8ec691e3
parent 28952 f59e24002678
child 28954 f97a0bcfd7a1
templater: extract function that loads template map file Prepares for API change. See the next patch for details. 'map' variable is renamed to avoid shadowing map() function.
mercurial/templater.py
--- a/mercurial/templater.py	Sun Apr 03 23:22:43 2016 +0900
+++ b/mercurial/templater.py	Sun Apr 03 23:18:30 2016 +0900
@@ -984,6 +984,33 @@
             stylelist.append(split[1])
     return ", ".join(sorted(stylelist))
 
+def _readmapfile(mapfile):
+    """Load template elements from the given map file"""
+    if not os.path.exists(mapfile):
+        raise error.Abort(_("style '%s' not found") % mapfile,
+                          hint=_("available styles: %s") % stylelist())
+
+    base = os.path.dirname(mapfile)
+    conf = config.config(includepaths=templatepaths())
+    conf.read(mapfile)
+
+    cache = {}
+    tmap = {}
+    for key, val in conf[''].items():
+        if not val:
+            raise error.ParseError(_('missing value'), conf.source('', key))
+        if val[0] in "'\"":
+            if val[0] != val[-1]:
+                raise error.ParseError(_('unmatched quotes'),
+                                       conf.source('', key))
+            cache[key] = unquotestring(val)
+        else:
+            val = 'default', val
+            if ':' in val[1]:
+                val = val[1].split(':', 1)
+            tmap[key] = val[0], os.path.join(base, val[1])
+    return cache, tmap
+
 class TemplateNotFound(error.Abort):
     pass
 
@@ -1011,27 +1038,9 @@
 
         if not mapfile:
             return
-        if not os.path.exists(mapfile):
-            raise error.Abort(_("style '%s' not found") % mapfile,
-                             hint=_("available styles: %s") % stylelist())
-
-        base = os.path.dirname(mapfile)
-        conf = config.config(includepaths=templatepaths())
-        conf.read(mapfile)
-
-        for key, val in conf[''].items():
-            if not val:
-                raise error.ParseError(_('missing value'), conf.source('', key))
-            if val[0] in "'\"":
-                if val[0] != val[-1]:
-                    raise error.ParseError(_('unmatched quotes'),
-                                           conf.source('', key))
-                self.cache[key] = unquotestring(val)
-            else:
-                val = 'default', val
-                if ':' in val[1]:
-                    val = val[1].split(':', 1)
-                self.map[key] = val[0], os.path.join(base, val[1])
+        cache, tmap = _readmapfile(mapfile)
+        self.cache.update(cache)
+        self.map = tmap
 
     def __contains__(self, key):
         return key in self.cache or key in self.map