Patchwork [4,of,9] templater: extract function that loads template map file

login
register
mail settings
Submitter Yuya Nishihara
Date April 15, 2016, 1:15 p.m.
Message ID <a67a064b7ec0f57e10ce.1460726143@mimosa>
Download mbox | patch
Permalink /patch/14643/
State Accepted
Headers show

Comments

Yuya Nishihara - April 15, 2016, 1:15 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1459693110 -32400
#      Sun Apr 03 23:18:30 2016 +0900
# Node ID a67a064b7ec0f57e10ce2a119d656adffadc04c1
# Parent  00ae86afdecc3366a3dd5a5dfcd31255c7016873
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.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -984,6 +984,33 @@  def stylelist():
             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 @@  class templater(object):
 
         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