mailpipe: replace `respond` callback with exceptions.
[pygrader.git] / pygrader / handler / __init__.py
1 # Copyright
2
3 "Define assorted handlers for use in :py:mod:`~pygrader.mailpipe`."
4
5 import pgp_mime as _pgp_mime
6
7
8 class InvalidMessage (ValueError):
9     def __init__(self, message=None, error=None):
10         super(InvalidMessage, self).__init__(error)
11         self.message = message
12         self.error = error
13
14
15 class UnsignedMessage (InvalidMessage):
16     def __init__(self, **kwargs):
17         if 'error' not in kwargs:
18             kwargs['error'] = 'unsigned message'
19         super(UnsignedMessage, self).__init__(**kwargs)
20
21
22 class InvalidSubjectMessage (InvalidMessage):
23     def __init__(self, subject=None, **kwargs):
24         if 'error' not in kwargs:
25             kwargs['error'] = 'invalid subject {!r}'.format(subject)
26         try:
27             super(InvalidSubjectMessage, self).__init__(**kwargs)
28         except TypeError:
29             raise ValueError(kwargs)
30         self.subject = subject
31
32
33 class Response (Exception):
34     """Exception to bubble out email responses.
35
36     Rather than sending email responses themselves, handlers should
37     raise this exception.  The caller can catch it and mail the email
38     (or take other appropriate action).
39     """
40     def __init__(self, message=None):
41         super(Response, self).__init__()
42         self.message = message