VirtualMailManager/cli/main.py
author Pascal Volk <user@localhost.localdomain.org>
Mon, 10 Feb 2014 22:54:43 +0000
branchv0.7.x
changeset 718 65a29a373c19
parent 713 74ee0a62039c
permissions -rw-r--r--
doc/web: Replaced vmm.cfg by a symbolic link.
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 -*-
703
58815c004a61 Updated copyright notices to include the year 2014.
Pascal Volk <user@localhost.localdomain.org>
parents: 675
diff changeset
     2
# Copyright (c) 2007 - 2014, 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
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    11
from configparser import NoOptionError, NoSectionError
343
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
713
74ee0a62039c VMM/cli: Make sure that there is at least one argument.
Pascal Volk <user@localhost.localdomain.org>
parents: 711
diff changeset
    17
from VirtualMailManager.constants import EX_MISSING_ARGS, EX_SUCCESS, \
74ee0a62039c VMM/cli: Make sure that there is at least one argument.
Pascal Volk <user@localhost.localdomain.org>
parents: 711
diff changeset
    18
     EX_USER_INTERRUPT, INVALID_ARGUMENT
666
50148adebee9 VMM/cli/main: Adjusted to new argparse functionality.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    19
from VirtualMailManager.cli.subcommands import RunContext, setup_parser
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    20
47
191d5a5adc4a * Removed gettext import and setup in VirtualMailManager/:
Pascal Volk <neverseen@users.sourceforge.net>
parents: 45
diff changeset
    21
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    22
_ = lambda msg: msg
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    23
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    24
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    25
def _get_handler():
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    26
    """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
    27
    try:
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    28
        handler = CliHandler()
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    29
    except (errors.NotRootError, errors.PermissionError, errors.VMMError,
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    30
            errors.ConfigError) as err:
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    31
        w_err(err.code, _('Error: %s') % err.msg)
48
0d5f58f8b8f5 * 'VirtualMailManager/Exceptions.py'
Pascal Volk <neverseen@users.sourceforge.net>
parents: 47
diff changeset
    32
    else:
354
a653c43048b1 VMM/cli/main: Adjusted _get_handler() to changes in config module.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 343
diff changeset
    33
        handler.cfg_install()
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    34
        return handler
0
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    35
bb0aa2102206 Initial import @sf.net
Pascal Volk <neverseen@users.sourceforge.net>
parents:
diff changeset
    36
713
74ee0a62039c VMM/cli: Make sure that there is at least one argument.
Pascal Volk <user@localhost.localdomain.org>
parents: 711
diff changeset
    37
def run(argv):
666
50148adebee9 VMM/cli/main: Adjusted to new argparse functionality.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    38
    parser = setup_parser()
713
74ee0a62039c VMM/cli: Make sure that there is at least one argument.
Pascal Volk <user@localhost.localdomain.org>
parents: 711
diff changeset
    39
    if len(argv) < 2:
74ee0a62039c VMM/cli: Make sure that there is at least one argument.
Pascal Volk <user@localhost.localdomain.org>
parents: 711
diff changeset
    40
        parser.print_usage()
74ee0a62039c VMM/cli: Make sure that there is at least one argument.
Pascal Volk <user@localhost.localdomain.org>
parents: 711
diff changeset
    41
        parser.exit(status=EX_MISSING_ARGS,
74ee0a62039c VMM/cli: Make sure that there is at least one argument.
Pascal Volk <user@localhost.localdomain.org>
parents: 711
diff changeset
    42
                   message=_('You must specify a subcommand at least.') + '\n')
666
50148adebee9 VMM/cli/main: Adjusted to new argparse functionality.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    43
    args = parser.parse_args()
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    44
    handler = _get_handler()
666
50148adebee9 VMM/cli/main: Adjusted to new argparse functionality.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    45
    run_ctx = RunContext(args, handler)
147
e3fd0b67ae50 Improved error handling in vmm's {user,alias,relocated}info.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 142
diff changeset
    46
    try:
666
50148adebee9 VMM/cli/main: Adjusted to new argparse functionality.
Pascal Volk <user@localhost.localdomain.org>
parents: 643
diff changeset
    47
        args.func(run_ctx)
183
eb4c73d9d0a4 vmm: reworked subcommand/arguments mapping.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    48
    except (EOFError, KeyboardInterrupt):
eb4c73d9d0a4 vmm: reworked subcommand/arguments mapping.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    49
        # 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
    50
        # with Ctrl+C or Ctrl+D.
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    51
        w_err(EX_USER_INTERRUPT, '', _('Ouch!'), '')
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    52
    except errors.VMMError as err:
694
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    53
        if handler.has_warnings():
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    54
            w_err(0, _('Warnings:'), *handler.get_warnings())
b1bfd4d1d9c0 Dropped pyPgSQL support.
Pascal Volk <user@localhost.localdomain.org>
parents: 676
diff changeset
    55
        w_err(err.code, _('Error: %s') % err.msg)
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    56
    except (BadOptionError, ConfigValueError) as err:
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    57
        w_err(INVALID_ARGUMENT, _('Error: %s') % err)
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    58
    except NoSectionError as err:
417
8209da83e256 VMM: Updated some messages. (doesn't, isn't) -> (does not, is not)
Pascal Volk <neverseen@users.sourceforge.net>
parents: 366
diff changeset
    59
        w_err(INVALID_ARGUMENT,
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    60
              _("Error: Unknown section: '%s'") % err.section)
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    61
    except NoOptionError as err:
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    62
        w_err(INVALID_ARGUMENT,
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    63
              _("Error: No option '%(option)s' in section: '%(section)s'") %
343
9232ed7e4d85 VMM/cli/main: Except more errors and handle them correct.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 340
diff changeset
    64
              {'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
    65
    if handler.has_warnings():
643
df1e3b67882a Ran 2to3 from Python 3.2.3.
Pascal Volk <user@localhost.localdomain.org>
parents: 616
diff changeset
    66
        w_err(0, _('Warnings:'), *handler.get_warnings())
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    67
    return EX_SUCCESS
183
eb4c73d9d0a4 vmm: reworked subcommand/arguments mapping.
Pascal Volk <neverseen@users.sourceforge.net>
parents: 180
diff changeset
    68
340
4515afec62e5 vmm: Renamed to VirtualMailManager/cli/main.py. Splitted subcommands
Pascal Volk <neverseen@users.sourceforge.net>
parents: 232
diff changeset
    69
del _