__init__.py 2.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
# commands
#
# Copyright (C) 2011 Carlos Garcia Campos <carlosgc@gnome.org>
#
# 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import argparse

__all__ = [ 'register_command',
            'print_help',
            'run',
            'UnknownCommandError',
            'Command' ]

class UnknownCommandError(Exception):
    '''Unknown command'''

class Command:

    name = None
    usage_args = '[ options ... ]'
    description = None

    def __init__(self):
        self._parser = argparse.ArgumentParser(
            description = self.description,
            prog = 'poppler-regtest %s' % (self.name),
            usage = 'poppler-regtest %s %s' % (self.name, self.usage_args))

    def _get_args_parser(self):
        return self._parser

    def execute(self, args):
        ns = self._parser.parse_args(args)
47
        return self.run(vars(ns))
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

    def run(self, options):
        raise NotImplementedError

_commands = {}
def register_command(command_name, command_class):
    _commands[command_name] = command_class

def _get_command(command_name):
    if command_name not in _commands:
        try:
            __import__('commands.%s' % command_name)
        except ImportError:
            pass

    if command_name not in _commands:
        raise UnknownCommandError('Invalid %s command' % command_name)

    return _commands[command_name]

def run(args):
    command_class = _get_command(args[0])
    command = command_class()
71
    return command.execute(args[1:])
72 73 74 75 76 77 78 79 80 81 82 83 84 85

def print_help():
    import os
    thisdir = os.path.abspath(os.path.dirname(__file__))

    for fname in os.listdir(os.path.join(thisdir)):
        name, ext = os.path.splitext(fname)
        if not ext == '.py':
            continue
        try:
            __import__('commands.%s' % name)
        except ImportError:
            pass

86
    print("Commands are:")
87 88 89
    commands = [(x.name, x.description) for x in _commands.values()]
    commands.sort()
    for name, description in commands:
90
        print("  %-15s %s" % (name, description))
91 92

    print
93
    print("For more information run 'poppler-regtest --help-command <command>'")