Imported Upstream version 1.8.0
[platform/upstream/gpgme.git] / lang / python / gpg / errors.py
1 # Copyright (C) 2004 Igor Belyi <belyi@users.sourceforge.net>
2 # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
3 #
4 #    This library is free software; you can redistribute it and/or
5 #    modify it under the terms of the GNU Lesser General Public
6 #    License as published by the Free Software Foundation; either
7 #    version 2.1 of the License, or (at your option) any later version.
8 #
9 #    This library is distributed in the hope that it will be useful,
10 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 #    Lesser General Public License for more details.
13 #
14 #    You should have received a copy of the GNU Lesser General Public
15 #    License along with this library; if not, write to the Free Software
16 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
17
18 from __future__ import absolute_import, print_function, unicode_literals
19 del absolute_import, print_function, unicode_literals
20
21 from . import gpgme
22 from . import util
23
24 # To appease static analysis tools, we define some constants here.
25 # They are overwritten with the proper values by process_constants.
26 NO_ERROR = None
27 EOF = None
28
29 util.process_constants('GPG_ERR_', globals())
30 del util
31
32 class GpgError(Exception):
33     pass
34
35 class GPGMEError(GpgError):
36     def __init__(self, error = None, message = None):
37         self.error = error
38         self.message = message
39
40     @classmethod
41     def fromSyserror(cls):
42         return cls(gpgme.gpgme_err_code_from_syserror())
43
44     def getstring(self):
45         message = "%s: %s" % (gpgme.gpgme_strsource(self.error),
46                               gpgme.gpgme_strerror(self.error))
47         if self.message != None:
48             message = "%s: %s" % (self.message, message)
49         return message
50
51     def getcode(self):
52         return gpgme.gpgme_err_code(self.error)
53
54     def getsource(self):
55         return gpgme.gpgme_err_source(self.error)
56
57     def __str__(self):
58         return self.getstring()
59
60 def errorcheck(retval, extradata = None):
61     if retval:
62         raise GPGMEError(retval, extradata)
63
64 class KeyNotFound(GPGMEError, KeyError):
65     """Raised if a key was not found
66
67     GPGME indicates this condition with EOF, which is not very
68     idiomatic.  We raise this error that is both a GPGMEError
69     indicating EOF, and a KeyError.
70
71     """
72     def __init__(self, keystr):
73         self.keystr = keystr
74         GPGMEError.__init__(self, EOF)
75     def __str__(self):
76         return self.keystr
77
78 # These errors are raised in the idiomatic interface code.
79
80 class EncryptionError(GpgError):
81     pass
82
83 class InvalidRecipients(EncryptionError):
84     def __init__(self, recipients):
85         self.recipients = recipients
86     def __str__(self):
87         return ", ".join("{}: {}".format(r.fpr,
88                                          gpgme.gpgme_strerror(r.reason))
89                          for r in self.recipients)
90
91 class DeryptionError(GpgError):
92     pass
93
94 class UnsupportedAlgorithm(DeryptionError):
95     def __init__(self, algorithm):
96         self.algorithm = algorithm
97     def __str__(self):
98         return self.algorithm
99
100 class SigningError(GpgError):
101     pass
102
103 class InvalidSigners(SigningError):
104     def __init__(self, signers):
105         self.signers = signers
106     def __str__(self):
107         return ", ".join("{}: {}".format(s.fpr,
108                                          gpgme.gpgme_strerror(s.reason))
109                          for s in self.signers)
110
111 class VerificationError(GpgError):
112     pass
113
114 class BadSignatures(VerificationError):
115     def __init__(self, result):
116         self.result = result
117     def __str__(self):
118         return ", ".join("{}: {}".format(s.fpr,
119                                          gpgme.gpgme_strerror(s.status))
120                          for s in self.result.signatures
121                          if s.status != NO_ERROR)
122
123 class MissingSignatures(VerificationError):
124     def __init__(self, result, missing):
125         self.result = result
126         self.missing = missing
127     def __str__(self):
128         return ", ".join(k.subkeys[0].fpr for k in self.missing)