[PATCH 03 of 10 RFC] templatefilter: add labelify and delabelify functions

Sean Farley sean.michael.farley at gmail.com
Thu Dec 20 23:37:53 CST 2012


# HG changeset patch
# User Sean Farley <sean.michael.farley at gmail.com>
# Date 1355961231 21600
# Node ID 19bea005cd095a81941820efcf5b924cc354d293
# Parent  812607049cddf4653985ed96ecf9ef9c8c7dab78
templatefilter: add labelify and delabelify functions

There are many directions that we could go here and I need some guidance as to which one to follow. The route taken in this patch series is to wrap the value in a dictionary (e.g. {data: "foo", label: "log.user"}). The repercussions of this are that _flatten, _showlist, and a few other pieces of code must be taught to not clobber the labeled data.

The upside to this model is that it's relatively simple to wrap data with a label. Alternatives could be:

- Using recursive templates similar to how {files} expand. This would, I think, be very fragile because later filters could potentially truncate the label data (e.g. {'node|short'})

- Using a custom class?

- Using something else more fancy?

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -420,5 +420,33 @@
     "date": datefunc,
 }
 
 # tell hggettext to extract docstrings from these functions:
 i18nfunctions = filters.values()
+
+# should this be a class?
+def _islabel(thing):
+    return isinstance(thing, dict) and 'data' in thing and 'label' in thing
+
+def labelify(thing, label=''):
+    if thing is None:
+        return None
+
+    if label is None:
+        label = ''
+
+    if _islabel(thing):
+        if thing['label'] not in label:
+            label += ' ' + thing['label']
+        thing = thing['data']
+
+    label = label.strip()
+
+    return {'data':thing, 'label':label}
+
+def delabelify(thing, label=''):
+    thing = labelify(thing, label)
+    if _islabel(thing):
+        label = thing['label']
+        thing = thing['data']
+
+    return thing, label


More information about the Mercurial-devel mailing list