[PATCH 3 of 5] templater: add support for mapping lookups

Dan Villiom Podlaski Christiansen danchr at gmail.com
Tue Sep 21 17:15:17 CDT 2010


# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1285022565 -7200
# Node ID f922fa4c67f82f361c0564c3b916f778a068898a
# Parent  ad0bf1c540e338d1638f3c2a7c17d642acdb161b
templater: add support for mapping lookups.

A lookup is designating using a dot in the template keyword, such as
'{x.y}', where x is the key and y the member. The key *must* be of
type dict. If the member is not found, the empty string is substituted.

Lookups are parsed and handled at the template instantiation stage,
rather than during parsing, as this simplifies the implementation a
good deal, and makes stuff like filtering mappings Magically
Work.

Nested lookups ought to work, but haven't been tested yet.

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -79,11 +79,25 @@ class engine(object):
         return self._cache[t]
 
     def _get(self, mapping, key):
-        v = mapping.get(key)
+        parts = key.split('.')
+        name = parts.pop(0)
+
+        v = mapping.get(name)
         if v is None:
-            v = self._defaults.get(key, '')
+            v = self._defaults.get(name, '')
         if hasattr(v, '__call__'):
             v = v(**mapping)
+
+        # handle dictionary/mapping lookups
+        for part in parts:
+            try:
+                v = v[part]
+            except (TypeError, KeyError):
+                v = ''
+                break
+        if isinstance(v, dict):
+            v = repr(v)
+
         return v
 
     def _filter(self, mapping, parts):


More information about the Mercurial-devel mailing list