From 369d60f237efa03709dbef299e4727b80f154fab Mon Sep 17 00:00:00 2001 From: Roger Hoover Date: Wed, 1 Aug 2012 17:51:05 -0700 Subject: [PATCH] updated and tested cmd line option parser --- superlance/process_state_email_monitor.py | 19 ++++++++++++++++-- .../tests/process_state_email_monitor_test.py | 20 +++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/superlance/process_state_email_monitor.py b/superlance/process_state_email_monitor.py index 7eeb4d0..71067ba 100644 --- a/superlance/process_state_email_monitor.py +++ b/superlance/process_state_email_monitor.py @@ -30,7 +30,7 @@ class ProcessStateEmailMonitor(ProcessStateMonitor): COMMASPACE = ', ' @classmethod - def create_from_cmd_line(cls): + def parse_cmd_line_options(cls): from optparse import OptionParser parser = OptionParser() @@ -48,13 +48,28 @@ def create_from_cmd_line(cls): help="TICK event name (defaults to TICK_60)") (options, args) = parser.parse_args() - + return options + + @classmethod + def validate_cmd_line_options(cls, options): if not options.to_emails: parser.print_help() sys.exit(1) if not options.from_email: parser.print_help() sys.exit(1) + + validated = copy.copy(options) + validated.to_emails = [x.strip() for x in options.to_emails.split(",")] + return validated + + @classmethod + def get_cmd_line_options(cls): + return cls.validate_cmd_line_options(cls.parse_cmd_line_options()) + + @classmethod + def create_from_cmd_line(cls): + options = cls.get_cmd_line_options() if not 'SUPERVISOR_SERVER_URL' in os.environ: sys.stderr.write('Must run as a supervisor event listener\n') diff --git a/superlance/tests/process_state_email_monitor_test.py b/superlance/tests/process_state_email_monitor_test.py index d956499..cec78f1 100644 --- a/superlance/tests/process_state_email_monitor_test.py +++ b/superlance/tests/process_state_email_monitor_test.py @@ -37,6 +37,26 @@ def _make_one_mock_send_smtp(self, **kwargs): obj = self._make_one(**kwargs) obj.send_smtp = mock.Mock() return obj + + def test_validate_cmd_line_options_single_to_email_ok(self): + klass = self._get_target_class() + + options = mock.Mock() + options.from_email = 'blah' + options.to_emails = 'frog' + + validated = klass.validate_cmd_line_options(options) + self.assertEquals(['frog'], validated.to_emails) + + def test_validate_cmd_line_options_multi_to_emails_ok(self): + klass = self._get_target_class() + + options = mock.Mock() + options.from_email = 'blah' + options.to_emails = 'frog, log,dog' + + validated = klass.validate_cmd_line_options(options) + self.assertEquals(['frog', 'log', 'dog'], validated.to_emails) def test_send_email_ok(self): email = {