<commit_before>import binascii
import uu
from cStringIO import StringIO
from email import encoders
from email.message import Message

class EncodeMessage(Message):
    """Just like a normal email.message.Message, but it automatically enocdes body parts"""
    def get_payload(self, i=None, encode=True):
        # taken from http://hg.python.org/cpython/file/0926adcc335c/Lib/email/message.py
        # Copyright (C) 2001-2006 Python Software Foundation
        # See PY-LIC for licence
        if i is None:
            payload = self._payload
        elif not isinstance(self._payload, list):
            raise TypeError('Expected list, got %s' % type(self._payload))
        else:
            payload = self._payload[i]
        if encode:
            if self.is_multipart():
                return None
            cte = self.get('content-transfer-encoding', '').lower()
            if cte == 'quoted-printable':
                return encoders._qencode(payload)
            elif cte == 'base64':
                try:
                    return encoders._bencode(payload)
                except binascii.Error:
                    # Incorrect padding
                    return payload
            elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
                sfp = StringIO()
                try:
                    uu.encode(StringIO(payload+'\n'), sfp, quiet=True)
                    payload = sfp.getvalue()
                except uu.Error:
                    # Some decoding problem
                    return payload
        # Everything else, including encodings with 8bit or 7bit are returned
        # unchanged.
        return payload
<commit_msg>liberate.utils: Return payload for multipart messages<commit_after>import binascii
import uu
from cStringIO import StringIO
from email import encoders
from email.message import Message

class EncodeMessage(Message):
    """Just like a normal email.message.Message, but it automatically enocdes body parts"""
    def get_payload(self, i=None):
        # taken from http://hg.python.org/cpython/file/0926adcc335c/Lib/email/message.py
        # Copyright (C) 2001-2006 Python Software Foundation
        # See PY-LIC for licence
        if i is None:
            payload = self._payload
        elif not isinstance(self._payload, list):
            raise TypeError('Expected list, got %s' % type(self._payload))
        else:
            payload = self._payload[i]

        if self.is_multipart():
            return payload
        cte = self.get('content-transfer-encoding', '').lower()
        if cte == 'quoted-printable':
            return encoders._qencode(payload)
        elif cte == 'base64':
            try:
                return encoders._bencode(payload)
            except binascii.Error:
                # Incorrect padding
                return payload
        elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):
            sfp = StringIO()
            try:
                uu.encode(StringIO(payload+'\n'), sfp, quiet=True)
                payload = sfp.getvalue()
            except uu.Error:
                # Some decoding problem
                return payload
        # Everything else, including encodings with 8bit or 7bit are returned
        # unchanged.
        return payload

{'fraction_non_alphanumeric': 0.0898842169408897, 'fraction_numerical': 0.01157830591102986, 'mean_word_length': 1.8572671888598782, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6227889', 'n_tokens_mistral': 911, 'n_tokens_neox': 850, 'n_words': 254}