comparison mercurial/templater.py @ 28953:7f6b8ec691e3

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.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 03 Apr 2016 23:18:30 +0900
parents f59e24002678
children f97a0bcfd7a1
comparison
equal deleted inserted replaced
28952:f59e24002678 28953:7f6b8ec691e3
982 continue 982 continue
983 if split[0] == "map-cmdline": 983 if split[0] == "map-cmdline":
984 stylelist.append(split[1]) 984 stylelist.append(split[1])
985 return ", ".join(sorted(stylelist)) 985 return ", ".join(sorted(stylelist))
986 986
987 def _readmapfile(mapfile):
988 """Load template elements from the given map file"""
989 if not os.path.exists(mapfile):
990 raise error.Abort(_("style '%s' not found") % mapfile,
991 hint=_("available styles: %s") % stylelist())
992
993 base = os.path.dirname(mapfile)
994 conf = config.config(includepaths=templatepaths())
995 conf.read(mapfile)
996
997 cache = {}
998 tmap = {}
999 for key, val in conf[''].items():
1000 if not val:
1001 raise error.ParseError(_('missing value'), conf.source('', key))
1002 if val[0] in "'\"":
1003 if val[0] != val[-1]:
1004 raise error.ParseError(_('unmatched quotes'),
1005 conf.source('', key))
1006 cache[key] = unquotestring(val)
1007 else:
1008 val = 'default', val
1009 if ':' in val[1]:
1010 val = val[1].split(':', 1)
1011 tmap[key] = val[0], os.path.join(base, val[1])
1012 return cache, tmap
1013
987 class TemplateNotFound(error.Abort): 1014 class TemplateNotFound(error.Abort):
988 pass 1015 pass
989 1016
990 class templater(object): 1017 class templater(object):
991 1018
1009 self.minchunk, self.maxchunk = minchunk, maxchunk 1036 self.minchunk, self.maxchunk = minchunk, maxchunk
1010 self.ecache = {} 1037 self.ecache = {}
1011 1038
1012 if not mapfile: 1039 if not mapfile:
1013 return 1040 return
1014 if not os.path.exists(mapfile): 1041 cache, tmap = _readmapfile(mapfile)
1015 raise error.Abort(_("style '%s' not found") % mapfile, 1042 self.cache.update(cache)
1016 hint=_("available styles: %s") % stylelist()) 1043 self.map = tmap
1017
1018 base = os.path.dirname(mapfile)
1019 conf = config.config(includepaths=templatepaths())
1020 conf.read(mapfile)
1021
1022 for key, val in conf[''].items():
1023 if not val:
1024 raise error.ParseError(_('missing value'), conf.source('', key))
1025 if val[0] in "'\"":
1026 if val[0] != val[-1]:
1027 raise error.ParseError(_('unmatched quotes'),
1028 conf.source('', key))
1029 self.cache[key] = unquotestring(val)
1030 else:
1031 val = 'default', val
1032 if ':' in val[1]:
1033 val = val[1].split(':', 1)
1034 self.map[key] = val[0], os.path.join(base, val[1])
1035 1044
1036 def __contains__(self, key): 1045 def __contains__(self, key):
1037 return key in self.cache or key in self.map 1046 return key in self.cache or key in self.map
1038 1047
1039 def load(self, t): 1048 def load(self, t):