# HG changeset patch # User Yuya Nishihara # Date 1459693110 -32400 # Node ID 7f6b8ec691e3f8c662fbf93b0441522e2b3d7e2d # Parent f59e2400267827e89e6736c4f6204c9869b63ee4 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. diff -r f59e24002678 -r 7f6b8ec691e3 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