Wrapping main logic into a main function
authorFrederic Massart <fred@moodle.com>
Tue, 22 Jul 2014 01:51:06 +0000 (09:51 +0800)
committerFrederic Massart <fred@moodle.com>
Tue, 22 Jul 2014 02:21:48 +0000 (10:21 +0800)
mdk.py
mdk/__main__.py

diff --git a/mdk.py b/mdk.py
index fb3942e..c00bdc5 100755 (executable)
--- a/mdk.py
+++ b/mdk.py
@@ -36,4 +36,4 @@ executable installed with the package is recommended.
 """
 
 import runpy
-runpy.run_module('mdk')
+a = runpy.run_module('mdk', None, '__main__')
index bcd5471..f57feaf 100644 (file)
@@ -22,79 +22,84 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 http://github.com/FMCorz/mdk
 """
 
-import sys
-import argparse
-import os
-import re
-import logging
-from .command import CommandRunner
-from .commands import getCommand, commandsList
-from .config import Conf
-from .tools import process
-from .version import __version__
-
-C = Conf()
-
-try:
-    debuglevel = getattr(logging, C.get('debug').upper())
-except AttributeError:
-    debuglevel = logging.INFO
-
-# Set logging levels.
-logging.basicConfig(format='%(message)s', level=debuglevel)
-logging.getLogger('requests').setLevel(logging.WARNING)  # Reset logging level of 'requests' module.
-
-availaliases = [str(x) for x in C.get('aliases').keys()]
-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')
-parser.add_argument('-l', '--list', action='store_true', help='list the available commands')
-parser.add_argument('-v', '--version', action='store_true', help='display the current version')
-parser.add_argument('command', metavar='command', nargs='?', help='command to call', choices=choices)
-parser.add_argument('args', metavar='arguments', nargs=argparse.REMAINDER, help='arguments of the command')
-parsedargs = parser.parse_args()
-
-cmd = parsedargs.command
-args = parsedargs.args
-
-# There is no command, what do we do?
-if not cmd:
-    if parsedargs.version:
-        print 'MDK version %s' % __version__
-    elif parsedargs.list:
-        for c in sorted(commandsList):
-            print '{0:<15} {1}'.format(c, getCommand(c)._description)
-    else:
-        parser.print_help()
-    sys.exit(0)
-
-# Looking up for an alias
-alias = C.get('aliases.%s' % cmd)
-if alias != None:
-    if alias.startswith('!'):
-        cmd = alias[1:]
-        i = 0
-        # Replace $1, $2, ... with passed arguments
-        for arg in args:
-            i += 1
-            cmd = cmd.replace('$%d' % i, arg)
-        # Remove unknown $[0-9]
-        cmd = re.sub(r'\$[0-9]', '', cmd)
-        result = process(cmd, stdout=None, stderr=None)
-        sys.exit(result[0])
-    else:
-        cmd = alias.split(' ')[0]
-        args = alias.split(' ')[1:] + args
-
-cls = getCommand(cmd)
-Cmd = cls(C)
-Runner = CommandRunner(Cmd)
-try:
-    Runner.run(args, prog='%s %s' % (os.path.basename(sys.argv[0]), cmd))
-except Exception as e:
-    import traceback
-    info = sys.exc_info()
-    logging.error('%s: %s', e.__class__.__name__, e)
-    logging.debug(''.join(traceback.format_tb(info[2])))
-    sys.exit(1)
+def main():
+
+    import sys
+    import argparse
+    import os
+    import re
+    import logging
+    from .command import CommandRunner
+    from .commands import getCommand, commandsList
+    from .config import Conf
+    from .tools import process
+    from .version import __version__
+
+    C = Conf()
+
+    try:
+        debuglevel = getattr(logging, C.get('debug').upper())
+    except AttributeError:
+        debuglevel = logging.INFO
+
+    # Set logging levels.
+    logging.basicConfig(format='%(message)s', level=debuglevel)
+    logging.getLogger('requests').setLevel(logging.WARNING)  # Reset logging level of 'requests' module.
+
+    availaliases = [str(x) for x in C.get('aliases').keys()]
+    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')
+    parser.add_argument('-l', '--list', action='store_true', help='list the available commands')
+    parser.add_argument('-v', '--version', action='store_true', help='display the current version')
+    parser.add_argument('command', metavar='command', nargs='?', help='command to call', choices=choices)
+    parser.add_argument('args', metavar='arguments', nargs=argparse.REMAINDER, help='arguments of the command')
+    parsedargs = parser.parse_args()
+
+    cmd = parsedargs.command
+    args = parsedargs.args
+
+    # There is no command, what do we do?
+    if not cmd:
+        if parsedargs.version:
+            print 'MDK version %s' % __version__
+        elif parsedargs.list:
+            for c in sorted(commandsList):
+                print '{0:<15} {1}'.format(c, getCommand(c)._description)
+        else:
+            parser.print_help()
+        sys.exit(0)
+
+    # Looking up for an alias
+    alias = C.get('aliases.%s' % cmd)
+    if alias != None:
+        if alias.startswith('!'):
+            cmd = alias[1:]
+            i = 0
+            # Replace $1, $2, ... with passed arguments
+            for arg in args:
+                i += 1
+                cmd = cmd.replace('$%d' % i, arg)
+            # Remove unknown $[0-9]
+            cmd = re.sub(r'\$[0-9]', '', cmd)
+            result = process(cmd, stdout=None, stderr=None)
+            sys.exit(result[0])
+        else:
+            cmd = alias.split(' ')[0]
+            args = alias.split(' ')[1:] + args
+
+    cls = getCommand(cmd)
+    Cmd = cls(C)
+    Runner = CommandRunner(Cmd)
+    try:
+        Runner.run(args, prog='%s %s' % (os.path.basename(sys.argv[0]), cmd))
+    except Exception as e:
+        import traceback
+        info = sys.exc_info()
+        logging.error('%s: %s', e.__class__.__name__, e)
+        logging.debug(''.join(traceback.format_tb(info[2])))
+        sys.exit(1)
+
+if __name__ == "__main__":
+    main()