[PATCH 1 of 3] commands: a decorator to pass arbitrary data from extensions

Cesar Mena cesar.mena at gmail.com
Fri Oct 8 13:13:51 CDT 2010


# HG changeset patch
# User Cesar Mena <cmena at pobox.com>
# Date 1277845967 14400
# Node ID af8aecd93a80794ff8a0ed670456cddc4a0afcda
# Parent  0ae35296fbf4d486ea4228bfd93c03a844ee9fb8
commands: a decorator to pass arbitrary data from extensions

in order to pass data back to a calling command, extensions must define
a function of the form:

def fn(ui, repo, data)

where ui and repo are the standard command arguments, and data is a
dictionary that can be populated to return information. function
return values are ignored.

such function is then added to a cmdextdatatable dictionary defined in
the extension with the command name as key and callable as value:

ie, cmdextdatatable = {'summary', _summaryoverlay} will call
_summaryoverlay in an extension, as described.

diff --git a/mercurial/commands.py b/mercurial/commands.py
--- a/mercurial/commands.py
+++ b/mercurial/commands.py
@@ -3445,6 +3445,21 @@
                     ui.write('  %s%s' % (repo.pathto(copy[f], cwd), end),
                              label='status.copied')
 
+def cmdextdata(cmd):
+    def _cmdextdata(ui, repo, *args, **opts):
+        d = {} # extdata data
+        for ename, ext in extensions.extensions():
+            t = getattr(ext, 'cmdextdatatable', None)
+            if t:
+                fn = t.get(cmd.__name__, None)
+                if fn:
+                    fn(ui, repo, d)
+        return cmd(ui, repo, *args, extdata=d, **opts)
+
+    _cmdextdata.__doc__ = getattr(cmd, '__doc__')
+
+    return _cmdextdata
+
 def summary(ui, repo, **opts):
     """summarize working directory state
 


More information about the Mercurial-devel mailing list