~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/email_message.py

  • Committer: Adeodato Simó
  • Date: 2007-07-19 21:22:53 UTC
  • mto: (2639.1.1 jam-integration)
  • mto: This revision was merged to the branch mainline in revision 2640.
  • Revision ID: dato@net.com.org.es-20070719212253-iaqokj16w681svwe
Changes after review by John.

* bzrlib/email_message.py:
  (EmailMessage): inherit from object.
  (EmailMessage.__getitem__): fix grammar in docstring.

* bzrlib/email_message.py,
  bzrlib/tests/test_email_message.py:
  Do not use variables named "string".

* bzrlib/tests/test_email_message.py:
  (TestEmailMessage.test_send): do not assert inside the verify_*
  functions that replace SMTPConnection.send_email. Instead, append the
  received message to a local variable, and call assert in the main body
  function.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
from bzrlib.smtp_connection import SMTPConnection
30
30
 
31
31
 
32
 
class EmailMessage:
 
32
class EmailMessage(object):
33
33
    """An email message.
34
34
    
35
35
    The constructor needs an origin address, a destination address or addresses
107
107
        if not self._parts:
108
108
            self._msgobj = Message.Message()
109
109
            if self._body is not None:
110
 
                string, encoding = self.string_with_encoding(self._body)
111
 
                self._msgobj.set_payload(string, encoding)
 
110
                body, encoding = self.string_with_encoding(self._body)
 
111
                self._msgobj.set_payload(body, encoding)
112
112
        else:
113
113
            self._msgobj = MIMEMultipart.MIMEMultipart()
114
114
 
116
116
                self._msgobj.set_boundary(boundary)
117
117
 
118
118
            for body, filename, mime_subtype in self._parts:
119
 
                string, encoding = self.string_with_encoding(body)
120
 
                payload = MIMEText.MIMEText(string, mime_subtype, encoding)
 
119
                body, encoding = self.string_with_encoding(body)
 
120
                payload = MIMEText.MIMEText(body, mime_subtype, encoding)
121
121
 
122
122
                if filename is not None:
123
123
                    content_type = payload['Content-Type']
142
142
    def __getitem__(self, header):
143
143
        """Get a header from the message, returning None if not present.
144
144
        
145
 
        This method does intentionally not raise KeyError to mimic the behavior
 
145
        This method intentionally does not raise KeyError to mimic the behavior
146
146
        of __getitem__ in email.Message.
147
147
        """
148
148
        return self._headers.get(header, None)
183
183
                email))
184
184
 
185
185
    @staticmethod
186
 
    def string_with_encoding(string):
 
186
    def string_with_encoding(string_):
187
187
        """Return a str object together with an encoding.
188
188
 
189
 
        :param string: A str or unicode object.
 
189
        :param string_: A str or unicode object.
190
190
        :return: A tuple (str, encoding), where encoding is one of 'ascii',
191
191
            'utf-8', or '8-bit', in that preferred order.
192
192
        """
195
195
        # avoid base64 when it's not necessary in order to be most compatible
196
196
        # with the capabilities of the receiving side, we check with encode()
197
197
        # and decode() whether the body is actually ascii-only.
198
 
        if isinstance(string, unicode):
 
198
        if isinstance(string_, unicode):
199
199
            try:
200
 
                return (string.encode('ascii'), 'ascii')
 
200
                return (string_.encode('ascii'), 'ascii')
201
201
            except UnicodeEncodeError:
202
 
                return (string.encode('utf-8'), 'utf-8')
 
202
                return (string_.encode('utf-8'), 'utf-8')
203
203
        else:
204
204
            try:
205
 
                string.decode('ascii')
206
 
                return (string, 'ascii')
 
205
                string_.decode('ascii')
 
206
                return (string_, 'ascii')
207
207
            except UnicodeDecodeError:
208
208
                try:
209
 
                    string.decode('utf-8')
210
 
                    return (string, 'utf-8')
 
209
                    string_.decode('utf-8')
 
210
                    return (string_, 'utf-8')
211
211
                except UnicodeDecodeError:
212
 
                    return (string, '8-bit')
 
212
                    return (string_, '8-bit')