117 lines
3.9 KiB
Python
117 lines
3.9 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2010 Entr'ouvert
|
|
#
|
|
# 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 2 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, write to the Free Software
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
# 02110-1301 USA
|
|
|
|
import optparse
|
|
from optparse import make_option
|
|
import sys
|
|
import os
|
|
|
|
__all__ = [
|
|
'Command',
|
|
'register_command',
|
|
]
|
|
|
|
import qommon
|
|
qommon._commands = {}
|
|
|
|
class Command:
|
|
doc = ''
|
|
name = None
|
|
usage_args = '[ options ... ]'
|
|
|
|
def __init__(self, options=[]):
|
|
self.options = options
|
|
|
|
def run(self, args):
|
|
options, args = self.parse_args(args)
|
|
return self.execute(options, args)
|
|
|
|
def parse_args(self, args):
|
|
self.parser = optparse.OptionParser(
|
|
usage='%%prog %s %s' % (self.name, _(self.usage_args)),
|
|
description=self.doc)
|
|
self.parser.add_options(self.options)
|
|
return self.parser.parse_args(args)
|
|
|
|
def execute(self, options, args):
|
|
"""The body of the command"""
|
|
raise NotImplementedError
|
|
|
|
def register(cls):
|
|
qommon._commands[cls.name] = cls
|
|
register = classmethod(register)
|
|
|
|
class Ctl:
|
|
def __init__(self, cmd_prefixes=[]):
|
|
self.cmd_prefixes = cmd_prefixes
|
|
self.parser = optparse.OptionParser(
|
|
usage='%prog [ -f config ] command [ options ... ]',
|
|
add_help_option=False)
|
|
self.parser.disable_interspersed_args()
|
|
self.parser.add_option('-f', '--file', action='store', metavar='CONFIG',
|
|
type='string', dest='configfile',
|
|
help=_('use a non default configuration file'))
|
|
self.parser.add_option('--help', action='callback',
|
|
callback=self.print_help,
|
|
help=_("Display this help and exit"))
|
|
|
|
def print_help(self, *args):
|
|
self.parser.print_help()
|
|
print
|
|
for cmd_prefix in self.cmd_prefixes:
|
|
if not cmd_prefix in sys.modules:
|
|
__import__(cmd_prefix)
|
|
mod = sys.modules.get(cmd_prefix)
|
|
if not mod:
|
|
continue
|
|
if os.path.isdir(mod.__file__):
|
|
cmddir = os.path.abspath(mod.__file__)
|
|
else:
|
|
cmddir = os.path.abspath(os.path.dirname(mod.__file__))
|
|
for fname in os.listdir(os.path.join(cmddir)):
|
|
name, ext = os.path.splitext(fname)
|
|
if not ext == '.py':
|
|
continue
|
|
if name.startswith('_'):
|
|
continue
|
|
try:
|
|
__import__('%s.%s' % (cmd_prefix, name))
|
|
except ImportError:
|
|
pass
|
|
|
|
commands = [(x.name, x.doc) for x in qommon._commands.values()]
|
|
commands.sort()
|
|
print 'Available commands:'
|
|
for name, description in commands:
|
|
print ' %-15s %s' % (name, description)
|
|
sys.exit(0)
|
|
|
|
def run(self, args):
|
|
options, args = self.parser.parse_args(args)
|
|
command, args = args[0], args[1:]
|
|
if command not in qommon._commands:
|
|
for cmd_prefix in self.cmd_prefixes:
|
|
try:
|
|
__import__('%s.%s' % (cmd_prefix, command))
|
|
except ImportError:
|
|
pass
|
|
command_class = qommon._commands[command]
|
|
cmd = command_class()
|
|
return cmd.run(args)
|
|
|