1# This Source Code Form is subject to the terms of the Mozilla Public
2# License, v. 2.0. If a copy of the MPL was not distributed with this
3# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5from __future__ import absolute_import, unicode_literals
6
7
8class CommandContext(object):
9    """Holds run-time state so it can easily be passed to command providers."""
10
11    def __init__(
12        self, cwd: str, settings=None, log_manager=None, commands=None, **kwargs
13    ):
14        self.cwd = cwd
15        self.settings = settings
16        self.log_manager = log_manager
17        self.commands = commands
18        self.is_interactive = None  # Filled in after args are parsed
19        self.telemetry = None  # Filled in after args are parsed
20        self.command_attrs = {}
21
22        for k, v in kwargs.items():
23            setattr(self, k, v)
24
25
26class MachError(Exception):
27    """Base class for all errors raised by mach itself."""
28
29
30class NoCommandError(MachError):
31    """No command was passed into mach."""
32
33    def __init__(self, namespace):
34        MachError.__init__(self)
35        self.namespace = namespace
36
37
38class UnknownCommandError(MachError):
39    """Raised when we attempted to execute an unknown command."""
40
41    def __init__(self, command, verb, suggested_commands=None):
42        MachError.__init__(self)
43
44        self.command = command
45        self.verb = verb
46        self.suggested_commands = suggested_commands or []
47
48
49class UnrecognizedArgumentError(MachError):
50    """Raised when an unknown argument is passed to mach."""
51
52    def __init__(self, command, arguments):
53        MachError.__init__(self)
54
55        self.command = command
56        self.arguments = arguments
57
58
59class FailedCommandError(Exception):
60    """Raised by commands to signal a handled failure to be printed by mach
61
62    When caught by mach a FailedCommandError will print message and exit
63    with ''exit_code''. The optional ''reason'' is a string in cases where
64    other scripts may wish to handle the exception, though this is generally
65    intended to communicate failure to mach.
66    """
67
68    def __init__(self, message, exit_code=1, reason=""):
69        Exception.__init__(self, message)
70        self.exit_code = exit_code
71        self.reason = reason
72
73
74class MissingFileError(MachError):
75    """Attempted to load a mach commands file that doesn't exist."""
76