From 3771beb8edf8146a0c9ac30cde2d38fcb5410da4 Mon Sep 17 00:00:00 2001 From: Fred Date: Sat, 2 Mar 2013 09:37:00 +0800 Subject: [PATCH] Command caller --- lib/command.py | 4 +-- mdk.py | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100755 mdk.py diff --git a/lib/command.py b/lib/command.py index 8251800..391d21d 100644 --- a/lib/command.py +++ b/lib/command.py @@ -90,8 +90,8 @@ class RunCommand(object): def command(self): return self._command - def run(self, sysargs=sys.argv): - parser = argparse.ArgumentParser(description=self.command.description) + def run(self, sysargs=sys.argv, prog=None): + parser = argparse.ArgumentParser(description=self.command.description, prog=prog) for argument in self.command.arguments: args = argument[0] kwargs = argument[1] diff --git a/mdk.py b/mdk.py new file mode 100755 index 0000000..f10399e --- /dev/null +++ b/mdk.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Moodle Development Kit + +Copyright (c) 2013 Frédéric Massart - FMCorz.net + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . + +http://github.com/FMCorz/mdk +""" + +import sys +import argparse +import os +import re +from lib.command import RunCommand +from lib.commands import * +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')] +availaliases = [str(x) for x in C.get('aliases').keys()] +choices = sorted(availcmds + 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='displauy 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(availcmds): + print ' %s' % c + 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 + +# Calling the command +classname = '%sCommand' % (cmd.capitalize()) +cls = globals().get(classname) +Cmd = cls(C) +Runner = RunCommand(Cmd) +Runner.run(args, prog='%s %s' % (os.path.basename(sys.argv[0]), cmd)) -- 2.11.0