VirtualMailManager/cli/main.py
branchv0.6.x
changeset 343 9232ed7e4d85
parent 340 4515afec62e5
child 354 a653c43048b1
equal deleted inserted replaced
342:b0c971f943dc 343:9232ed7e4d85
     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
       
    12 
    11 from VirtualMailManager import ENCODING, errors
    13 from VirtualMailManager import ENCODING, errors
    12 from VirtualMailManager.config import ConfigValueError
    14 from VirtualMailManager.config import BadOptionError, ConfigValueError
    13 from VirtualMailManager.cli import w_err
    15 from VirtualMailManager.cli import w_err
    14 from VirtualMailManager.cli.handler import CliHandler
    16 from VirtualMailManager.cli.handler import CliHandler
    15 from VirtualMailManager.constants import DATABASE_ERROR, EX_MISSING_ARGS, \
    17 from VirtualMailManager.constants import CONF_ERROR, DATABASE_ERROR, \
    16      EX_SUCCESS, EX_UNKNOWN_COMMAND, EX_USER_INTERRUPT
    18      EX_MISSING_ARGS, EX_SUCCESS, EX_UNKNOWN_COMMAND, EX_USER_INTERRUPT, \
       
    19      INVALID_ARGUMENT
    17 from VirtualMailManager.cli.subcommands import RunContext, cmd_map, usage
    20 from VirtualMailManager.cli.subcommands import RunContext, cmd_map, usage
    18 
    21 
    19 
    22 
    20 _ = lambda msg: msg
    23 _ = lambda msg: msg
       
    24 
    21 
    25 
    22 def _get_handler():
    26 def _get_handler():
    23     """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."""
    24     try:
    28     try:
    25         handler = CliHandler()
    29         handler = CliHandler()
    26         handler.cfg_install()
    30         handler.cfg_install()
    27     except (errors.NotRootError, errors.PermissionError, errors.VMMError,
    31     except (errors.NotRootError, errors.PermissionError, errors.VMMError,
    28             errors.ConfigError, ConfigValueError), err:
    32             errors.ConfigError), err:
    29         w_err(err.code, _(u'Error: %s') % err.msg)
    33         w_err(err.code, _(u'Error: %s') % err.msg)
       
    34     except ConfigValueError, err:
       
    35         w_err(CONF_ERROR, _(u'Error: %s') % err)
    30     else:
    36     else:
    31         return handler
    37         return handler
    32 
    38 
    33 
    39 
    34 def run(argv):
    40 def run(argv):
    57         w_err(EX_USER_INTERRUPT, '', _(u'Ouch!'), '')
    63         w_err(EX_USER_INTERRUPT, '', _(u'Ouch!'), '')
    58     except errors.VMMError, err:
    64     except errors.VMMError, err:
    59         if err.code != DATABASE_ERROR:
    65         if err.code != DATABASE_ERROR:
    60             w_err(err.code, _(u'Error: %s') % err.msg)
    66             w_err(err.code, _(u'Error: %s') % err.msg)
    61         w_err(err.code, unicode(err.msg, ENCODING, 'replace'))
    67         w_err(err.code, unicode(err.msg, ENCODING, 'replace'))
       
    68     except (BadOptionError, ConfigValueError), err:
       
    69         w_err(INVALID_ARGUMENT, _(u'Error: %s') % err)
       
    70     except NoSectionError, err:
       
    71         w_err(INVALID_ARGUMENT, _(u"Error: No 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})
    62     if handler.has_warnings():
    76     if handler.has_warnings():
    63         w_err(0, _(u'Warnings:'), *handler.get_warnings())
    77         w_err(0, _(u'Warnings:'), *handler.get_warnings())
    64     return EX_SUCCESS
    78     return EX_SUCCESS
    65 
    79 
    66 del _
    80 del _