handler: remove TypeError check InvalidSubjectMessage.__init__.
[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     def message_id(self):
15         """Return a short string identifying the invalid message.
16         """
17         if self.message is None:
18             return None
19         subject = self.message['Subject']
20         if subject is not None:
21             return repr(subject)
22         message_id = self.message['Message-ID']
23         if message_id is not None:
24             return message_id
25         return None
26
27
28 class InsecureMessage (InvalidMessage):
29     def __init__(self, **kwargs):
30         if 'error' not in kwargs:
31             kwargs['error'] = 'insecure message'
32         super(InsecureMessage, self).__init__(**kwargs)
33
34
35 class UnsignedMessage (InsecureMessage):
36     def __init__(self, **kwargs):
37         if 'error' not in kwargs:
38             kwargs['error'] = 'unsigned message'
39         super(UnsignedMessage, self).__init__(**kwargs)
40
41
42 class InvalidSubjectMessage (InvalidMessage):
43     def __init__(self, subject=None, **kwargs):
44         if 'error' not in kwargs:
45             kwargs['error'] = 'invalid subject {!r}'.format(subject)
46         super(InvalidSubjectMessage, self).__init__(**kwargs)
47         self.subject = subject
48
49
50 class Response (Exception):
51     """Exception to bubble out email responses.
52
53     Rather than sending email responses themselves, handlers should
54     raise this exception.  The caller can catch it and mail the email
55     (or take other appropriate action).
56     """
57     def __init__(self, message=None, complete=False):
58         super(Response, self).__init__()
59         self.message = message
60         self.complete = complete