diff mercurial/templater.py @ 28954:f97a0bcfd7a1

templater: separate function to create templater from map file (API) New frommapfile() function will make it clear when template aliases will be loaded. They should be applied to command arguments and templates in hgrc, but not to map files. Otherwise, our stock styles and web templates (i.e map-file templates) could be modified unintentionally. Future patches will add "aliases" argument to __init__(), but not to frommapfile().
author Yuya Nishihara <yuya@tcha.org>
date Sun, 03 Apr 2016 23:26:48 +0900
parents 7f6b8ec691e3
children eea98190ed73
line wrap: on
line diff
--- a/mercurial/templater.py	Sun Apr 03 23:18:30 2016 +0900
+++ b/mercurial/templater.py	Sun Apr 03 23:26:48 2016 +0900
@@ -1016,10 +1016,9 @@
 
 class templater(object):
 
-    def __init__(self, mapfile, filters=None, defaults=None, cache=None,
+    def __init__(self, filters=None, defaults=None, cache=None,
                  minchunk=1024, maxchunk=65536):
         '''set up template engine.
-        mapfile is name of file to read map definitions from.
         filters is dict of functions. each transforms a value into another.
         defaults is dict of default map definitions.'''
         if filters is None:
@@ -1036,11 +1035,15 @@
         self.minchunk, self.maxchunk = minchunk, maxchunk
         self.ecache = {}
 
-        if not mapfile:
-            return
+    @classmethod
+    def frommapfile(cls, mapfile, filters=None, defaults=None, cache=None,
+                    minchunk=1024, maxchunk=65536):
+        """Create templater from the specified map file"""
+        t = cls(filters, defaults, cache, minchunk, maxchunk)
         cache, tmap = _readmapfile(mapfile)
-        self.cache.update(cache)
-        self.map = tmap
+        t.cache.update(cache)
+        t.map = tmap
+        return t
 
     def __contains__(self, key):
         return key in self.cache or key in self.map