VirtualMailManager/cli/main.py
changeset 571 a4aead244f75
parent 568 14abdd04ddf5
child 616 996bfcae49b0
equal deleted inserted replaced
465:c0e1fb1b0145 571:a4aead244f75
       
     1 # -*- coding: UTF-8 -*-
       
     2 # Copyright (c) 2007 - 2012, Pascal Volk
       
     3 # See COPYING for distribution information.
       
     4 """
       
     5     VirtualMailManager.cli.main
       
     6     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
       
     7 
       
     8     VirtualMailManager's command line interface.
       
     9 """
       
    10 
       
    11 from ConfigParser import NoOptionError, NoSectionError
       
    12 
       
    13 from VirtualMailManager import ENCODING, errors
       
    14 from VirtualMailManager.config import BadOptionError, ConfigValueError
       
    15 from VirtualMailManager.cli import w_err
       
    16 from VirtualMailManager.cli.handler import CliHandler
       
    17 from VirtualMailManager.constants import DATABASE_ERROR, EX_MISSING_ARGS, \
       
    18      EX_SUCCESS, EX_UNKNOWN_COMMAND, EX_USER_INTERRUPT, INVALID_ARGUMENT
       
    19 from VirtualMailManager.cli.subcommands import RunContext, cmd_map, \
       
    20      update_cmd_map, usage
       
    21 
       
    22 
       
    23 _ = lambda msg: msg
       
    24 
       
    25 
       
    26 def _get_handler():
       
    27     """Try to get a CliHandler. Exit the program when an error occurs."""
       
    28     try:
       
    29         handler = CliHandler()
       
    30     except (errors.NotRootError, errors.PermissionError, errors.VMMError,
       
    31             errors.ConfigError), err:
       
    32         w_err(err.code, _(u'Error: %s') % err.msg)
       
    33     else:
       
    34         handler.cfg_install()
       
    35         return handler
       
    36 
       
    37 
       
    38 def run(argv):
       
    39     update_cmd_map()
       
    40     if len(argv) < 2:
       
    41         usage(EX_MISSING_ARGS, _(u"You must specify a subcommand at least."))
       
    42 
       
    43     sub_cmd = argv[1].lower()
       
    44     if sub_cmd in cmd_map:
       
    45         cmd_func = cmd_map[sub_cmd].func
       
    46     else:
       
    47         for cmd in cmd_map.itervalues():
       
    48             if cmd.alias == sub_cmd:
       
    49                 cmd_func = cmd.func
       
    50                 sub_cmd = cmd.name
       
    51                 break
       
    52         else:
       
    53             usage(EX_UNKNOWN_COMMAND, _(u"Unknown subcommand: '%s'") % sub_cmd)
       
    54 
       
    55     handler = _get_handler()
       
    56     run_ctx = RunContext(argv, handler, sub_cmd)
       
    57     try:
       
    58         cmd_func(run_ctx)
       
    59     except (EOFError, KeyboardInterrupt):
       
    60         # TP: We have to cry, because root has killed/interrupted vmm
       
    61         # with Ctrl+C or Ctrl+D.
       
    62         w_err(EX_USER_INTERRUPT, '', _(u'Ouch!'), '')
       
    63     except errors.VMMError, err:
       
    64         if err.code != DATABASE_ERROR:
       
    65             w_err(err.code, _(u'Error: %s') % err.msg)
       
    66         w_err(err.code, unicode(err.msg, ENCODING, 'replace'))
       
    67     except (BadOptionError, ConfigValueError), err:
       
    68         w_err(INVALID_ARGUMENT, _(u'Error: %s') % err)
       
    69     except NoSectionError, err:
       
    70         w_err(INVALID_ARGUMENT,
       
    71               _(u"Error: Unknown section: '%s'") % err.section)
       
    72     except NoOptionError, err:
       
    73         w_err(INVALID_ARGUMENT,
       
    74               _(u"Error: No option '%(option)s' in section: '%(section)s'") %
       
    75               {'option': err.option, 'section': err.section})
       
    76     if handler.has_warnings():
       
    77         w_err(0, _(u'Warnings:'), *handler.get_warnings())
       
    78     return EX_SUCCESS
       
    79 
       
    80 del _