extensions: add wrapping functions
authorMatt Mackall <mpm@selenic.com>
Wed, 22 Oct 2008 17:34:50 -0500
changeset 7215 0ab5f21c390b
parent 7214 0e8a9530d323
child 7216 292fb2ad2846
extensions: add wrapping functions
mercurial/extensions.py
--- a/mercurial/extensions.py	Wed Oct 22 17:34:37 2008 -0500
+++ b/mercurial/extensions.py	Wed Oct 22 17:34:50 2008 -0500
@@ -6,7 +6,7 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 import imp, os
-import util
+import util, cmdutil
 from i18n import _
 
 _extensions = {}
@@ -87,3 +87,28 @@
             if ui.print_exc():
                 return 1
 
+def wrapcommand(table, command, wrapper):
+    aliases, entry = cmdutil.findcmd(command, table)
+    for alias, e in table.iteritems():
+        if e is entry:
+            key = alias
+            break
+
+    origfn = entry[0]
+    def wrap(*args, **kwargs):
+        return wrapper(origfn, *args, **kwargs)
+
+    wrap.__doc__ = getattr(origfn, '__doc__')
+
+    newentry = list(entry)
+    newentry[0] = wrap
+    table[key] = tuple(newentry)
+    return entry
+
+def wrapfunction(container, funcname, wrapper):
+    def wrap(*args, **kwargs):
+        return wrapper(origfn, *args, **kwargs)
+
+    origfn = getattr(container, funcname)
+    setattr(container, funcname, wrap)
+    return origfn