Add support for extension modules
authormason@suse.com
Fri, 26 Aug 2005 14:05:52 -0700
changeset 1071 8f0ac653f85e
parent 1070 b55c6685c953
child 1072 05dc7aba22eb
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: hg/mercurial/ui.py ===================================================================
mercurial/commands.py
mercurial/ui.py
--- a/mercurial/commands.py	Fri Aug 26 13:59:14 2005 -0700
+++ b/mercurial/commands.py	Fri Aug 26 14:05:52 2005 -0700
@@ -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 sets")
@@ -1729,10 +1729,29 @@
     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])
@@ -1741,7 +1760,6 @@
             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)
@@ -1755,12 +1773,11 @@
         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:
@@ -1785,6 +1802,8 @@
             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)
--- a/mercurial/ui.py	Fri Aug 26 13:59:14 2005 -0700
+++ b/mercurial/ui.py	Fri Aug 26 14:05:52 2005 -0700
@@ -22,6 +22,10 @@
         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)
@@ -63,6 +67,9 @@
                 yield section, name, value.replace('\n', '\\n')
                 seen[section, name] = 1
 
+    def extensions(self):
+        return self.configitems("extensions")
+
     def username(self):
         return (os.environ.get("HGUSER") or
                 self.config("ui", "username") or