From 2a1d31457cfbf051951f16f05a7ff93315c8e80c Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 24 Apr 2012 18:02:09 -0400 Subject: [PATCH] Add --respond option to pg.py's mailpipe command. --- bin/pg.py | 11 +++++++++++ pygrader/email.py | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/bin/pg.py b/bin/pg.py index 5980b01..077b3e3 100755 --- a/bin/pg.py +++ b/bin/pg.py @@ -32,6 +32,7 @@ import pgp_mime as _pgp_mime from pygrader import __version__ from pygrader import LOG as _LOG from pygrader.email import test_smtp as _test_smtp +from pygrader.email import Responder as _Responder from pygrader.mailpipe import mailpipe as _mailpipe from pygrader.storage import initialize as _initialize from pygrader.storage import load_course as _load_course @@ -137,6 +138,10 @@ if __name__ == '__main__': '-l', '--max-late', default=0, type=float, help=('Grace period in seconds before an incoming assignment is ' 'actually marked as late')) + mailpipe_parser.add_argument( + '-r', '--respond', default=False, action='store_const', const=True, + help=('Send automatic response emails to acknowledge incomming ' + 'messages.')) todo_parser = subparsers.add_parser( 'todo', help=_todo.__doc__.splitlines()[0]) @@ -215,6 +220,12 @@ if __name__ == '__main__': kwargs['smtp'] = _pgp_mime.get_smtp(*params) del params + if hasattr(args, 'respond') and getattr(args, 'respond'): + kwargs['respond'] = _Responder( + smtp=kwargs.get('smtp', None), + use_color=kwargs.get('use_color', False), + dry_run=kwargs.get('dry_run', False)) + _LOG.debug('execute {} with {}'.format(args.func, kwargs)) try: ret = args.func(**kwargs) diff --git a/pygrader/email.py b/pygrader/email.py index 4fab66b..aeb4da8 100644 --- a/pygrader/email.py +++ b/pygrader/email.py @@ -111,6 +111,18 @@ def send_emails(emails, smtp=None, use_color=None, debug_target=None, if callback: callback(None) + +class Responder (object): + def __init__(self, *args, **kwargs): + self.args = args + if kwargs is None: + kwargs = {} + self.kwargs = kwargs + + def __call__(self, message): + send_emails([(message, None)], *self.args, **self.kwargs) + + def get_address(person, header=False): r""" >>> from pygrader.model.person import Person as Person -- 2.26.2