mailpipe|handler: add `complete` option to control Response mangling.
[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         try:
47             super(InvalidSubjectMessage, self).__init__(**kwargs)
48         except TypeError:
49             raise ValueError(kwargs)
50         self.subject = subject
51
52
53 class Response (Exception):
54     """Exception to bubble out email responses.
55
56     Rather than sending email responses themselves, handlers should
57     raise this exception.  The caller can catch it and mail the email
58     (or take other appropriate action).
59     """
60     def __init__(self, message=None, complete=False):
61         super(Response, self).__init__()
62         self.message = message
63         self.complete = complete