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