# -*- coding: UTF-8 -*-# Copyright (c) 2007 - 2014, Pascal Volk# See COPYING for distribution information.""" VirtualMailManager.cli.main ~~~~~~~~~~~~~~~~~~~~~~~~~~~ VirtualMailManager's command line interface."""fromconfigparserimportNoOptionError,NoSectionErrorfromVirtualMailManagerimportENCODING,errorsfromVirtualMailManager.configimportBadOptionError,ConfigValueErrorfromVirtualMailManager.cliimportw_errfromVirtualMailManager.cli.handlerimportCliHandlerfromVirtualMailManager.constantsimportEX_MISSING_ARGS,EX_SUCCESS, \EX_USER_INTERRUPT,INVALID_ARGUMENTfromVirtualMailManager.cli.subcommandsimportRunContext,setup_parser_=lambdamsg:msgdef_get_handler():"""Try to get a CliHandler. Exit the program when an error occurs."""try:handler=CliHandler()except(errors.NotRootError,errors.PermissionError,errors.VMMError,errors.ConfigError)aserr:w_err(err.code,_('Error: %s')%err.msg)else:handler.cfg_install()returnhandlerdefrun(argv):parser=setup_parser()iflen(argv)<2:parser.print_usage()parser.exit(status=EX_MISSING_ARGS,message=_('You must specify a subcommand at least.')+'\n')args=parser.parse_args()handler=_get_handler()run_ctx=RunContext(args,handler)try:args.func(run_ctx)except(EOFError,KeyboardInterrupt):# TP: We have to cry, because root has killed/interrupted vmm# with Ctrl+C or Ctrl+D.w_err(EX_USER_INTERRUPT,'',_('Ouch!'),'')excepterrors.VMMErroraserr:ifhandler.has_warnings():w_err(0,_('Warnings:'),*handler.get_warnings())w_err(err.code,_('Error: %s')%err.msg)except(BadOptionError,ConfigValueError)aserr:w_err(INVALID_ARGUMENT,_('Error: %s')%err)exceptNoSectionErroraserr:w_err(INVALID_ARGUMENT,_("Error: Unknown section: '%s'")%err.section)exceptNoOptionErroraserr:w_err(INVALID_ARGUMENT,_("Error: No option '%(option)s' in section: '%(section)s'")%{'option':err.option,'section':err.section})ifhandler.has_warnings():w_err(0,_('Warnings:'),*handler.get_warnings())returnEX_SUCCESSdel_