changeset 8474:bccc90b56d02

templater: clean up the process method, separating code paths
author Dirkjan Ochtman <dirkjan@ochtman.nl>
date Sun, 17 May 2009 16:05:50 +0200
parents 69f51fd9fb48
children caaf1e779b37
files mercurial/templater.py
diffstat 1 files changed, 30 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/templater.py	Sun May 17 09:54:08 2009 +0200
+++ b/mercurial/templater.py	Sun May 17 16:05:50 2009 +0200
@@ -70,8 +70,32 @@
             else:
                 yield str(item)
 
+    def _format(self, key, format, get, map):
+        v = get(key)
+        if not hasattr(v, '__iter__'):
+            raise SyntaxError(_("Error expanding '%s%%%s'") % (key, format))
+        lm = map.copy()
+        for i in v:
+            lm.update(i)
+            yield self.process(format, lm)
+
+    def _filter(self, key, filters, get, map):
+        v = get(key)
+        for f in filters.split('|')[1:]:
+            v = self.filters[f](v)
+        return v
+
     def _process(self, tmpl, map):
         '''Render a template. Returns a generator.'''
+
+        def get(key):
+            v = map.get(key)
+            if v is None:
+                v = self.defaults.get(key, '')
+            if hasattr(v, '__call__'):
+                v = v(**map)
+            return v
+
         while tmpl:
             m = self.template_re.search(tmpl)
             if not m:
@@ -79,31 +103,18 @@
                 break
 
             start, end = m.span(0)
-            key, format, fl = m.groups()
+            key, fmt, fl = m.groups()
 
             if start:
                 yield tmpl[:start]
             tmpl = tmpl[end:]
 
-            if key in map:
-                v = map[key]
+            if fmt:
+                yield self._format(key, fmt, get, map)
+            elif fl:
+                yield self._filter(key, fl, get, map)
             else:
-                v = self.defaults.get(key, "")
-            if hasattr(v, '__call__'):
-                v = v(**map)
-            if format:
-                if not hasattr(v, '__iter__'):
-                    raise SyntaxError(_("Error expanding '%s%%%s'")
-                                      % (key, format))
-                lm = map.copy()
-                for i in v:
-                    lm.update(i)
-                    yield self.process(format, lm)
-            else:
-                if fl:
-                    for f in fl.split("|")[1:]:
-                        v = self.filters[f](v)
-                yield v
+                yield get(key)
 
 engines = {'default': engine}