Exemplo n.º 1
0
    def set_charset(self, charset):
        """Set the charset of the payload to a given character set.

        charset can be a Charset instance, a string naming a character set, or
        None.  If it is a string it will be converted to a Charset instance.
        If charset is None, the charset parameter will be removed from the
        Content-Type field.  Anything else will generate a TypeError.

        The message will be assumed to be of type text/* encoded with
        charset.input_charset.  It will be converted to charset.output_charset
        and encoded properly, if needed, when generating the plain text
        representation of the message.  MIME headers (MIME-Version,
        Content-Type, Content-Transfer-Encoding) will be added as needed.

        """
        if charset is None:
            self.del_param('charset')
            self._charset = None
            return
        if isinstance(charset, str):
            charset = bongo.external.email.charset.Charset(charset)
        if not isinstance(charset, bongo.external.email.charset.Charset):
            raise TypeError(charset)
        # BAW: should we accept strings that can serve as arguments to the
        # Charset constructor?

        self._charset = charset
        if not self.has_key('MIME-Version'):
            self.add_header('MIME-Version', '1.0')
        if not self.has_key('Content-Type'):
            self.add_header('Content-Type', 'text/plain',
                            charset=charset.get_output_charset())
        else:
            self.set_param('charset', charset.get_output_charset())

        if not self['Content-Transfer-Encoding'] :
            cte = charset.get_body_encoding()
            try :
                cte(self)
            except TypeError:
                self.add_header('Content-Transfer-Encoding', cte)
Exemplo n.º 2
0
    def iter_payload(self, decode=False):
        payload = self.get_payload_obj()
        if payload is None :
            return
        if not isinstance(payload, payloads.Payload) :
            raise TypeError('unsupported payload type for iterating')

        if self._charset :
            charset = self._charset
        else :
            charset = self._read_charset()
            
        encoding = self['Content-Transfer-Encoding']
                    
        encode = not decode and charset and encoding != payload.encoding
        subdecode = decode or not charset or (encoding != payload.encoding)

        leftover = None
        for buf in payload.iter(subdecode):
            if encode :
                if isinstance(buf, unicode) :
                    buf = buf.encode(charset.get_output_charset())

                if charset.body_encoding == bongo.external.email.charset.BASE64 :
                    # Encode 57 bytes at a time (lines 76 long)
                    if leftover :
                        buf = leftover + buf
                        leftover = None
                    
                        amountLeftover = len(buf) % 57
                        leftover = buf[-amountLeftover:]
                        buf = buf[:-amountLeftover]
                            
                if buf != '' :
                    buf = charset.body_encode(buf)
                else :
                    buf = None

            if buf :
                yield buf

        if leftover:
            leftover = charset.body_encode(leftover)
            yield leftover