changeset 4543:6b4e8a75d5fc

extensions: separate function for loading single extensions
author Matt Mackall <mpm@selenic.com>
date Mon, 11 Jun 2007 21:09:23 -0500
parents af02e6078d08
children 930ed513c864
files mercurial/commands.py
diffstat 1 files changed, 39 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/commands.py	Mon Jun 11 21:09:23 2007 -0500
+++ b/mercurial/commands.py	Mon Jun 11 21:09:23 2007 -0500
@@ -3149,53 +3149,53 @@
                 return sys.modules[v]
         raise KeyError(name)
 
-def load_extensions(ui):
-    added = []
-    for ext_name, load_from_name in ui.extensions():
-        if ext_name in external:
-            continue
+def load_extension(ui, name, load):
+    if name in external:
+        return
+    if load:
+        # the module will be loaded in sys.modules
+        # choose an unique name so that it doesn't
+        # conflicts with other modules
+        module_name = "hgext_%s" % name.replace('.', '_')
+        mod = imp.load_source(module_name, load)
+    else:
+        def importh(name):
+            mod = __import__(name)
+            components = name.split('.')
+            for comp in components[1:]:
+                mod = getattr(mod, comp)
+            return mod
         try:
-            if load_from_name:
-                # the module will be loaded in sys.modules
-                # choose an unique name so that it doesn't
-                # conflicts with other modules
-                module_name = "hgext_%s" % ext_name.replace('.', '_')
-                mod = imp.load_source(module_name, load_from_name)
-            else:
-                def importh(name):
-                    mod = __import__(name)
-                    components = name.split('.')
-                    for comp in components[1:]:
-                        mod = getattr(mod, comp)
-                    return mod
-                try:
-                    mod = importh("hgext.%s" % ext_name)
-                except ImportError:
-                    mod = importh(ext_name)
-            external[ext_name] = mod.__name__
-            added.append((mod, ext_name))
+            mod = importh("hgext.%s" % name)
+        except ImportError:
+            mod = importh(name)
+    external[name] = mod.__name__
+
+    uisetup = getattr(mod, 'uisetup', None)
+    if uisetup:
+        uisetup(ui)
+    reposetup = getattr(mod, 'reposetup', None)
+    if reposetup:
+        hg.repo_setup_hooks.append(reposetup)
+    cmdtable = getattr(mod, 'cmdtable', {})
+    overrides = [cmd for cmd in cmdtable if cmd in table]
+    if overrides:
+        ui.warn(_("extension '%s' overrides commands: %s\n")
+                % (name, " ".join(overrides)))
+    table.update(cmdtable)
+
+def load_extensions(ui):
+    for name, load in ui.extensions():
+        try:
+            load_extension(ui, name, load)
         except (util.SignalInterrupt, KeyboardInterrupt):
             raise
         except Exception, inst:
             ui.warn(_("*** failed to import extension %s: %s\n") %
-                    (ext_name, inst))
+                    (name, inst))
             if ui.print_exc():
                 return 1
 
-    for mod, name in added:
-        uisetup = getattr(mod, 'uisetup', None)
-        if uisetup:
-            uisetup(ui)
-        reposetup = getattr(mod, 'reposetup', None)
-        if reposetup:
-            hg.repo_setup_hooks.append(reposetup)
-        cmdtable = getattr(mod, 'cmdtable', {})
-        overrides = [cmd for cmd in cmdtable if cmd in table]
-        if overrides:
-            ui.warn(_("extension '%s' overrides commands: %s\n")
-                    % (name, " ".join(overrides)))
-        table.update(cmdtable)
-
 def catchterm(*args):
     raise util.SignalInterrupt