[patch 1/7] Add support for extension modules

Chris Mason mason at suse.com
Mon Aug 22 12:44:35 CDT 2005


# HG changeset patch
# User mason at suse.com
Add support for extension modules

This adds support for an [extensions] section to hgrc.  This has the form of:

[extensions]
mod=[path]

If a path is specified, the python module found at that path is load.
Otherwise, __import__ is used to find the module.

Each module must implement a dict called cmdtable where the command line
options for that module live.  Each module must also implement a reposetup
function:

cmdtable = {}
def reposetup(ui, repo): pass

Index: mine/mercurial/ui.py
===================================================================
--- mine.orig/mercurial/ui.py	2005-08-22 08:20:41.000000000 -0400
+++ mine/mercurial/ui.py	2005-08-22 09:53:35.000000000 -0400
@@ -22,6 +22,10 @@ class ui:
         self.debugflag = self.configbool("ui", "debug")
         self.interactive = self.configbool("ui", "interactive", True)
 
+        self.updateopts(verbose, debug, quiet, interactive)
+
+    def updateopts(self, verbose=False, debug=False, quiet=False,
+                 interactive=True):
         self.quiet = (self.quiet or quiet) and not verbose and not debug
         self.verbose = (self.verbose or verbose) or debug
         self.debugflag = (self.debugflag or debug)
@@ -52,6 +56,9 @@ class ui:
             return self.cdata.items(section)
         return []
 
+    def extensions(self):
+        return self.configitems("extensions")
+
     def username(self):
         return (os.environ.get("HGUSER") or
                 self.config("ui", "username") or
Index: mine/mercurial/commands.py
===================================================================
--- mine.orig/mercurial/commands.py	2005-08-22 08:20:41.000000000 -0400
+++ mine/mercurial/commands.py	2005-08-22 09:53:37.000000000 -0400
@@ -6,7 +6,7 @@
 # of the GNU General Public License, incorporated herein by reference.
 
 from demandload import demandload
-demandload(globals(), "os re sys signal shutil")
+demandload(globals(), "os re sys signal shutil imp")
 demandload(globals(), "fancyopts ui hg util lock")
 demandload(globals(), "fnmatch hgweb mdiff random signal time traceback")
 demandload(globals(), "errno socket version struct atexit")
@@ -1510,10 +1510,29 @@ def dispatch(args):
     except AttributeError:
         pass
 
+    u = ui.ui()
+    external = []
+    for x in u.extensions():
+        if x[1]:
+            mod = imp.load_source(x[0], x[1])
+        else:
+            def importh(name):
+                mod = __import__(name)
+                components = name.split('.')
+                for comp in components[1:]:
+                    mod = getattr(mod, comp)
+                return mod
+            mod = importh(x[0])
+        external.append(mod)
+    for x in external:
+        for t in x.cmdtable:
+            if t in table:
+                u.warn("module %s override %s\n" % (x.__name__, t))
+        table.update(x.cmdtable)
+
     try:
         cmd, func, args, options, cmdoptions = parse(args)
     except ParseError, inst:
-        u = ui.ui()
         if inst.args[0]:
             u.warn("hg %s: %s\n" % (inst.args[0], inst.args[1]))
             help_(u, inst.args[0])
@@ -1522,7 +1541,6 @@ def dispatch(args):
             help_(u, 'shortlist')
         sys.exit(-1)
     except UnknownCommand, inst:
-        u = ui.ui()
         u.warn("hg: unknown command '%s'\n" % inst.args[0])
         help_(u, 'shortlist')
         sys.exit(1)
@@ -1544,12 +1562,11 @@ def dispatch(args):
         s = get_times()
         def print_time():
             t = get_times()
-            u = ui.ui()
             u.warn("Time: real %.3f secs (user %.3f+%.3f sys %.3f+%.3f)\n" %
                 (t[4]-s[4], t[0]-s[0], t[2]-s[2], t[1]-s[1], t[3]-s[3]))
         atexit.register(print_time)
 
-    u = ui.ui(options["verbose"], options["debug"], options["quiet"],
+    u.updateopts(options["verbose"], options["debug"], options["quiet"],
               not options["noninteractive"])
 
     try:
@@ -1557,6 +1574,8 @@ def dispatch(args):
             if cmd not in norepo.split():
                 path = options["repository"] or ""
                 repo = hg.repository(ui=u, path=path)
+                for x in external: 
+                    x.reposetup(u, repo)
                 d = lambda: func(u, repo, *args, **cmdoptions)
             else:
                 d = lambda: func(u, *args, **cmdoptions)

--


More information about the Mercurial mailing list