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/<subtype>.
+ """
+ return ('%s.svg' % world.name, 'svg+xml')
def render(self, world, players):
template = self.template_lib.get(world.name)
if template == None:
e.setup()
r = WorldRenderer()
print r.render(e.world, players)
- #f = open('world.svg', 'w')
- #f.write(r.render(generate_earth()))
- #f.close()
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
import tempfile
from ..base import Player, PlayerError
+from ..graphics import WorldRenderer
# Configure alternative sendmail command in case smtplib is too
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):
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)
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):