From 02b1388d795d2b62360b3acaf1ae3d165a9c6514 Mon Sep 17 00:00:00 2001 From: Antoine Nguyen Date: Wed, 9 Oct 2013 18:49:59 +0200 Subject: [PATCH] Moving commands related code to the commands directory. --- bin/modoboa-admin.py | 2 +- modoboa/core/commands/__init__.py | 90 +++++++++++++++++++++++++++ modoboa/core/commands/deploy.py | 2 +- modoboa/core/commands/help.py | 3 +- modoboa/core/commands/postfix_maps.py | 2 +- modoboa/core/management.py | 90 --------------------------- modoboa/core/models.py | 1 + 7 files changed, 96 insertions(+), 94 deletions(-) delete mode 100644 modoboa/core/management.py diff --git a/bin/modoboa-admin.py b/bin/modoboa-admin.py index 6da3810af..9309b6cc8 100644 --- a/bin/modoboa-admin.py +++ b/bin/modoboa-admin.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # coding: utf-8 -from modoboa.core.management import handle_command_line +from modoboa.core.commands import handle_command_line if __name__ == "__main__": handle_command_line() diff --git a/modoboa/core/commands/__init__.py b/modoboa/core/commands/__init__.py index e69de29bb..e7ef1ae9e 100644 --- a/modoboa/core/commands/__init__.py +++ b/modoboa/core/commands/__init__.py @@ -0,0 +1,90 @@ +# coding: utf-8 +import os +import sys +import argparse +from django.conf import settings +from django.template import Context, Template + + +class Command(object): + """Base command class + + A valid administrative command must inherit from this class. + """ + help = "No help available." + + def __init__(self, commands, verbose=False, **kwargs): + self._commands = commands + self._parser = argparse.ArgumentParser() + self._verbose = verbose + if not settings.configured: + settings.configure() + self._templates_dir = "%s/commands/templates" % os.path.dirname(__file__) + + def _render_template(self, tplfile, env): + fp = open(tplfile) + t = Template(fp.read()) + fp.close() + return t.render(Context(env)) + + def run(self, cmdline): + args = self._parser.parse_args(cmdline) + self.handle(args) + + def handle(self, parsed_args): + """A command must overload this method to be called + + :param parsed_args: + """ + raise NotImplementedError + + +def scan_for_commands(dirname=""): + """Build a dictionnary containing all commands + + :param str dirname: the directory where commands are located + :return: a dict of commands (name : class) + """ + path = os.path.join(os.path.dirname(__file__), dirname) + result = {} + for f in os.listdir(path): + if f in ['.', '..', '__init__.py']: + continue + if not f.endswith('.py'): + continue + if os.path.isfile(f): + continue + cmdname = f.replace('.py', '') + cmdmod = __import__("modoboa.core.commands", globals(), locals(), + [cmdname]) + cmdmod = getattr(cmdmod, cmdname) + if '_' in cmdname: + cmdclassname = ''.join([s.capitalize() for s in cmdname.split('_')]) + else: + cmdclassname = cmdname.capitalize() + try: + cmdclass = getattr(cmdmod, "%sCommand" % cmdclassname) + except AttributeError: + continue + result[cmdname] = cmdclass + return result + + +def handle_command_line(): + commands = scan_for_commands() + parser = argparse.ArgumentParser( + description="A set of utilities to ease the installation of Modoboa.", + epilog="""Available commands: +%s +""" % "\n".join(["\t%s" % c for c in sorted(commands)])) + parser.add_argument('--verbose', action='store_true', + help='Activate verbose output') + parser.add_argument('command', type=str, + help='A valid command name') + (args, remaining) = parser.parse_known_args() + + if not args.command in commands: + print >> sys.stderr, "Unknown command '%s'" % args.command + sys.exit(1) + + commands[args.command](commands, verbose=args.verbose).run(remaining) diff --git a/modoboa/core/commands/deploy.py b/modoboa/core/commands/deploy.py index bac58eab4..91c90341e 100644 --- a/modoboa/core/commands/deploy.py +++ b/modoboa/core/commands/deploy.py @@ -7,7 +7,7 @@ import subprocess from django.core import management from django.template import Context, Template from modoboa.lib.sysutils import exec_cmd -from modoboa.core.management import Command +from . import Command dbconn_tpl = """ '{{ conn_name }}': { diff --git a/modoboa/core/commands/help.py b/modoboa/core/commands/help.py index cd1ada463..dfe46307c 100644 --- a/modoboa/core/commands/help.py +++ b/modoboa/core/commands/help.py @@ -1,6 +1,7 @@ # coding: utf-8 import sys -from modoboa.core.management import Command +from . import Command + class HelpCommand(Command): help = "Display the help message associated to a specific command" diff --git a/modoboa/core/commands/postfix_maps.py b/modoboa/core/commands/postfix_maps.py index e8c0e549a..eda26f4fc 100644 --- a/modoboa/core/commands/postfix_maps.py +++ b/modoboa/core/commands/postfix_maps.py @@ -3,7 +3,7 @@ import getpass import inspect import os from django.template import Context, Template -from modoboa.core.management import Command +from . import Command class MapFilesGenerator(object): diff --git a/modoboa/core/management.py b/modoboa/core/management.py deleted file mode 100644 index 10c9e60ef..000000000 --- a/modoboa/core/management.py +++ /dev/null @@ -1,90 +0,0 @@ -# coding: utf-8 -import os -import sys -import argparse -from django.conf import settings -from django.template import Context, Template - - -class Command(object): - """Base command class - - A valid administrative command must inherit from this class. - """ - help = "No help available." - - def __init__(self, commands, verbose=False, **kwargs): - self._commands = commands - self._parser = argparse.ArgumentParser() - self._verbose = verbose - if not settings.configured: - settings.configure() - self._templates_dir = "%s/commands/templates" % os.path.dirname(__file__) - - def _render_template(self, tplfile, env): - fp = open(tplfile) - t = Template(fp.read()) - fp.close() - return t.render(Context(env)) - - def run(self, cmdline): - args = self._parser.parse_args(cmdline) - self.handle(args) - - def handle(self, parsed_args): - """A command must overload this method to be called - - :param parsed_args: - """ - raise NotImplementedError - - -def scan_for_commands(dirname="commands"): - """Build a dictionnary containing all commands - - :param dirname: the directory where commands are located - :return: a dict of commands (name : class) - """ - path = os.path.join(os.path.dirname(__file__), dirname) - result = {} - for f in os.listdir(path): - if f in ['.', '..', '__init__.py']: - continue - if not f.endswith('.py'): - continue - if os.path.isfile(f): - continue - cmdname = f.replace('.py', '') - cmdmod = __import__("modoboa.core.commands", globals(), locals(), - [cmdname]) - cmdmod = getattr(cmdmod, cmdname) - if '_' in cmdname: - cmdclassname = ''.join([s.capitalize() for s in cmdname.split('_')]) - else: - cmdclassname = cmdname.capitalize() - try: - cmdclass = getattr(cmdmod, "%sCommand" % cmdclassname) - except AttributeError: - continue - result[cmdname] = cmdclass - return result - - -def handle_command_line(): - commands = scan_for_commands() - parser = argparse.ArgumentParser( - description="A set of utilities to ease the installation of Modoboa.", - epilog="""Available commands: -%s -""" % "\n".join(["\t%s" % c for c in sorted(commands)])) - parser.add_argument('--verbose', action='store_true', - help='Activate verbose output') - parser.add_argument('command', type=str, - help='A valid command name') - (args, remaining) = parser.parse_known_args() - - if not args.command in commands: - print >> sys.stderr, "Unknown command '%s'" % args.command - sys.exit(1) - - commands[args.command](commands, verbose=args.verbose).run(remaining) diff --git a/modoboa/core/models.py b/modoboa/core/models.py index 987b706b7..4af548ad3 100644 --- a/modoboa/core/models.py +++ b/modoboa/core/models.py @@ -1,3 +1,4 @@ +import sys import os import re import hashlib