Package turbomail :: Module wrappedmessage
[hide private]
[frames] | no frames]

Source Code for Module turbomail.wrappedmessage

  1  # encoding: utf-8 
  2   
  3  import email 
  4   
  5  from turbomail.compat import make_msgid 
  6  from turbomail.message import BaseMessage 
  7  from turbomail.util import Address, AddressList 
  8   
  9   
 10  __all__ = ['WrappedMessage'] 
 11   
 12   
 13   
14 -class ImmutableProxy(object):
15 """This is a wrapper for email.Message so that a user can not modify the 16 Message instance easily - actually modifications won't have any effect on 17 the underlying message class but with this wrapper newbies won't fall into 18 the trap."""
19 - def __init__(self, obj):
20 self._obj = obj
21
22 - def __contains__(self, name):
23 return (name in self._obj)
24
25 - def __getattr__(self, name):
26 if name is not None: 27 for prefix in ['set_', 'replace_', 'add_', 'del_']: 28 if name.startswith(prefix): 29 raise TypeError('This instance is not mutable!') 30 if name in ['attach']: 31 raise TypeError('This instance is not mutable!') 32 return getattr(self._obj, name)
33
34 - def __getitem__(self, name):
35 return self._obj[name]
36
37 - def __len__(self):
38 return len(self._obj)
39 40
41 -class WrappedMessage(BaseMessage):
42 """This is a wrapper for arbitrary messages so you can send RFC822 messages 43 that were previously assembled (e.g. read from the filesystem).""" 44
45 - def __init__(self, smtp_from=None, to=None, message=None, **kw):
46 super(WrappedMessage, self).__init__(smtp_from=smtp_from, to=to, kw=kw) 47 self.message = message 48 self._id = None 49 self._email_msg = None 50 if len(kw) > 0: 51 parameter_name = kw.keys()[0] 52 error_msg = "__init__() got an unexpected keyword argument '%s'" 53 raise TypeError(error_msg % parameter_name)
54
55 - def __setattr__(self, name, value):
56 "Reset cached values if new ones are set." 57 super(WrappedMessage, self).__setattr__(name, value) 58 if name == 'message': 59 # Prevent recursion - circumvent __setattr__ 60 self.__dict__['_id'] = None 61 self.__dict__['_email_msg'] = None
62
63 - def __str__(self):
64 return self.message
65
66 - def envelope_sender(self):
67 """Returns the address of the envelope sender address (SMTP from).""" 68 return Address(self.smtp_from)
69 envelope_sender = property(envelope_sender) 70
71 - def id(self):
72 """Get an ID for the message which is used for logging. 73 The idea is to use the message id if it is available, otherwise a 74 generated message id.""" 75 if self._id is None: 76 msg_id = None 77 if self.message is not None: 78 if self.email_msg is not None: 79 msg_id = self.email_msg['Message-ID'] 80 if msg_id is None: 81 msg_id = make_msgid() 82 self.__dict__['_id'] = msg_id 83 return self._id
84 id = property(id) 85
86 - def email_msg(self):
87 """Return an email.Message built from the current message. 88 89 Please note that updating properties in the returned Message will not 90 update the real message content (msg). If you change the message content, 91 a previously returned Message from this method will not be updated, 92 instead a new Message instance will be returned if you call this method 93 again.""" 94 if self._email_msg is None: 95 msg = email.message_from_string(str(self)) 96 self.__dict__['_email_msg'] = ImmutableProxy(msg) 97 return self._email_msg
98 email_msg = property(email_msg) 99
100 - def recipients(self):
101 return AddressList(self.to)
102 recipients = property(recipients)
103