Lazy loading of command
authorFred <fmcell@gmail.com>
Mon, 4 Mar 2013 05:05:31 +0000 (13:05 +0800)
committerFred <fmcell@gmail.com>
Mon, 4 Mar 2013 05:05:31 +0000 (13:05 +0800)
lib/commands/__init__.py
mdk.py

index e5e2c5d..97cd048 100644 (file)
@@ -22,22 +22,24 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 http://github.com/FMCorz/mdk
 """
 
-from alias import AliasCommand
-from backport import BackportCommand
-from backup import BackupCommand
-from behat import BehatCommand
-from config import ConfigCommand
-from create import CreateCommand
-from fix import FixCommand
-from info import InfoCommand
-from init import InitCommand
-from install import InstallCommand
-from phpunit import PhpunitCommand
-from pull import PullCommand
-from purge import PurgeCommand
-from push import PushCommand
-from rebase import RebaseCommand
-from remove import RemoveCommand
-from run import RunCommand
-from update import UpdateCommand
-from upgrade import UpgradeCommand
+commandsList = [
+    'alias',
+    'backport',
+    'backup',
+    'behat',
+    'config',
+    'create',
+    'fix',
+    'info',
+    'init',
+    'install',
+    'phpunit',
+    'pull',
+    'purge',
+    'push',
+    'rebase',
+    'remove',
+    'run',
+    'update',
+    'upgrade'
+]
diff --git a/mdk.py b/mdk.py
index 0a2f54f..4e17c58 100755 (executable)
--- a/mdk.py
+++ b/mdk.py
@@ -27,16 +27,21 @@ import argparse
 import os
 import re
 from lib.command import CommandRunner
-from lib.commands import *
+from lib.commands import commandsList
 from lib.config import Conf
 from lib.tools import process
 from version import __version__
 
 C = Conf()
 
-availcmds = [x.replace('Command', '').lower() for x in globals().keys() if x.endswith('Command')]
+
+def getCommand(cmd):
+    """Lazy loading of a command class. Millseconds saved, hurray!"""
+    cls = cmd.capitalize() + 'Command'
+    return getattr(getattr(getattr(__import__('lib.%s.%s' % ('commands', cmd)), 'commands'), cmd), cls)
+
 availaliases = [str(x) for x in C.get('aliases').keys()]
-choices = sorted(availcmds + availaliases)
+choices = sorted(commandsList + availaliases)
 
 parser = argparse.ArgumentParser(description='Moodle Development Kit', add_help=False)
 parser.add_argument('-h', '--help', action='store_true', help='show this help message and exit')
@@ -54,8 +59,8 @@ if not cmd:
     if parsedargs.version:
         print 'MDK version %s' % __version__
     elif parsedargs.list:
-        for c in sorted(availcmds):
-            print '  %s' % c
+        for c in sorted(commandsList):
+            print '{0:<15} {1}'.format(c, getCommand(c)._description)
     else:
         parser.print_help()
     sys.exit(0)
@@ -78,9 +83,7 @@ if alias != None:
         cmd = alias.split(' ')[0]
         args = alias.split(' ')[1:] + args
 
-# Calling the command
-classname = '%sCommand' % (cmd.capitalize())
-cls = globals().get(classname)
+cls = getCommand(cmd)
 Cmd = cls(C)
 Runner = CommandRunner(Cmd)
 Runner.run(args, prog='%s %s' % (os.path.basename(sys.argv[0]), cmd))