1 # Copyright (C) 2004 Igor Belyi <belyi@users.sourceforge.net>
2 # Copyright (C) 2002 John Goerzen <jgoerzen@complete.org>
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.
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.
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
18 from __future__ import absolute_import, print_function, unicode_literals
19 del absolute_import, print_function, unicode_literals
24 # To appease static analysis tools, we define some constants here.
25 # They are overwritten with the proper values by process_constants.
29 util.process_constants('GPG_ERR_', globals())
32 class GpgError(Exception):
35 class GPGMEError(GpgError):
36 def __init__(self, error = None, message = None):
38 self.message = message
41 def fromSyserror(cls):
42 return cls(gpgme.gpgme_err_code_from_syserror())
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)
52 return gpgme.gpgme_err_code(self.error)
55 return gpgme.gpgme_err_source(self.error)
58 return self.getstring()
60 def errorcheck(retval, extradata = None):
62 raise GPGMEError(retval, extradata)
64 class KeyNotFound(GPGMEError, KeyError):
65 """Raised if a key was not found
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.
72 def __init__(self, keystr):
74 GPGMEError.__init__(self, EOF)
78 # These errors are raised in the idiomatic interface code.
80 class EncryptionError(GpgError):
83 class InvalidRecipients(EncryptionError):
84 def __init__(self, recipients):
85 self.recipients = recipients
87 return ", ".join("{}: {}".format(r.fpr,
88 gpgme.gpgme_strerror(r.reason))
89 for r in self.recipients)
91 class DeryptionError(GpgError):
94 class UnsupportedAlgorithm(DeryptionError):
95 def __init__(self, algorithm):
96 self.algorithm = algorithm
100 class SigningError(GpgError):
103 class InvalidSigners(SigningError):
104 def __init__(self, signers):
105 self.signers = signers
107 return ", ".join("{}: {}".format(s.fpr,
108 gpgme.gpgme_strerror(s.reason))
109 for s in self.signers)
111 class VerificationError(GpgError):
114 class BadSignatures(VerificationError):
115 def __init__(self, result):
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)
123 class MissingSignatures(VerificationError):
124 def __init__(self, result, missing):
126 self.missing = missing
128 return ", ".join(k.subkeys[0].fpr for k in self.missing)