aboutsummaryrefslogtreecommitdiff
blob: c3bcdd4bc676a5ac49af5775057fc5b0f36bd667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# Copyright(c) 2004-2009, Gentoo Foundation
#
# Licensed under the GNU General Public License, v2 or later

"""Exception classes for gentoolkit"""

__all__ = (
    "GentoolkitException",
    "GentoolkitFatalError",
    "GentoolkitAmbiguousPackage",
    "GentoolkitInvalidAtom",
    "GentoolkitInvalidCategory",
    "GentoolkitInvalidPackage",
    "GentoolkitInvalidCPV",
    "GentoolkitInvalidRegex",
    "GentoolkitInvalidVersion",
    "GentoolkitNoMatches",
    "GentoolkitSetNotFound",
    "GentoolkitUnknownKeyword",
    "GentoolkitNonZeroExit",
)

# ==========
# Exceptions
# ==========


class GentoolkitException(Exception):
    """Base class for gentoolkit exceptions."""

    def __init__(self, is_serious=True):
        self.is_serious = is_serious


class GentoolkitFatalError(GentoolkitException):
    """A fatal error occurred. Usually used to catch Portage exceptions."""

    def __init__(self, err, is_serious=True):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.err = err

    def __str__(self):
        return "Fatal error: %s" % self.err


class GentoolkitAmbiguousPackage(GentoolkitException):
    """Got an ambiguous package name."""

    def __init__(self, choices, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.choices = choices

    def __str__(self):
        choices = "\n".join("  %s" % x for x in self.choices)
        return "\n".join(("Ambiguous package name. Choose from:", choices))


class GentoolkitInvalidAtom(GentoolkitException):
    """Got a malformed package atom."""

    def __init__(self, atom, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.atom = atom

    def __str__(self):
        return "Invalid atom: '%s'" % self.atom


class GentoolkitSetNotFound(GentoolkitException):
    """Got unknown set."""

    def __init__(self, setname, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.setname = setname

    def __str__(self):
        return "Unknown set: '%s'" % self.setname


class GentoolkitInvalidCategory(GentoolkitException):
    """The category was not listed in portage.settings.categories."""

    def __init__(self, category, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.category = category

    def __str__(self):
        return "Invalid category: '%s'" % self.category


class GentoolkitInvalidPackage(GentoolkitException):
    """Got an unknown or invalid package."""

    def __init__(self, package, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.package = package

    def __str__(self):
        return "Invalid package: '%s'" % self.package


class GentoolkitInvalidCPV(GentoolkitException):
    """Got an invalid category/package-ver string."""

    def __init__(self, cpv, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.cpv = cpv

    def __str__(self):
        return "Invalid CPV: '%s'" % self.cpv


class GentoolkitInvalidRegex(GentoolkitException):
    """The regex could not be compiled."""

    def __init__(self, regex, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.regex = regex

    def __str__(self):
        return "Invalid regex: '%s'" % self.regex


class GentoolkitInvalidVersion(GentoolkitException):
    """Got a malformed version."""

    def __init__(self, version, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.version = version

    def __str__(self):
        return "Malformed version: '%s'" % self.version


class GentoolkitNoMatches(GentoolkitException):
    """No packages were found matching the search query."""

    def __init__(self, query, in_installed=False, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.query = query
        self.in_installed = in_installed

    def __str__(self):
        inst = "installed " if self.in_installed else ""
        return f"No {inst}packages matching '{self.query}'"


class GentoolkitUnknownKeyword(GentoolkitException):
    """No packages were found matching the search query."""

    def __init__(self, query, keywords, use, is_serious=True):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.query = query
        self.keywords = keywords
        self.use = use

    def __str__(self):
        return (
            "Unable to determine the install keyword for:\n"
            + "'%s', KEYWORDS = '%s'\nUSE flags = '%s'"
            % (self.query, self.keywords, self.use)
        )


class GentoolkitNonZeroExit(GentoolkitException):
    """Used to signal, that a non-fatal, no warning error occurred.
    The primary use case is for not returning any data."""

    def __init__(self, return_code=1, is_serious=False):
        GentoolkitException.__init__(self, is_serious=is_serious)
        self.return_code = return_code


# vim: set ts=4 sw=4 tw=79: