From c5464efdf61bb3a7f5ead1240bc4a5b3fbb271fc Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 28 Mar 2010 01:31:47 -0400 Subject: [PATCH] Added rendered world part to EmailPlayer notification --- pyrisk/graphics.py | 10 +++++++--- pyrisk/player/email.py | 18 ++++++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/pyrisk/graphics.py b/pyrisk/graphics.py index 7085eb3..a2fc250 100644 --- a/pyrisk/graphics.py +++ b/pyrisk/graphics.py @@ -502,6 +502,13 @@ class WorldRenderer (object): self.line_width = line_width self.dpcm = dpcm self.army_scale = 3 + def filename_and_mime_image_type(self, world): + """Return suggestions for emailing the rendered object. + + Returns (filename, subtype), where the MIME type is + image/. + """ + return ('%s.svg' % world.name, 'svg+xml') def render(self, world, players): template = self.template_lib.get(world.name) if template == None: @@ -682,6 +689,3 @@ def render_earth(): e.setup() r = WorldRenderer() print r.render(e.world, players) - #f = open('world.svg', 'w') - #f.write(r.render(generate_earth())) - #f.close() diff --git a/pyrisk/player/email.py b/pyrisk/player/email.py index 67bc7f9..4feb957 100644 --- a/pyrisk/player/email.py +++ b/pyrisk/player/email.py @@ -21,8 +21,9 @@ from __future__ import absolute_import from cStringIO import StringIO from email.generator import Generator from email.parser import Parser -from email.mime.text import MIMEText +from email.mime.image import MIMEImage from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText from email.utils import getaddresses, formataddr, formatdate, make_msgid import os import smtplib @@ -31,6 +32,7 @@ import sys import tempfile from ..base import Player, PlayerError +from ..graphics import WorldRenderer # Configure alternative sendmail command in case smtplib is too @@ -218,11 +220,12 @@ class EmailPlayer (Player): TODO: details on procmail setup """ - def __init__(self, name, address, incoming, outgoing): + def __init__(self, name, address, incoming, outgoing, world_renderer=None): Player.__init__(self, name) self.address = address self.outgoing = outgoing self.incoming = incoming + self.world_renderer = world_renderer def _tag(self): return '[PyRisk %d]' % (id(self)) def _send_mail(self, world, log, subject, body): @@ -230,6 +233,8 @@ class EmailPlayer (Player): msg.attach(encodedMIMEText(body, filename='body')) msg.attach(self._log_part(log)) msg.attach(self._world_part(world)) + if self.world_renderer != None: + msg.attach(self._rendered_world_part(world, log)) msg['To'] = formataddr((self.name, self.address)) tag = self._tag() msg['Subject'] = '%s %s' % (tag, subject) @@ -266,6 +271,15 @@ class EmailPlayer (Player): else: body.append(' %s\t%s\t%d' % (terr, terr.player, terr.armies)) return encodedMIMEText('\n'.join(body), filename='world') + def _rendered_world_part(self, log, world): + start_event = log[0] + players = start_event.players + body = self.world_renderer.render(world, players) + filename,subtype = \ + self.world_renderer.filename_and_mime_image_type(world) + part = MIMEImage(body, subtype) + part.add_header('Content-Disposition', 'attachment', filename) + return part def _log_part(self, log): return encodedMIMEText('\n'.join(log), filename='log') def report(self, world, log): -- 2.26.2