Jason R. Coombs avatar Jason R. Coombs committed 7ac61f0

Some improvements to notification.py

Comments (0)

Files changed (1)


 # -*- coding: UTF-8 -*-
 	Classes for notifying in the case of an event.
 	All objects should support the .write method to append data and
 		SMTPMailbox - sends a message to an SMTP mailbox upon
-Copyright © 2004-2008 Jason R. Coombs
+Copyright © 2004-2011 Jason R. Coombs
+from __future__ import print_function
 import string
 import smtplib
 import socket
 import sys
 import traceback
-from StringIO import StringIO
+import io
 from jaraco.util.dictlib import DictFilter
 from jaraco.util.itertools import flatten
 		return '%(class_name)s <notifier@%(machine_name)s>' % vars()
 	def notify(self, msg = '', importance = 'Normal', subject='Notification'):
-		import smtplib
 		headers = dict(
 			From = self.from_addr,
 			To = self.to_addrs,
 			Importance = importance,
 			Subject = subject,
-			)
+		)
 		if hasattr(self, 'cc_addrs'):
 			headers['CC'] = self.cc_addrs
-			self.format_message(headers, msg))
+			self.format_message(headers, msg)
+		)
 	def dest_addrs(self):
-		from string import strip
-		get_attr = lambda a: getattr(self, a, None)
-		to_attrs = ('to_addrs', 'cc_addrs', 'bcc_addrs')
-		values = filter(None, map(get_attr, to_attrs))
-		split_email = lambda s: map(strip, s.split(','))
-		all_addrs = flatten(map(split_email, values))
-		return all_addrs
+		return itertools.chain.from_iterable(
+			comma_separated_values(getattr(self, key, ''))
+			for key in ('to_addrs', 'cc_addrs', 'bcc_addrs')
+		)
 	def get_connect_args(self):
 		attrs = 'host', 'port'
 	def _get_buffer(self):
-		return self.__dict__.setdefault('buffer', StringIO())
+		return self.__dict__.setdefault('buffer', io.StringIO())
 	def __del__(self):
 		# note, the documentation warns against performing external
 			return self.target_func(*args, **kargs)
 		except Exception, e:
-			print >> self, 'Unhandled exception encountered'
+			print('Unhandled exception encountered', file=self)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.