VirtualMailManager/cli/main.py
author Pascal Volk <neverseen@users.sourceforge.net>
Fri, 06 Aug 2010 23:06:19 +0000
branchv0.6.x
changeset 346 95d45e4ec1a6
parent 343 9232ed7e4d85
child 354 a653c43048b1
permissions -rw-r--r--
VMM/cli/handler: Don't perform config and environment checks twice.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     1
# -*- coding: UTF-8 -*-
162
0ac9ef587769 Updated copyright notices to include the year 2010.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 157
diff changeset
     2
# Copyright 2007 - 2010, Pascal Volk
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     3
# See COPYING for distribution information.
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
     4
"""
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
     5
    VirtualMailManager.cli.main
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
     6
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
     7
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
     8
    VirtualMailManager's command line interface.
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
     9
"""
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    10
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    11
from ConfigParser import NoOptionError, NoSectionError
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    12
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    13
from VirtualMailManager import ENCODING, errors
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    14
from VirtualMailManager.config import BadOptionError, ConfigValueError
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    15
from VirtualMailManager.cli import w_err
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    16
from VirtualMailManager.cli.handler import CliHandler
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    17
from VirtualMailManager.constants import CONF_ERROR, DATABASE_ERROR, \
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    18
     EX_MISSING_ARGS, EX_SUCCESS, EX_UNKNOWN_COMMAND, EX_USER_INTERRUPT, \
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    19
     INVALID_ARGUMENT
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    20
from VirtualMailManager.cli.subcommands import RunContext, cmd_map, usage
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    21
47
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 45
diff changeset
    22
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    23
_ = lambda msg: msg
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    24
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    25
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    26
def _get_handler():
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    27
    """Try to get a CliHandler. Exit the program when an error occurs."""
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    28
    try:
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    29
        handler = CliHandler()
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    30
        handler.cfg_install()
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    31
    except (errors.NotRootError, errors.PermissionError, errors.VMMError,
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    32
            errors.ConfigError), err:
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    33
        w_err(err.code, _(u'Error: %s') % err.msg)
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    34
    except ConfigValueError, err:
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    35
        w_err(CONF_ERROR, _(u'Error: %s') % err)
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    36
    else:
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    37
        return handler
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    38
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    39
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    40
def run(argv):
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    41
    if len(argv) < 2:
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    42
        usage(EX_MISSING_ARGS, _(u"You must specify a subcommand at least"))
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    43
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    44
    sub_cmd = argv[1].lower()
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    45
    if sub_cmd in cmd_map:
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    46
        cmd_func = cmd_map[sub_cmd].func
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    47
    else:
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    48
        for cmd in cmd_map.itervalues():
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    49
            if cmd.alias == sub_cmd:
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    50
                cmd_func = cmd.func
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    51
                sub_cmd = cmd.name
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    52
                break
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    53
        else:
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    54
            usage(EX_UNKNOWN_COMMAND, _(u"Unknown subcommand: '%s'") % sub_cmd)
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    55
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    56
    handler = _get_handler()
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    57
    run_ctx = RunContext(argv, handler, sub_cmd)
147
e3fd0b67ae50 Improved error handling in vmm's {user,alias,relocated}info.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 142
diff changeset
    58
    try:
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    59
        cmd_func(run_ctx)
183
eb4c73d9d0a4 vmm: reworked subcommand/arguments mapping.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    60
    except (EOFError, KeyboardInterrupt):
eb4c73d9d0a4 vmm: reworked subcommand/arguments mapping.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    61
        # TP: We have to cry, because root has killed/interrupted vmm
eb4c73d9d0a4 vmm: reworked subcommand/arguments mapping.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    62
        # with Ctrl+C or Ctrl+D.
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    63
        w_err(EX_USER_INTERRUPT, '', _(u'Ouch!'), '')
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    64
    except errors.VMMError, err:
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    65
        if err.code != DATABASE_ERROR:
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    66
            w_err(err.code, _(u'Error: %s') % err.msg)
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    67
        w_err(err.code, unicode(err.msg, ENCODING, 'replace'))
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    68
    except (BadOptionError, ConfigValueError), err:
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    69
        w_err(INVALID_ARGUMENT, _(u'Error: %s') % err)
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    70
    except NoSectionError, err:
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    71
        w_err(INVALID_ARGUMENT, _(u"Error: No section '%s'") % err.section)
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    72
    except NoOptionError, err:
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    73
        w_err(INVALID_ARGUMENT,
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    74
              _(u"Error: No option '%(option)s' in section: '%(section)s'") %
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    75
              {'option': err.option, 'section': err.section})
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    76
    if handler.has_warnings():
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    77
        w_err(0, _(u'Warnings:'), *handler.get_warnings())
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    78
    return EX_SUCCESS
183
eb4c73d9d0a4 vmm: reworked subcommand/arguments mapping.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    79
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    80
del _