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 _ |