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 |
17 from VirtualMailManager.constants import DATABASE_ERROR, EX_MISSING_ARGS, \ |
17 from VirtualMailManager.constants import DATABASE_ERROR, EX_SUCCESS, \ |
18 EX_SUCCESS, EX_UNKNOWN_COMMAND, EX_USER_INTERRUPT, INVALID_ARGUMENT |
18 EX_USER_INTERRUPT, INVALID_ARGUMENT |
19 from VirtualMailManager.cli.subcommands import RunContext, cmd_map, \ |
19 from VirtualMailManager.cli.subcommands import RunContext, setup_parser |
20 update_cmd_map, usage |
|
21 |
20 |
22 |
21 |
23 _ = lambda msg: msg |
22 _ = lambda msg: msg |
24 |
23 |
25 |
24 |
33 else: |
32 else: |
34 handler.cfg_install() |
33 handler.cfg_install() |
35 return handler |
34 return handler |
36 |
35 |
37 |
36 |
38 def run(argv): |
37 def run(): |
39 update_cmd_map() |
38 parser = setup_parser() |
40 if len(argv) < 2: |
39 args = parser.parse_args() |
41 usage(EX_MISSING_ARGS, _("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.values(): |
|
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, _("Unknown subcommand: '%s'") % sub_cmd) |
|
54 |
|
55 handler = _get_handler() |
40 handler = _get_handler() |
56 run_ctx = RunContext(argv, handler, sub_cmd) |
41 run_ctx = RunContext(args, handler) |
57 try: |
42 try: |
58 cmd_func(run_ctx) |
43 args.func(run_ctx) |
59 except (EOFError, KeyboardInterrupt): |
44 except (EOFError, KeyboardInterrupt): |
60 # TP: We have to cry, because root has killed/interrupted vmm |
45 # TP: We have to cry, because root has killed/interrupted vmm |
61 # with Ctrl+C or Ctrl+D. |
46 # with Ctrl+C or Ctrl+D. |
62 w_err(EX_USER_INTERRUPT, '', _('Ouch!'), '') |
47 w_err(EX_USER_INTERRUPT, '', _('Ouch!'), '') |
63 except errors.VMMError as err: |
48 except errors.VMMError as err: |